Class: Doodle::KeyedAttribute

Inherits:
AttributeCollector show all
Defined in:
lib/doodle.rb

Overview

define collector methods for hash-like attribute collectors

Constant Summary

Constants inherited from Doodle

Helper

Instance Method Summary collapse

Methods inherited from AttributeCollector

#initialize, #resolve_collector_class, #resolve_value

Methods inherited from DoodleAttribute

#default_defined?, #init_defined?, #optional?, params_from_args, #required?, #validate!

Methods inherited from Doodle

context, raise_exception_on_error, raise_exception_on_error=

Methods included from Core

included

Constructor Details

This class inherits a constructor from Doodle::AttributeCollector

Instance Method Details

#define_collectionObject

define a collector for keyed collections

  • collection should provide a :[] method



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File 'lib/doodle.rb', line 1133

def define_collection
  # need to use string eval because passing block
  if collector_class.nil?
    doodle_owner.sc_eval("def #{collector_name}(*args, &block)
               junk = #{name} if !#{name} # force initialization for classes
               args.each do |arg|
                 #{name}[arg.send(:#{key})] = arg
               end
             end", __FILE__, __LINE__)
  else
    doodle_owner.sc_eval("def #{collector_name}(*args, &block)
                      junk = #{name} if !#{name} # force initialization for classes
                      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

#post_process(results) ⇒ Object



1124
1125
1126
1127
1128
1129
# File 'lib/doodle.rb', line 1124

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