/
columns.rb
52 lines (42 loc) · 1.13 KB
/
columns.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Monotable
class Columns
attr_reader :columns,:columns_by_id
def initialize(from_xbd=nil)
@columns={}
@columns_by_id=[]
load_xbd(from_xbd) if from_xbd
end
def load_xbd(xbd_tag)
xbd_tag.each_tag do |tag|
col_info={}
tag.each_attribute do |k,v|
col_info[k]=v
end
self << col_info
end
end
def <<(column_properties)
column = Column.new column_properties
@columns[column]||= begin
id = @columns_by_id.length
@columns_by_id<<column
id
end
end
def inspect; xbd_tag.to_xml; end
def length;@columns.length; end
def each; @columns_by_id.each {|col| yield col} end
def each_with_index; @columns_by_id.each_with_index {|col,i| yield col,i} end
def [](key) @columns[key] || @columns_by_id[key] end
def ==(other) @columns_by_id == other.columns_by_id; end
def xbd_tag
Xbd::Tag.new("columns") do |tag|
each do |column|
tag<<Xbd::Tag.new("column") do |col_tag|
column.each {|k,v| col_tag[k]=v}
end
end
end
end
end
end