Class: Loady::AttributeArray

Inherits:
Array
  • Object
show all
Defined in:
lib/loady/attribute_array.rb

Instance Method Summary collapse

Instance Method Details

#to_attributes(names, options = {}) ⇒ Object

usage: aa = Loady::AttributeArray.new([‘john’, ‘doe’]) aa.to_attributes([:first, :last])

> { first: ‘john’, last: ‘doe’ }

options:

strip: false    -- default = true
                -- array values must be strings if :strip is true


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/loady/attribute_array.rb', line 12

def to_attributes(names, options = {})
  options = { strip: true }.merge(options)

  attr_hash = {}

  names.each_with_index do |name, i|
    attr_hash[name] = 
      if i < size && self[i]
        options[:strip] ? self[i].to_s.strip : self[i]
      end
  end

  attr_hash
end