Class: Doodle::KeyedAttribute

Inherits:
AttributeCollector show all
Defined in:
lib/doodle/collector.rb,
lib/doodle/collector.rb,
lib/doodle/collector-1.9.rb

Overview

define collector methods for hash-like attribute collectors

Instance Method Summary collapse

Methods inherited from AttributeCollector

#initialize, #resolve_collector_class, #resolve_value

Methods inherited from DoodleAttribute

#abstract, #default_defined?, #init_defined?, #optional?, #readonly, #required?, #validate!

Methods included from DoodleAttribute::ClassMethods

#params_from_args

Methods inherited from Doodle

TypedArray, raise_exception_on_error, raise_exception_on_error=

Methods included from ClassMethods

#context, #parent

Methods included from JSON

included

Methods included from Core::ModuleMethods

#included

Constructor Details

This class inherits a constructor from Doodle::AttributeCollector

Instance Method Details

#define_collectorObject

define a collector for keyed collections

  • collection should provide :[], :clone and :replace methods



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/doodle/collector.rb', line 163

def define_collector
  collector_spec.each do |collector_name, collector_class|
    # need to use string eval because passing block
    # FIXME: don't use eval in 1.9+
    if collector_class.nil?
      doodle_owner.sc_eval("def #{collector_name}(*args, &block)
               collection = #{name}
               args.each do |arg|
                 #{name}[arg.send(:#{key})] = arg
               end
             end", __FILE__, __LINE__)
    else
      doodle_owner.sc_eval("def #{collector_name}(*args, &block)
                      collection = #{name}
                      if args.size > 0 and args.all?{|x| x.kind_of?(#{collector_class})}
                        args.each do |arg|
                          #{name}[arg.send(:#{key})] = arg
                        end
                      else
                        obj = #{collector_class}.new(*args, &block)
                        #{name}[obj.send(:#{key})] = obj
                      end
                 end", __FILE__, __LINE__)
    end
  end
end

#post_process(results) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/doodle/collector.rb', line 105

def post_process(results)
  collection = create_collection
  results.inject(collection) do |h, result|
    h[result.send(key)] = result
    h
  end
end