Hello, recently I ran into Ruby Structs<http://www.ruby-doc.org/core-1.9.3/Struct.html>. Some examples: # Create a structure with a name in StructStruct.new("Customer", :name, :address) #=> Struct::CustomerStruct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> # Create a structure named by its constantCustomer = Struct.new(:name, :address) #=> CustomerCustomer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main"> Automatic key -> symbol, key -> string, key -> method accessing: Customer = Struct.new(:name, :address, :zip)joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe["name"] = "Luke"joe[:zip] = "90210" joe.name #=> "Luke"joe.zip #=> "90210" It seems to me it fits Model representation perfectly, so I wonder if a Model could have advantages being represented by a Struct class (performance, coherence...). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/4KvuuN8HbnMJ. 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.
The DAO pattern is totally different than the ActiveRecord pattern, so you probably won''t see anything like this happen inside of AR itself. Other ORMs may do something like this, though. If your model is just a struct, then it has no ''domain'' methods... -- 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.
Maurizio De Santis
2012-Jul-22 16:20 UTC
Re: Could Ruby Structs be useful to ActiveRecord?
What do you mean for ''domain'' methods? Do you mean validations, etc? They can be implemented; the model could be a sort of "struct on steroids"... something like this: module ActiveRecord class Struct < ::Struct def self.find end def self.all end # other various implementations end class StructFor def [](table_name) # retrieve table column names ActiveRecord:::Struct.new(*table_column_names) end end end model: class User < ActiveRecord::StructFor[:users] # this returns ActiveRStruct.new(:firstname, :lastname, :active) def before_save # ... end end Il giorno sabato 21 luglio 2012 22:03:06 UTC+2, Steve Klabnik ha scritto:> > The DAO pattern is totally different than the ActiveRecord pattern, so > you probably won''t see anything like this happen inside of AR itself. > Other ORMs may do something like this, though. > > If your model is just a struct, then it has no ''domain'' methods... >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/KAK9EQK1Qs8J. 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.
Yeah, you''d _have_ to do something like that. Then you''re not implementing the ActiveRecord pattern anymore, so I don''t think it''d really be appropriate.> Active Record uses the most obvious approach, putting data access logic in the domain object. > > - http://martinfowler.com/eaaCatalog/activeRecord.html-- 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.