Method: ROM::Mapper::AttributeDSL#unfold

Defined in:
lib/rom/mapper/attribute_dsl.rb

#unfold(name, options = EMPTY_HASH) ⇒ Object

Define an embedded hash attribute that requires “unfold” transformation

Typically this is used in non-sql context to convert array of values (like in Cassandra ‘SET’ or ‘LIST’ types) to array of tuples.

Source values are assigned to the first key, the other keys being left blank.

Examples:

dsl = AttributeDSL.new([])

dsl.unfold(tags: [:name, :type], from: :tags_list)

dsl.unfold :tags, from: :tags_list do
  attribute :name, from: :tag_name
  attribute :type, from: :tag_type
end

See Also:



300
301
302
303
304
305
306
307
308
# File 'lib/rom/mapper/attribute_dsl.rb', line 300

def unfold(name, options = EMPTY_HASH)
  with_attr_options(name, options) do |attr_options|
    old_name = attr_options.fetch(:from, name)
    dsl(old_name, type: :array, unfold: true) do
      attribute name, attr_options
      yield if block_given?
    end
  end
end