Class: Class
- Inherits:
-
Object
- Object
- Class
- Defined in:
- lib/lay.rb
Overview
Summary:
For creating nested object hierarchies.
Usage:
Person.lay(["Aye"], :name => "Bob", :_salary => 10)
The previous line does the following:
- instantiates Person
- passes "Aye" to its constructor
- sets "name" attr to "Bob"
- sets "salary" member variable to 10
- returns the new instance
Instance Method Summary collapse
Instance Method Details
#lay(args = [], attrs = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lay.rb', line 15 def lay(args = [], attrs = {}) # If only first arg passed, it's the attrs args, attrs = nil, args if attrs == {} # Use args list as constructor args if passed o = self.new *args if args o = self.new unless args # Set attrs as member variables attrs.each do |k,v| # If it begins with :_, use instance_eval if k.to_s =~ /^_/ o.instance_variable_set(k.to_s.sub("_", "@"), v) # Else, set parameter else o.send("#{k}=", v) end end o end |