Module: Rails::DataMapper::MultiparameterAttributes

Defined in:
lib/dm-rails/multiparameter_attributes.rb

Overview

Include this module into a DataMapper model to enable multiparameter attributes.

A multiparameter attribute has attr(Xc) as name where attr specifies the attribute, X the position of the value, and c an optional typecast identifier. All values that share an attr are sorted by their position, optionally cast using #to_c (where c is the typecast identifier) and then used to instantiate the proper attribute value.

Examples:

# Assigning a hash with multiparameter values for a +Date+ property called
# +written_on+:
resource.attributes = {
    'written_on(1i)' => '2004',
    'written_on(2i)' => '6',
    'written_on(3i)' => '24' }

# +Date+ will be initialized with each string cast to a number using
# #to_i.
resource.written_on == Date.new(2004, 6, 24)

Instance Method Summary collapse

Instance Method Details

#attributes=(attributes) ⇒ Hash

Merges multiparameter attributes and calls super with the merged attributes.

Parameters:

  • attributes (Hash{String,Symbol=>Object})

    Names and values of attributes to assign.

Returns:

  • (Hash)

    Names and values of attributes assigned.

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dm-rails/multiparameter_attributes.rb', line 82

def attributes=(attributes)
  attribs = attributes.dup
  multi_parameter_attributes = []
  attribs.each do |k, v|
    if k.to_s.include?("(")
      multi_parameter_attributes << [ k, v ]
      attribs.delete(k)
    end
  end

  attribs.merge!(merge_multiparameter_attributes(multi_parameter_attributes))
  super(attribs)
end