Class: Namo
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/Namo/Row.rb,
lib/namo.rb,
lib/Namo/VERSION.rb,
lib/Namo/Enumerable.rb,
lib/Namo/NegatedDimension.rb
Overview
Namo/NegatedDimension.rb Namo::NegatedDimension
Defined Under Namespace
Modules: Enumerable
Classes: NegatedDimension, Row
Constant Summary
collapse
- VERSION =
'0.12.0'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Enumerable
#drop, #drop_while, #each, #first, #last, #partition, #reject, #select, #sort_by, #take, #take_while, #uniq
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13
14
15
|
# File 'lib/namo.rb', line 13
def data
@data
end
|
Returns the value of attribute formulae.
14
15
16
|
# File 'lib/namo.rb', line 14
def formulae
@formulae
end
|
#name ⇒ Object
Returns the value of attribute name.
15
16
17
|
# File 'lib/namo.rb', line 15
def name
@name
end
|
Instance Method Details
#&(other) ⇒ Object
93
94
95
96
97
|
# File 'lib/namo.rb', line 93
def &(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
self.class.new(@data & other.data, formulae: @formulae.dup)
end
|
#*(other) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/namo.rb', line 111
def *(other)
raise_unless_namo(other)
raise_unless_shared_data_dimensions(other)
shared = data_dimensions & other.data_dimensions
combined_data = []
@data.each do |left_row|
other.data.each do |right_row|
if shared.all?{|dim| left_row[dim] == right_row[dim]}
combined_data << left_row.merge(right_row)
end
end
end
self.class.new(combined_data, formulae: other.formulae.merge(@formulae))
end
|
#**(other) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/namo.rb', line 126
def **(other)
raise_unless_namo(other)
raise_unless_disjoint_data_dimensions(other)
combined_data = []
@data.each do |left_row|
other.data.each do |right_row|
combined_data << left_row.merge(right_row)
end
end
self.class.new(combined_data, formulae: other.formulae.merge(@formulae))
end
|
#+(other) ⇒ Object
81
82
83
84
85
|
# File 'lib/namo.rb', line 81
def +(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
self.class.new(@data + other.data, formulae: other.formulae.merge(@formulae))
end
|
#-(other) ⇒ Object
87
88
89
90
91
|
# File 'lib/namo.rb', line 87
def -(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
self.class.new(@data - other.data, formulae: @formulae.dup)
end
|
#/(other) ⇒ Object
138
139
140
141
142
143
144
145
|
# File 'lib/namo.rb', line 138
def /(other)
raise_unless_namo(other)
kept = data_dimensions - other.data_dimensions
projected = @data.map do |row|
kept.each_with_object({}){|dim, hash| hash[dim] = row[dim]}
end
self.class.new(projected.uniq, formulae: @formulae.dup)
end
|
#<(other) ⇒ Object
168
169
170
171
172
|
# File 'lib/namo.rb', line 168
def <(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
proper_subset_of_rows?(other)
end
|
#<=(other) ⇒ Object
174
175
176
177
178
|
# File 'lib/namo.rb', line 174
def <=(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
subset_of_rows?(other)
end
|
#==(other) ⇒ Object
147
148
149
150
|
# File 'lib/namo.rb', line 147
def ==(other)
return false unless other.is_a?(Namo)
canonical_data == other.canonical_data
end
|
#===(other) ⇒ Object
152
153
154
155
156
|
# File 'lib/namo.rb', line 152
def ===(other)
return false unless other.is_a?(Namo)
dimensions.sort == other.dimensions.sort &&
@formulae.keys.sort == other.formulae.keys.sort
end
|
#>(other) ⇒ Object
180
181
182
183
184
|
# File 'lib/namo.rb', line 180
def >(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
other.proper_subset_of_rows?(self)
end
|
#>=(other) ⇒ Object
186
187
188
189
190
|
# File 'lib/namo.rb', line 186
def >=(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
other.subset_of_rows?(self)
end
|
#[](*names, **selections) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/namo.rb', line 53
def [](*names, **selections)
rows = selections.any? ? select{|row| row.match?(selections)} : entries
negated, positive = names.partition{|n| n.is_a?(NegatedDimension)}
if negated.any? && positive.any?
raise ArgumentError, "cannot mix projection and contraction in a single call"
end
projected = (
if negated.any?
excluded = negated.map(&:name)
kept = data_dimensions - excluded
rows.map do |row|
kept.each_with_object({}){|name, hash| hash[name] = row[name]}
end
elsif positive.any?
rows.map do |row|
positive.each_with_object({}){|name, hash| hash[name] = row[name]}
end
else
rows.map(&:to_h)
end
)
self.class.new(projected, formulae: @formulae.dup)
end
|
#[]=(name, proc) ⇒ Object
77
78
79
|
# File 'lib/namo.rb', line 77
def []=(name, proc)
@formulae[name] = proc
end
|
#^(other) ⇒ Object
105
106
107
108
109
|
# File 'lib/namo.rb', line 105
def ^(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
self.class.new((@data - other.data) + (other.data - @data), formulae: other.formulae.merge(@formulae))
end
|
#coordinates(*dims) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/namo.rb', line 39
def coordinates(*dims)
if dims.empty?
values.transform_values(&:uniq)
elsif dims.length == 1
values(dims.first).uniq
else
dims.each_with_object({}){|dim, hash| hash[dim] = values(dim).uniq}
end
end
|
#data_dimensions ⇒ Object
21
22
23
|
# File 'lib/namo.rb', line 21
def data_dimensions
@data.first&.keys || []
end
|
#derived_dimensions ⇒ Object
25
26
27
|
# File 'lib/namo.rb', line 25
def derived_dimensions
@formulae.keys
end
|
#dimensions ⇒ Object
17
18
19
|
# File 'lib/namo.rb', line 17
def dimensions
data_dimensions + derived_dimensions
end
|
#eql?(other) ⇒ Boolean
158
159
160
161
162
|
# File 'lib/namo.rb', line 158
def eql?(other)
self.class == other.class &&
canonical_data == other.canonical_data &&
@formulae.keys.sort == other.formulae.keys.sort
end
|
#hash ⇒ Object
164
165
166
|
# File 'lib/namo.rb', line 164
def hash
[self.class, canonical_data, @formulae.keys.sort].hash
end
|
#to_a ⇒ Object
192
193
194
195
196
197
198
|
# File 'lib/namo.rb', line 192
def to_a
@data.map do |row|
row.keys.each_with_object({}) do |key, hash|
hash[key] = row[key]
end
end
end
|
#to_h ⇒ Object
49
50
51
|
# File 'lib/namo.rb', line 49
def to_h
values
end
|
#values(*dims) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/namo.rb', line 29
def values(*dims)
if dims.empty?
dimensions.each_with_object({}){|dim, hash| hash[dim] = values_for(dim)}
elsif dims.length == 1
values_for(dims.first)
else
dims.each_with_object({}){|dim, hash| hash[dim] = values_for(dim)}
end
end
|
#|(other) ⇒ Object
99
100
101
102
103
|
# File 'lib/namo.rb', line 99
def |(other)
raise_unless_namo(other)
raise_unless_matching_data_dimensions(other)
self.class.new((@data | other.data), formulae: other.formulae.merge(@formulae))
end
|