Class: Doodle::AppendableAttribute

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

Overview

define collector methods for array-like attribute collectors

Constant Summary

Constants inherited from Doodle

Helper

Instance Method Summary collapse

Methods inherited from AttributeCollector

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

  • collection should provide a :<< method



1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/doodle.rb', line 1098

def define_collection
  if collector_class.nil?
    doodle_owner.sc_eval("def #{collector_name}(*args, &block)
               junk = #{name} if !#{name} # force initialization for classes
               args.unshift(block) if block_given?
               #{name}.<<(*args);
             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})}
                        #{name}.<<(*args)
                      else
                        #{name} << #{collector_class}.new(*args, &block)
                      end
                    end", __FILE__, __LINE__)
  end
end