Sha256: 0ffca524b2a315060e0d65758319295eed1e62463993c33ca79430e60978386a

Contents?: true

Size: 843 Bytes

Versions: 28

Compression:

Stored size: 843 Bytes

Contents

# This is similar to Representable::Serializer and allows to apply a piece of logic (the
# block passed to #call) to every twin for this property.
#
# For a scalar property, this will be run once and yield the property's value.
# For a collection, this is run per item and yields the item.
class Disposable::Twin::PropertyProcessor
  def initialize(definition, twin, value=nil)
    value ||= twin.send(definition.getter)
    @definition = definition
    @value      = value
  end

  def call(&block)
    if @definition[:collection]
      collection!(&block)
    else
      property!(&block)
    end
  end

private
  def collection!
    # FIXME: the nil collection is not tested, yet!
    (@value || []).collect { |nested_twin| yield(nested_twin) }
  end

  def property!
    twin = @value or return nil
    nested_model = yield(twin)
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
disposable-0.4.1 lib/disposable/twin/property_processor.rb
disposable-0.4.0 lib/disposable/twin/property_processor.rb
disposable-0.3.2 lib/disposable/twin/property_processor.rb
disposable-0.3.1 lib/disposable/twin/property_processor.rb
disposable-0.3.0 lib/disposable/twin/property_processor.rb
disposable-0.2.6 lib/disposable/twin/property_processor.rb
disposable-0.2.5 lib/disposable/twin/property_processor.rb
disposable-0.2.4 lib/disposable/twin/property_processor.rb
disposable-0.2.3 lib/disposable/twin/property_processor.rb
disposable-0.2.2 lib/disposable/twin/property_processor.rb
disposable-0.2.1 lib/disposable/twin/property_processor.rb
disposable-0.1.15 lib/disposable/twin/property_processor.rb
disposable-0.2.0 lib/disposable/twin/property_processor.rb
disposable-0.2.0.rc2 lib/disposable/twin/property_processor.rb
disposable-0.2.0.rc1 lib/disposable/twin/property_processor.rb
disposable-0.1.14 lib/disposable/twin/property_processor.rb
disposable-0.1.13 lib/disposable/twin/property_processor.rb
disposable-0.1.12 lib/disposable/twin/property_processor.rb
disposable-0.1.11 lib/disposable/twin/property_processor.rb
disposable-0.1.9 lib/disposable/twin/property_processor.rb