Class: Microstation::TaggedElement::Set

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, element) ⇒ Set

Returns a new instance of Set.



13
14
15
16
# File 'lib/microstation/wrap.rb', line 13

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/microstation/wrap.rb', line 73

def method_missing(meth,*args,&block)
 # binding.pry
  base = meth.to_s.sub("=", "")
  if attributes.include?(base)
    if meth.match /(=)/
      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.



11
12
13
# File 'lib/microstation/wrap.rb', line 11

def element
  @element
end

Instance Method Details

#[](name) ⇒ Object



33
34
35
# File 'lib/microstation/wrap.rb', line 33

def [](name)
  find_attribute(name)
end

#attributesObject



37
38
39
# File 'lib/microstation/wrap.rb', line 37

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

#element_value(name) ⇒ Object



45
46
47
# File 'lib/microstation/wrap.rb', line 45

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

#elements=(elements) ⇒ Object



22
23
24
25
26
27
# File 'lib/microstation/wrap.rb', line 22

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

#find_attribute(name) ⇒ Object



29
30
31
# File 'lib/microstation/wrap.rb', line 29

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

#nameObject



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

def name
  @name
end

#stringify_keys(hash) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/microstation/wrap.rb', line 57

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

#to_hashObject



49
50
51
52
53
54
55
# File 'lib/microstation/wrap.rb', line 49

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



65
66
67
68
69
70
71
# File 'lib/microstation/wrap.rb', line 65

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



41
42
43
# File 'lib/microstation/wrap.rb', line 41

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