New feature suggestion: ActiveRecord store_reader

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

New feature suggestion: ActiveRecord store_reader

zubin
Hello,

I'd like to propose this feature:

Like `attr_reader`, I'd like to see a `store_reader` AR class method which adds store getter methods.

Why? Consider this:

class User < ActiveRecord::Base
  store_accessor
:data, :name
end


user
= User.new(name: "John Doe", data: {fav_tv_show: "Twin Peaks"})
user
.name #=> nil
user
.data #=> {"fav_tv_show" => "Twin Peaks"}

To avoid this, I'd like the opportunity to force writing without the store setter method while providing the convenience store getter method.

Would this addition be considered? If so, I'm happy to work on it.

--
Zubin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New feature suggestion: ActiveRecord store_reader

Matt Jones-15

On Aug 2, 2017, at 7:27 PM, zubin <[hidden email]> wrote:

Hello,

I'd like to propose this feature:

Like `attr_reader`, I'd like to see a `store_reader` AR class method which adds store getter methods.

Why? Consider this:

class User < ActiveRecord::Base
  store_accessor :data, :name
end


user = User.new(name: "John Doe", data: {fav_tv_show: "Twin Peaks"})
user.name #=> nil
user.data #=> {"fav_tv_show" => "Twin Peaks"}

To avoid this, I'd like the opportunity to force writing without the store setter method while providing the convenience store getter method.

Would this addition be considered? If so, I'm happy to work on it.

You could write this with `store_accessor` already:

def self.store_reader(store_attribute, *keys)
  store_accessor(store_attribute, *keys)
  keys.each { |k| undef_method(“#{k}=") }
end

Or swap in “private” for “undef_method” if you want to use the setters internally. 

—Matt Jones

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New feature suggestion: ActiveRecord store_reader

zubin
That's definitely an option (nice two-liner btw) but I'd rather not have to do that in every app (I'm using JSONB columns more and more these days). 

Since there's `attr_accessor` and `attr_reader`, is it not reasonable to have `store_reader` alongside `store_accessor`? 

On Friday, 4 August 2017 02:59:52 UTC+10, Matt Jones wrote:

On Aug 2, 2017, at 7:27 PM, zubin <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="vvj90DjwAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">zubin....@...> wrote:

Hello,

I'd like to propose this feature:

Like `attr_reader`, I'd like to see a `store_reader` AR class method which adds store getter methods.

Why? Consider this:

class User < ActiveRecord::Base
  store_accessor :data, :name
end


user = User.new(name: "John Doe", data: {fav_tv_show: "Twin Peaks"})
user.name #=> nil
user.data #=> {"fav_tv_show" => "Twin Peaks"}

To avoid this, I'd like the opportunity to force writing without the store setter method while providing the convenience store getter method.

Would this addition be considered? If so, I'm happy to work on it.

You could write this with `store_accessor` already:

def self.store_reader(store_attribute, *keys)
  store_accessor(store_attribute, *keys)
  keys.each { |k| undef_method(“#{k}=") }
end

Or swap in “private” for “undef_method” if you want to use the setters internally. 

—Matt Jones

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Loading...