Class: Namo

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Namo/Row.rb,
lib/namo.rb,
lib/Namo/VERSION.rb,
lib/Namo/Formulae.rb,
lib/Namo/Formulary.rb,
lib/Namo/Collection.rb,
lib/Namo/Enumerable.rb,
lib/Namo/NegatedDimension.rb

Overview

Namo/NegatedDimension.rb Namo::NegatedDimension

Direct Known Subclasses

Collection

Defined Under Namespace

Modules: Enumerable, Formulary Classes: Collection, Formulae, NegatedDimension, Row

Constant Summary collapse

VERSION =
'0.29.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



16
17
18
# File 'lib/namo.rb', line 16

def data
  @data
end

#formulaeObject

Returns the value of attribute formulae.



17
18
19
# File 'lib/namo.rb', line 17

def formulae
  @formulae
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/namo.rb', line 18

def name
  @name
end

Instance Method Details

#&(other) ⇒ Object



134
135
136
137
138
# File 'lib/namo.rb', line 134

def &(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  self.class.new(@data & other.data, formulae: @formulae.dup)
end

#*(other, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/namo.rb', line 152

def *(other, &block)
  raise_unless_namo(other)
  raise_unless_shared_data_dimensions(other)
  raise_unless_data_formula_exclusivity(other)
  shared = data_dimensions & other.data_dimensions
  combined_data = []
  @data.each do |left_row|
    matched = other.data.select{|right_row| shared.all?{|dim| left_row[dim] == right_row[dim]}}
    if block
      candidates = other.class.new(matched, formulae: other.formulae.dup)
      chosen = block.call(Row.new(left_row, @formulae, self), candidates)
      chosen.data.each{|right_row| combined_data << left_row.merge(right_row)}
    else
      matched.each{|right_row| combined_data << left_row.merge(right_row)}
    end
  end
  self.class.new(combined_data, formulae: other.formulae.merge(@formulae))
end

#**(other, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/namo.rb', line 171

def **(other, &block)
  raise_unless_namo(other)
  raise_unless_disjoint_data_dimensions(other)
  raise_unless_data_formula_exclusivity(other)
  combined_data = []
  @data.each do |left_row|
    if block
      candidates = other.class.new(other.data, formulae: other.formulae.dup)
      chosen = block.call(Row.new(left_row, @formulae, self), candidates)
      chosen.data.each{|right_row| combined_data << left_row.merge(right_row)}
    else
      other.data.each{|right_row| combined_data << left_row.merge(right_row)}
    end
  end
  self.class.new(combined_data, formulae: other.formulae.merge(@formulae))
end

#+(other) ⇒ Object



122
123
124
125
126
# File 'lib/namo.rb', line 122

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



128
129
130
131
132
# File 'lib/namo.rb', line 128

def -(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  self.class.new(@data - other.data, formulae: @formulae.dup)
end

#/(other) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/namo.rb', line 188

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



218
219
220
221
222
# File 'lib/namo.rb', line 218

def <(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  proper_subset_of_rows?(other)
end

#<<(constituent) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/namo.rb', line 113

def <<(constituent)
  case constituent
  when Module then attach(constituent)
  when Row then add_row(constituent.to_h)
  when Hash then add_row(constituent)
  else raise TypeError, "can't append #{constituent.class} to a Namo; expected a Module (formulary), a Hash, or a Row"
  end
end

#<=(other) ⇒ Object



224
225
226
227
228
# File 'lib/namo.rb', line 224

def <=(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  subset_of_rows?(other)
end

#==(other) ⇒ Object



197
198
199
200
# File 'lib/namo.rb', line 197

def ==(other)
  return false unless other.is_a?(Namo)
  row_multiset == other.row_multiset
end

#===(other) ⇒ Object



202
203
204
205
206
# File 'lib/namo.rb', line 202

def ===(other)
  return false unless other.is_a?(Namo)
  dimensions.sort == other.dimensions.sort &&
    @formulae.keys.sort == other.formulae.keys.sort
end

#>(other) ⇒ Object



230
231
232
233
234
# File 'lib/namo.rb', line 230

def >(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  other.proper_subset_of_rows?(self)
end

#>=(other) ⇒ Object



236
237
238
239
240
# File 'lib/namo.rb', line 236

def >=(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  other.subset_of_rows?(self)
end

#[](*names, **selections) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/namo.rb', line 58

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
  )
  carried = positive.any? ? @formulae.reject{|name, _| positive.include?(name)} : @formulae.dup
  self.class.new(projected, formulae: carried)
end

#[]=(name, value) ⇒ Object



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

def []=(name, value)
  if value.respond_to?(:call)
    @data.each{|row| row.delete(name)} if @data.first&.key?(name)
    @formulae[name] = value
  else
    @formulae.delete(name)
    @data.each{|row| row[name] = value}
  end
end

#^(other) ⇒ Object



146
147
148
149
150
# File 'lib/namo.rb', line 146

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

#attach(modul) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/namo.rb', line 93

def attach(modul)
  collisions = attach_collisions(modul)
  unless collisions.empty?
    raise ArgumentError, "formulary methods collide with data dimensions: #{collisions.inspect}"
  end
  @formulae.attach(modul)
  self
end

#attach!(modul) ⇒ Object



102
103
104
105
106
# File 'lib/namo.rb', line 102

def attach!(modul)
  attach_collisions(modul).each{|name| @data.each{|row| row.delete(name)}}
  @formulae.attach(modul)
  self
end

#coordinates(*dims) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/namo.rb', line 44

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_dimensionsObject



24
25
26
# File 'lib/namo.rb', line 24

def data_dimensions
  @data.first&.keys || []
end

#derived_dimensionsObject



28
29
30
# File 'lib/namo.rb', line 28

def derived_dimensions
  @formulae.keys
end

#detach(constituent) ⇒ Object



108
109
110
111
# File 'lib/namo.rb', line 108

def detach(constituent)
  @formulae.detach(constituent)
  self
end

#dimensionsObject



20
21
22
# File 'lib/namo.rb', line 20

def dimensions
  data_dimensions + derived_dimensions
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/namo.rb', line 208

def eql?(other)
  self.class == other.class &&
    row_multiset == other.row_multiset &&
    @formulae.keys.sort == other.formulae.keys.sort
end

#hashObject



214
215
216
# File 'lib/namo.rb', line 214

def hash
  [self.class, row_multiset, @formulae.keys.sort].hash
end

#to_aObject



242
243
244
245
246
247
248
# File 'lib/namo.rb', line 242

def to_a
  @data.map do |row|
    row.keys.each_with_object({}) do |key, hash|
      hash[key] = row[key]
    end
  end
end

#to_hObject



54
55
56
# File 'lib/namo.rb', line 54

def to_h
  values
end

#values(*dims) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/namo.rb', line 32

def values(*dims)
  materialising do
    if dims.empty?
      materialisable_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
end

#|(other) ⇒ Object



140
141
142
143
144
# File 'lib/namo.rb', line 140

def |(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  self.class.new((@data | other.data), formulae: other.formulae.merge(@formulae))
end