Class: Microstation::TaggedElement::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/microstation/tagged_element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, element) ⇒ Set

Returns a new instance of Set.



6
7
8
9
# File 'lib/microstation/tagged_element.rb', line 6

def initialize(name, element)
  @name = name
  @element = element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/microstation/tagged_element.rb', line 62

def method_missing(meth, *args, &block)
  # binding.pry
  base = meth.to_s.sub("=", "")
  if attributes.include?(base)
    if /(=)/.match?(meth)
      update_element(base, *args)
    else
      element_value(base.to_s)
    end
  else
    super(meth, *args, &block)
  end
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



4
5
6
# File 'lib/microstation/tagged_element.rb', line 4

def element
  @element
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/microstation/tagged_element.rb', line 4

def name
  @name
end

Instance Method Details

#[](name) ⇒ Object



22
23
24
# File 'lib/microstation/tagged_element.rb', line 22

def [](name)
  find_attribute(name)
end

#attributesObject



26
27
28
# File 'lib/microstation/tagged_element.rb', line 26

def attributes
  @elements.map { |e| e.name }
end

#element_value(name) ⇒ Object



34
35
36
# File 'lib/microstation/tagged_element.rb', line 34

def element_value(name)
  find_attribute(name).value
end

#elements=(elements) ⇒ Object



11
12
13
14
15
16
# File 'lib/microstation/tagged_element.rb', line 11

def elements=(elements)
  elements.each do |ele|
    ele.base_element = @element
  end
  @elements = elements
end

#find_attribute(name) ⇒ Object



18
19
20
# File 'lib/microstation/tagged_element.rb', line 18

def find_attribute(name)
  @elements.find { |a| a.name == name.to_s }
end

#stringify_keys(hash) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/microstation/tagged_element.rb', line 46

def stringify_keys(hash)
  result = {}
  hash.each do |key, value|
    result[key.to_s] = value
  end
  result
end

#to_hashObject



38
39
40
41
42
43
44
# File 'lib/microstation/tagged_element.rb', line 38

def to_hash
  result = {}
  @elements.each do |ele|
    result[ele.name] = ele.value unless ele.value == "" || ele.value.nil?
  end
  result
end

#update(value_hash) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/microstation/tagged_element.rb', line 54

def update(value_hash)
  value_hash = stringify_keys(value_hash)
  valid_atts = attributes & value_hash.keys
  valid_atts.each do |att|
    update_element(att, value_hash[att])
  end
end

#update_element(name, value) ⇒ Object



30
31
32
# File 'lib/microstation/tagged_element.rb', line 30

def update_element(name, value)
  find_attribute(name)._update(value)
end