Class: Doodle::AppendableAttribute

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

Overview

define collector methods for array-like attribute collectors

Instance Method Summary collapse

Methods inherited from AttributeCollector

#initialize, #post_process, #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 appendable collections

  • collection should provide a :<< method



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/doodle/collector.rb', line 131

def define_collector
  collector_spec.each do |collector_name, collector_class|
    # FIXME: don't use eval in 1.9+
    if collector_class.nil?
      doodle_owner.sc_eval("def #{collector_name}(*args, &block)
               collection = self.#{name}
               args.unshift(block) if block_given?
               collection.<<(*args);
             end", __FILE__, __LINE__)
    else
      doodle_owner.sc_eval("def #{collector_name}(*args, &block)
                      collection = self.send(:#{name})
                      if args.size > 0 and args.all?{|x| x.kind_of?(#{collector_class})}
                        collection.<<(*args)
                      else
                        # FIXME: this is a wierd one - need name here - can't use collection directly...?
                        #{name} << #{collector_class}.new(*args, &block)
                        # this is OK
                        #self.send(:#{name}) << #{collector_class}.new(*args, &block)
                        # but this isn't
                        #collection.<<(#{collector_class}.new(*args, &block))
                      end
                    end", __FILE__, __LINE__)
    end
  end
end