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, id, elements, model) ⇒ Instance



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

def initialize(ts,id, elements, model)
  @tagset = ts
  @microstation_id = id
  @elements = elements
  @microstation_model = model
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/microstation/ts/instance.rb', line 130

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.



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

def elements
  @elements
end

#microstation_idObject (readonly)

Returns the value of attribute microstation_id.



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

def microstation_id
  @microstation_id
end

#microstation_modelObject (readonly)

Returns the value of attribute microstation_model.



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

def microstation_model
  @microstation_model
end

#tagsetObject (readonly)

Returns the value of attribute tagset.



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

def tagset
  @tagset
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  find_attribute(name)
end

#[]=(name, value) ⇒ Object



53
54
55
# File 'lib/microstation/ts/instance.rb', line 53

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

#attribute_hashObject



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

def attribute_hash
  result = {"microstation_id" => microstation_id}
  each_pair do |name, value|
    result[name] = value
  end
  result
end

#attributesObject



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

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

#eachObject



103
104
105
106
107
# File 'lib/microstation/ts/instance.rb', line 103

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

#each_pairObject



109
110
111
112
113
# File 'lib/microstation/ts/instance.rb', line 109

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

#element_value(name) ⇒ Object



57
58
59
# File 'lib/microstation/ts/instance.rb', line 57

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

#find(&block) ⇒ Object



99
100
101
# File 'lib/microstation/ts/instance.rb', line 99

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

#find_attribute(name) ⇒ Object



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

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

#map_vObject



115
116
117
118
119
120
# File 'lib/microstation/ts/instance.rb', line 115

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

#nameObject



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

def name
  tagset.name
end

#pair(el) ⇒ Object



85
86
87
# File 'lib/microstation/ts/instance.rb', line 85

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

#selectObject



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

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_hObject



77
78
79
80
81
82
# File 'lib/microstation/ts/instance.rb', line 77

def to_h
  result = {'tagset_name'=> tagset.name, 'microstation_id' => microstation_id,
            'microstation_model' => microstation_model, }
  result['attributes'] = attribute_hash
  result
end

#to_hashObject



61
62
63
64
65
66
67
# File 'lib/microstation/ts/instance.rb', line 61

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

#to_sObject



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

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

#update(value_hash) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/microstation/ts/instance.rb', line 122

def update(value_hash)
  value_hash = value_hash.transform{|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



49
50
51
# File 'lib/microstation/ts/instance.rb', line 49

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