Class: Rumx::ListAttribute

Inherits:
Attribute show all
Defined in:
lib/rumx/list_attribute.rb

Instance Attribute Summary

Attributes inherited from Attribute

#allow_read, #allow_write, #description, #name, #type

Instance Method Summary collapse

Methods inherited from Attribute

#[], #get_value, #param_value

Constructor Details

#initialize(name, type, description, allow_read, allow_write, options) ⇒ ListAttribute

options

list_type - any of the available types (required)
max_size  - max size of the list, can be a symbol representing a method or an integer (defaults to the current size of the list)


7
8
9
10
11
# File 'lib/rumx/list_attribute.rb', line 7

def initialize(name, type, description, allow_read, allow_write, options)
  super
  raise 'List attribute called without list_type option' unless options[:list_type]
  @list_type = Type.find(options[:list_type])
end

Instance Method Details

#each_attribute_info(bean, ancestry, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rumx/list_attribute.rb', line 13

def each_attribute_info(bean, ancestry, &block)
  list = bean.send(name)
  return unless list
  child_ancestry = ancestry+[name]
  index_index = child_ancestry.size
  list.each_with_index do |value, i|
    value = nil unless allow_read
    child_ancestry[index_index] = i
    yield AttributeInfo.new(self, bean, child_ancestry, value)
  end
end

#write?(bean, params) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rumx/list_attribute.rb', line 25

def write?(bean, params)
  #puts "list write params=#{params.inspect}"
  is_written = false
  if allow_write
    list = bean.send(name)
    return false unless list
    max_size = self[:max_size]
    if max_size
      if max_size.kind_of?(Symbol)
        max_size = bean.send(max_size)
      end
    else
      # Default to current size of the list if unset
      max_size = obj.size
    end
    param_value(params) do |sub_params|
      each_param(sub_params) do |index, value|
        if index < max_size
          list[index] = @list_type.string_to_value(value)
          is_written = true
        end
      end
    end
  end
  return is_written
end