Class: Microstation::TS::Instance

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/microstation/ts/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ts, elements) ⇒ Instance

Returns a new instance of Instance.



10
11
12
13
# File 'lib/microstation/ts/instance.rb', line 10

def initialize(ts, elements)
  @tagset = ts
  @elements = elements
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/microstation/ts/instance.rb', line 96

def method_missing(meth,*args,&block)
  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

#elementsObject (readonly)

Returns the value of attribute elements.



8
9
10
# File 'lib/microstation/ts/instance.rb', line 8

def elements
  @elements
end

#tagsetObject (readonly)

Returns the value of attribute tagset.



8
9
10
# File 'lib/microstation/ts/instance.rb', line 8

def tagset
  @tagset
end

Instance Method Details

#[](name) ⇒ Object



23
24
25
# File 'lib/microstation/ts/instance.rb', line 23

def [](name)
  find_attribute(name)
end

#[]=(name, value) ⇒ Object



35
36
37
# File 'lib/microstation/ts/instance.rb', line 35

def []=(name,value)
  update_element(name,value)
end

#attributesObject



27
28
29
# File 'lib/microstation/ts/instance.rb', line 27

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

#eachObject



69
70
71
72
73
# File 'lib/microstation/ts/instance.rb', line 69

def each
  @elements.each do |el|
    yield el
  end
end

#each_pairObject



75
76
77
78
79
# File 'lib/microstation/ts/instance.rb', line 75

def each_pair
  @elements.each do |el|
    yield el.name, el.value
  end
end

#element_value(name) ⇒ Object



39
40
41
# File 'lib/microstation/ts/instance.rb', line 39

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

#find(&block) ⇒ Object



65
66
67
# File 'lib/microstation/ts/instance.rb', line 65

def find(&block)
  select(&block).first
end

#find_attribute(name) ⇒ Object



19
20
21
# File 'lib/microstation/ts/instance.rb', line 19

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

#map_vObject



81
82
83
84
85
86
# File 'lib/microstation/ts/instance.rb', line 81

def map_v
  each_pair do |k,v|
    new_v = yield v
    update_element(k,new_v)
  end
end

#pair(el) ⇒ Object



51
52
53
# File 'lib/microstation/ts/instance.rb', line 51

def pair(el)
  [el.name, el.value]
end

#selectObject



55
56
57
58
59
60
61
62
63
# File 'lib/microstation/ts/instance.rb', line 55

def select
  result = []
  each do |el|
    k,v = pair(el)
    save = yield k,v
    result << find_attribute(k) if save
  end
  self.class.new(tagset,result)
end

#to_hashObject



43
44
45
46
47
48
49
# File 'lib/microstation/ts/instance.rb', line 43

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

#to_sObject



15
16
17
# File 'lib/microstation/ts/instance.rb', line 15

def to_s
  "TS:Instance #{tagset.name}"
end

#update(value_hash) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/microstation/ts/instance.rb', line 88

def update(value_hash)
  value_hash = value_hash.map_kv{|k,v| [k.to_s,v.to_s] }
  valid_atts = attributes & value_hash.keys
  valid_atts.each do |att|
    update_element(att,value_hash[att])
  end
end

#update_element(name, value) ⇒ Object



31
32
33
# File 'lib/microstation/ts/instance.rb', line 31

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