Sha256: 3d94c4006e80689383d43d951a094f003dc0c625c592187d4c7a8ad13069cb23

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

module Candy
  
  # Shared methods to create associations between top-level objects and embedded objects (hashes, 
  # arrays, or Candy::Pieces).
  module Embeddable
    # Tells an embedded object whom it belongs to and what attribute it's associated with.  When
    # its own state changes, it can use this information to update the parent.
    def adopt(parent, attribute)
      @__candy_parent = parent
      @__candy_parent_key = attribute
    end
    
  private
    # If we're an attribute of another object, set our field names accordingly.
    def embedded(fields)
      new_fields = {}
      fields.each{|k,v| new_fields["#{@__candy_parent_key}.#{k}".to_sym] = v}
      new_fields
    end
  
    # Convert hashes and arrays to CandyHashes and CandyArrays.
    def embeddify(value)
      case value
      when CandyHash then value
      when Hash then CandyHash.embed(value)
      when CandyArray then value
      when Array then CandyArray.embed(*value)   # Explode our array into separate arguments
      else
        value
      end
    end
  
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
candy-0.2.6 lib/candy/embeddable.rb
candy-0.2.5 lib/candy/embeddable.rb
candy-0.2.4 lib/candy/embeddable.rb
candy-0.2.3 lib/candy/embeddable.rb
candy-0.2.2 lib/candy/embeddable.rb
candy-0.2.1 lib/candy/embeddable.rb