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

Returns a new instance of Instance.



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

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



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/microstation/ts/instance.rb', line 122

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

#elementsObject (readonly)

Returns the value of attribute elements.



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

def elements
  @elements
end

#microstation_idObject (readonly)

Returns the value of attribute microstation_id.



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

def microstation_id
  @microstation_id
end

#microstation_modelObject (readonly)

Returns the value of attribute microstation_model.



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

def microstation_model
  @microstation_model
end

#tagsetObject (readonly)

Returns the value of attribute tagset.



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

def tagset
  @tagset
end

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  find_attribute(name)
end

#[]=(name, value) ⇒ Object



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

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

#attribute_hashObject



64
65
66
67
68
69
70
# File 'lib/microstation/ts/instance.rb', line 64

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

#attributesObject



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

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

#each(&block) ⇒ Object



97
98
99
# File 'lib/microstation/ts/instance.rb', line 97

def each(&block)
  @elements.each(&block)
end

#each_pairObject



101
102
103
104
105
# File 'lib/microstation/ts/instance.rb', line 101

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

#element_value(name) ⇒ Object



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

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

#find(&block) ⇒ Object



93
94
95
# File 'lib/microstation/ts/instance.rb', line 93

def find(&block)
  find(&block)
end

#find_attribute(name) ⇒ Object



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

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

#map_vObject



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

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

#nameObject



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

def name
  tagset.name
end

#pair(el) ⇒ Object



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

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

#selectObject



83
84
85
86
87
88
89
90
91
# File 'lib/microstation/ts/instance.rb', line 83

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



72
73
74
75
76
77
# File 'lib/microstation/ts/instance.rb', line 72

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

#to_hashObject



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

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

#to_sObject



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

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

#update(value_hash) ⇒ Object



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

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



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

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