I frequently find the need to have an object with specific attributes, preferably initializable with a code block and/or a hash, that I also want serializable/deserializable to a hash. Example: class Foo attr_accessor :bar, :none end foo = Foo.new do |f| f.bar = true f.none = false end puts s = foo.to_hash { :bar => true, :none => false} foo = Foo.from_hash(s) Of course I could write a library to do this, but it seems obvious enough that it would exist. Am I missing it?
Hi, Maybe something like the following using OpenStruct? require "ostruct" class HashableStruct < OpenStruct def self.from_hash(hash) new(hash) end def to_hash @table end end f = HashableStruct.from_hash(:bar => true, :none => false) p f # => #<HashableStruct bar=true, none=false> p f.to_hash # => {:bar=>true, :none=>false} I don''t see the benefit of using a block though, it only adds another line. HTH, Eloy On Jul 7, 2009, at 8:42 AM, Alex wrote:> > I frequently find the need to have an object with specific attributes, > preferably initializable with a code block and/or a hash, that I also > want serializable/deserializable to a hash. > > Example: > > class Foo > attr_accessor :bar, :none > end > > foo = Foo.new do |f| > f.bar = true > f.none = false > end > > puts s = foo.to_hash > > { :bar => true, :none => false} > > foo = Foo.from_hash(s) > > Of course I could write a library to do this, but it seems obvious > enough that it would exist. Am I missing it? > > > >
Thanks for the suggestion. That looks like what I need. The benefits of the block are that you are writing to the object instead of a hashtable, so incorrect attributes are caught sooner. It also allows more flexibility in the code, i.e. everything doesn''t have to be one statement so you can do loops, conditionals, etc. On Jul 7, 12:27 am, Eloy Duran <eloy.de.en...@gmail.com> wrote:> Hi, > > Maybe something like the following using OpenStruct? > > require "ostruct" > > class HashableStruct < OpenStruct > def self.from_hash(hash) > new(hash) > end > > def to_hash > @table > end > end > > f = HashableStruct.from_hash(:bar => true, :none => false) > p f # => #<HashableStruct bar=true, none=false> > p f.to_hash # => {:bar=>true, :none=>false} > > I don''t see the benefit of using a block though, it only adds another > line. > > HTH, > Eloy > > On Jul 7, 2009, at 8:42 AM, Alex wrote: > > > > > > > I frequently find the need to have an object with specific attributes, > > preferably initializable with a code block and/or a hash, that I also > > want serializable/deserializable to a hash. > > > Example: > > > class Foo > > attr_accessor :bar, :none > > end > > > foo = Foo.new do |f| > > f.bar = true > > f.none = false > > end > > > puts s = foo.to_hash > > > { :bar => true, :none => false} > > > foo = Foo.from_hash(s) > > > Of course I could write a library to do this, but it seems obvious > > enough that it would exist. Am I missing it?
Well, ok, except it doesn''t catch invalid attributes since you can''t restrict them. Kind of icky but it works. On Jul 7, 12:27 am, Eloy Duran <eloy.de.en...@gmail.com> wrote:> Hi, > > Maybe something like the following using OpenStruct? > > require "ostruct" > > class HashableStruct < OpenStruct > def self.from_hash(hash) > new(hash) > end > > def to_hash > @table > end > end > > f = HashableStruct.from_hash(:bar => true, :none => false) > p f # => #<HashableStruct bar=true, none=false> > p f.to_hash # => {:bar=>true, :none=>false} > > I don''t see the benefit of using a block though, it only adds another > line. > > HTH, > Eloy > > On Jul 7, 2009, at 8:42 AM, Alex wrote: > > > > > > > I frequently find the need to have an object with specific attributes, > > preferably initializable with a code block and/or a hash, that I also > > want serializable/deserializable to a hash. > > > Example: > > > class Foo > > attr_accessor :bar, :none > > end > > > foo = Foo.new do |f| > > f.bar = true > > f.none = false > > end > > > puts s = foo.to_hash > > > { :bar => true, :none => false} > > > foo = Foo.from_hash(s) > > > Of course I could write a library to do this, but it seems obvious > > enough that it would exist. Am I missing it?
I think that Struct might be your best bet: https://gist.github.com/afe18b5713b036e2324f It''ll disallow invalid keys, plus, is way faster than OpenStruct. Pat On Wed, Jul 8, 2009 at 4:23 AM, Alex <alex@liivid.com> wrote:> > Well, ok, except it doesn''t catch invalid attributes since you can''t > restrict them. Kind of icky but it works. > > On Jul 7, 12:27 am, Eloy Duran <eloy.de.en...@gmail.com> wrote: > > Hi, > > > > Maybe something like the following using OpenStruct? > > > > require "ostruct" > > > > class HashableStruct < OpenStruct > > def self.from_hash(hash) > > new(hash) > > end > > > > def to_hash > > @table > > end > > end > > > > f = HashableStruct.from_hash(:bar => true, :none => false) > > p f # => #<HashableStruct bar=true, none=false> > > p f.to_hash # => {:bar=>true, :none=>false} > > > > I don''t see the benefit of using a block though, it only adds another > > line. > > > > HTH, > > Eloy > > > > On Jul 7, 2009, at 8:42 AM, Alex wrote: > > > > > > > > > > > > > I frequently find the need to have an object with specific attributes, > > > preferably initializable with a code block and/or a hash, that I also > > > want serializable/deserializable to a hash. > > > > > Example: > > > > > class Foo > > > attr_accessor :bar, :none > > > end > > > > > foo = Foo.new do |f| > > > f.bar = true > > > f.none = false > > > end > > > > > puts s = foo.to_hash > > > > > { :bar => true, :none => false} > > > > > foo = Foo.from_hash(s) > > > > > Of course I could write a library to do this, but it seems obvious > > > enough that it would exist. Am I missing it? > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Pat, thanks for that great example! On Jul 8, 1:50 pm, Pat Nakajima <patnakaj...@gmail.com> wrote:> I think that Struct might be your best bet:https://gist.github.com/afe18b5713b036e2324f > It''ll disallow invalid keys, plus, is way faster than OpenStruct. > > Pat > > > > On Wed, Jul 8, 2009 at 4:23 AM, Alex <a...@liivid.com> wrote: > > > Well, ok, except it doesn''t catch invalid attributes since you can''t > > restrict them. Kind of icky but it works. > > > On Jul 7, 12:27 am, Eloy Duran <eloy.de.en...@gmail.com> wrote: > > > Hi, > > > > Maybe something like the following using OpenStruct? > > > > require "ostruct" > > > > class HashableStruct < OpenStruct > > > def self.from_hash(hash) > > > new(hash) > > > end > > > > def to_hash > > > @table > > > end > > > end > > > > f = HashableStruct.from_hash(:bar => true, :none => false) > > > p f # => #<HashableStruct bar=true, none=false> > > > p f.to_hash # => {:bar=>true, :none=>false} > > > > I don''t see the benefit of using a block though, it only adds another > > > line. > > > > HTH, > > > Eloy > > > > On Jul 7, 2009, at 8:42 AM, Alex wrote: > > > > > I frequently find the need to have an object with specific attributes, > > > > preferably initializable with a code block and/or a hash, that I also > > > > want serializable/deserializable to a hash. > > > > > Example: > > > > > class Foo > > > > attr_accessor :bar, :none > > > > end > > > > > foo = Foo.new do |f| > > > > f.bar = true > > > > f.none = false > > > > end > > > > > puts s = foo.to_hash > > > > > { :bar => true, :none => false} > > > > > foo = Foo.from_hash(s) > > > > > Of course I could write a library to do this, but it seems obvious > > > > enough that it would exist. Am I missing it?