Class: Doodle::AttributeCollector

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

Overview

base class for attribute collector classes

Direct Known Subclasses

AppendableAttribute, KeyedAttribute

Instance Method Summary collapse

Methods inherited from DoodleAttribute

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

Methods inherited from Doodle

context, parent, raise_exception_on_error, raise_exception_on_error=

Methods included from Core

included

Constructor Details

#initialize(*args, &block) ⇒ AttributeCollector

Returns a new instance of AttributeCollector.



1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
# File 'lib/doodle.rb', line 1404

def initialize(*args, &block)
  super
  define_collection
  from Hash do |hash|
    resolve_collector_class
    hash.inject(self.init.clone) do |h, (key, value)|
      h[key] = resolve_value(value)
      h
    end
  end
  from Enumerable do |enum|
    #p [:enum, Enumerable]
    resolve_collector_class
    # this is not very elegant but String is a classified as an
    # Enumerable in 1.8.x (but behaves differently)
    if enum.kind_of?(String) && self.init.kind_of?(String)
      post_process( resolve_value(enum) )
    else
      post_process( enum.map{ |value| resolve_value(value) } )
    end
  end
end

Instance Method Details

#post_process(results) ⇒ Object



1426
1427
1428
1429
# File 'lib/doodle.rb', line 1426

def post_process(results)
  #p [:post_process, results]
  self.init.clone.replace(results)
end

#resolve_collector_classObject



1387
1388
1389
1390
1391
# File 'lib/doodle.rb', line 1387

def resolve_collector_class
  if !collector_class.kind_of?(Class)
    self.collector_class = Doodle::Utils.const_resolve(collector_class)
  end
end

#resolve_value(value) ⇒ Object



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
# File 'lib/doodle.rb', line 1392

def resolve_value(value)
  if value.kind_of?(collector_class)
    #p [:resolve_value, :value, value]
    value
  elsif collector_class.__doodle__.conversions.key?(value.class)
    #p [:resolve_value, :collector_class_from, value]
    collector_class.from(value)
  else
    #p [:resolve_value, :collector_class_new, value]
    collector_class.new(value)
  end
end