Class: Nemo::Util::ObjectCollector
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Nemo::Util::ObjectCollector
- Defined in:
- lib/nemo/util/collector.rb
Overview
Collect a list of objects using minimal syntax, used in conjunction with Nemo::Util::Accessors. Use the << or o methods to add an object. All other messages are forwarded to the most recently added object.
class Person
extend Nemo::Util::Accessors
call_accessor :name, :age
end
collector = Nemo::Util::ObjectCollector.new
people = collector.collect do
o Person.new
name 'Kevin'
age 28
o Person.new
name 'Sara'
age 22
end
people.inspect
# => [<Person @age=28, @name="Kevin">, <Person @age=22, @name="Sara">]
Direct Known Subclasses
Instance Attribute Summary collapse
-
#index_error ⇒ Object
Returns the value of attribute index_error.
-
#objects ⇒ Object
Returns the value of attribute objects.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #collect(&block) ⇒ Object
-
#initialize ⇒ ObjectCollector
constructor
A new instance of ObjectCollector.
- #method_missing(symbol, *args, &block) ⇒ Object
- #o(object) ⇒ Object
Constructor Details
#initialize ⇒ ObjectCollector
Returns a new instance of ObjectCollector.
31 32 33 34 |
# File 'lib/nemo/util/collector.rb', line 31 def initialize @objects = Array.new @index_error = 'no objects defined' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
49 50 51 52 |
# File 'lib/nemo/util/collector.rb', line 49 def method_missing(symbol, *args, &block) raise IndexError, @index_error if @objects.size == 0 @objects.last.send(symbol, *args, &block) end |
Instance Attribute Details
#index_error ⇒ Object
Returns the value of attribute index_error.
24 25 26 |
# File 'lib/nemo/util/collector.rb', line 24 def index_error @index_error end |
#objects ⇒ Object
Returns the value of attribute objects.
25 26 27 |
# File 'lib/nemo/util/collector.rb', line 25 def objects @objects end |
Class Method Details
.collect(&block) ⇒ Object
27 28 29 |
# File 'lib/nemo/util/collector.rb', line 27 def self.collect(&block) self.new.collect(&block) end |
Instance Method Details
#<<(object) ⇒ Object
36 37 38 |
# File 'lib/nemo/util/collector.rb', line 36 def <<(object) @objects << object end |
#collect(&block) ⇒ Object
44 45 46 47 |
# File 'lib/nemo/util/collector.rb', line 44 def collect(&block) instance_eval(&block) if block_given? @objects end |
#o(object) ⇒ Object
40 41 42 |
# File 'lib/nemo/util/collector.rb', line 40 def o(object) self << object end |