Class: SSO::Elements::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/sso/elements/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElement

Returns a new instance of Element.



6
7
8
# File 'lib/sso/elements/element.rb', line 6

def initialize
  @fields = {}
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



4
5
6
# File 'lib/sso/elements/element.rb', line 4

def fields
  @fields
end

Class Method Details

.from_value(config, value) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sso/elements/element.rb', line 94

def self.from_value(config, value)
  element = Element.new

  value.each_with_index do |data, index|
    field = config.fields[index]
    type = config.types[index]

    if config.grouped_field_quantity[field] <= 1
      element.set_field(field, Field.new(type, data, index))
    else
      element.set_multi_field(field, Field.new(type, data, index))
    end
  end

  element
end

Instance Method Details

#[](index) ⇒ Object



42
43
44
# File 'lib/sso/elements/element.rb', line 42

def [](index)
  @fields[index].value
end

#[]=(index, value) ⇒ Object



46
47
48
# File 'lib/sso/elements/element.rb', line 46

def []=(index, value)
  @fields[index].value = value
end

#cloneObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/sso/elements/element.rb', line 27

def clone
  element = Element.new
  element.fields = {}

  @fields.each do |name, field|
    element.set_field(name, Field.new(field.type, field.value, field.index))
  end

  element
end

#compactObject



50
51
52
53
54
55
56
57
58
# File 'lib/sso/elements/element.rb', line 50

def compact
  result = {}

  @fields.each do |name, field|
    result[name] = field.value
  end

  result
end

#field_namesObject



38
39
40
# File 'lib/sso/elements/element.rb', line 38

def field_names
  @fields.keys
end

#idObject



60
61
62
# File 'lib/sso/elements/element.rb', line 60

def id
  @fields['ID'].value
end

#id=(value) ⇒ Object



64
65
66
# File 'lib/sso/elements/element.rb', line 64

def id=(value)
  @fields['ID'].value = value
end

#nameObject



68
69
70
# File 'lib/sso/elements/element.rb', line 68

def name
  @fields['Name'].value
end

#name=(value) ⇒ Object



72
73
74
# File 'lib/sso/elements/element.rb', line 72

def name=(value)
  @fields['Name'].value = value
end

#set_field(index, field) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/sso/elements/element.rb', line 10

def set_field(index, field)
  @fields[index] = field

  return if index == 'ID'
  return if index == 'Name'

  define_singleton_method(index) {@fields[index].value }
  define_singleton_method("#{index}=") { |value| @fields[index].value = value }
end

#set_multi_field(index, field) ⇒ Object



20
21
22
23
24
25
# File 'lib/sso/elements/element.rb', line 20

def set_multi_field(index, field)
  @fields[index] ||= []
  @fields[index] << field

  define_singleton_method(index) {@fields[index] }
end

#to_valueObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sso/elements/element.rb', line 76

def to_value
  value = []

  self.fields.values.each do |field|
    if field.is_a?(Array)
      field.each do |f|
        value[f.index] = f.value
      end

      next
    end

    value[field.index] = field.value
  end

  value
end