1 | # rm::data_structures::item_structure
|
---|
2 | # ItemStructure module
|
---|
3 | # http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109346709572_859750_3810Report.html
|
---|
4 | # refs #54
|
---|
5 | #require 'set'
|
---|
6 | #include OpenEHR::RM::DataStructures
|
---|
7 | module OpenEhr
|
---|
8 | module RM
|
---|
9 | module DataStructures
|
---|
10 | module ItemStructure
|
---|
11 | class ItemStructure < DataStructure
|
---|
12 | end
|
---|
13 |
|
---|
14 | class ItemSingle < ItemStructure
|
---|
15 | attr_reader :item
|
---|
16 |
|
---|
17 | def initialize(args = {})
|
---|
18 | super(args)
|
---|
19 | self.item = args[:item]
|
---|
20 | end
|
---|
21 |
|
---|
22 | def as_hierarchy
|
---|
23 | return @item
|
---|
24 | end
|
---|
25 |
|
---|
26 | def item=(item)
|
---|
27 | raise ArgumentError, 'item is mandatory' if item.nil?
|
---|
28 | @item = item
|
---|
29 | end
|
---|
30 | end
|
---|
31 |
|
---|
32 | class ItemList < ItemStructure
|
---|
33 | attr_accessor :items
|
---|
34 |
|
---|
35 | def initialize(args = {})
|
---|
36 | super(args)
|
---|
37 | self.items = args[:items]
|
---|
38 | end
|
---|
39 |
|
---|
40 | def item_count
|
---|
41 | unless @items.nil?
|
---|
42 | return @items.size
|
---|
43 | else
|
---|
44 | return 0
|
---|
45 | end
|
---|
46 | end
|
---|
47 |
|
---|
48 | def names
|
---|
49 | return @items.collect{|item| item.name}
|
---|
50 | end
|
---|
51 |
|
---|
52 | def named_item(a_name)
|
---|
53 | @items.each do |item|
|
---|
54 | return item if item.name.value == a_name
|
---|
55 | end
|
---|
56 | retrun nil
|
---|
57 | end
|
---|
58 |
|
---|
59 | def ith_item(i)
|
---|
60 | raise ArgumentError, 'index invalid' if i <= 0
|
---|
61 | return @items[i - 1]
|
---|
62 | end
|
---|
63 |
|
---|
64 | def as_hierarchy
|
---|
65 | return Cluster.new(:name => @name,
|
---|
66 | :archetype_node_id => @archetype_node_id,
|
---|
67 | :items => @items)
|
---|
68 | end
|
---|
69 | end
|
---|
70 |
|
---|
71 | class ItemTable < ItemStructure
|
---|
72 | attr_accessor :rows
|
---|
73 |
|
---|
74 | def initialize(args = {})
|
---|
75 | super(args)
|
---|
76 | self.rows = args[:rows]
|
---|
77 | end
|
---|
78 |
|
---|
79 | def row_count
|
---|
80 | if @rows.nil?
|
---|
81 | return 0
|
---|
82 | else
|
---|
83 | return @rows.size
|
---|
84 | end
|
---|
85 | end
|
---|
86 |
|
---|
87 | def column_count
|
---|
88 | if @rows.nil?
|
---|
89 | return 0
|
---|
90 | else
|
---|
91 | return @rows[0].items.count
|
---|
92 | end
|
---|
93 | end
|
---|
94 |
|
---|
95 | def row_names
|
---|
96 | if @rows.nil?
|
---|
97 | return []
|
---|
98 | else
|
---|
99 | return @rows.collect{|r| r.name}
|
---|
100 | end
|
---|
101 | end
|
---|
102 |
|
---|
103 | def column_names
|
---|
104 | if @rows.nil?
|
---|
105 | return []
|
---|
106 | else
|
---|
107 | return @rows[0].items.collect{|i| i.name}
|
---|
108 | end
|
---|
109 | end
|
---|
110 |
|
---|
111 | def ith_row(i)
|
---|
112 | raise ArgumentError, 'invalid index' if i<=0 or i>@rows.size
|
---|
113 | if @rows.nil?
|
---|
114 | return []
|
---|
115 | else
|
---|
116 | return @rows[i - 1]
|
---|
117 | end
|
---|
118 | end
|
---|
119 |
|
---|
120 | def has_row_with_name?(key)
|
---|
121 | raise ArgumentError, 'invalid argument' if key.nil? or key.empty?
|
---|
122 | @rows.each do |row|
|
---|
123 | return true if row.items[0].name.value == key
|
---|
124 | end
|
---|
125 | return false
|
---|
126 | end
|
---|
127 |
|
---|
128 | def has_column_with_name?(key)
|
---|
129 | raise ArgumentError, 'invalid argument' if key.nil? or key.empty?
|
---|
130 | self.column_names.each do |name|
|
---|
131 | return true if name.value == key
|
---|
132 | end
|
---|
133 | return false
|
---|
134 | end
|
---|
135 |
|
---|
136 | def named_row(key)
|
---|
137 | raise ArgumentError, 'invalid argument' unless has_row_with_name?(key)
|
---|
138 | @rows.each do |row|
|
---|
139 | return row if row.items[0].name.value == key
|
---|
140 | end
|
---|
141 | return []
|
---|
142 | end
|
---|
143 |
|
---|
144 | def has_row_with_key?(keys)
|
---|
145 | keys.each do |key|
|
---|
146 | @rows.each do |row|
|
---|
147 | return true if row.items[0].name.value == key
|
---|
148 | end
|
---|
149 | end
|
---|
150 | return false
|
---|
151 | end
|
---|
152 |
|
---|
153 | def row_with_key(keys)
|
---|
154 | unless has_row_with_key?(keys)
|
---|
155 | raise ArgumentError, 'no row for key'
|
---|
156 | end
|
---|
157 | keys.each do |key|
|
---|
158 | @rows.each do |row|
|
---|
159 | return row if row.items[0].name.value == key
|
---|
160 | end
|
---|
161 | end
|
---|
162 | end
|
---|
163 |
|
---|
164 | def element_at_cell_ij(i,j)
|
---|
165 | return @rows[i-1].items[j-1]
|
---|
166 | end
|
---|
167 |
|
---|
168 | def element_at_named_cell(row_key, column_key)
|
---|
169 | i,j=0,0
|
---|
170 | @rows[0].items.each do |c|
|
---|
171 | break if c.name.value == column_key
|
---|
172 | i+=1
|
---|
173 | end
|
---|
174 | @rows.each do |row|
|
---|
175 | break if row.name.value == row_key
|
---|
176 | j+=1
|
---|
177 | end
|
---|
178 | return element_at_cell_ij(i,j)
|
---|
179 | end
|
---|
180 |
|
---|
181 | def as_hierarchy
|
---|
182 | return @rows[0]
|
---|
183 | end
|
---|
184 | end
|
---|
185 |
|
---|
186 | class ItemTree < ItemStructure
|
---|
187 | attr_accessor :items
|
---|
188 |
|
---|
189 | def initialize(args ={ })
|
---|
190 | super(args)
|
---|
191 | self.items = args[:items]
|
---|
192 | end
|
---|
193 | end
|
---|
194 | end # of ItemStructure
|
---|
195 | end # of DataStructures
|
---|
196 | end # of RM
|
---|
197 | end # of OpenEHR
|
---|