Class: Namo

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

Overview

Namo/NegatedDimension.rb Namo::NegatedDimension

Defined Under Namespace

Classes: NegatedDimension, Row

Constant Summary collapse

VERSION =
'0.11.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/namo.rb', line 12

def data
  @data
end

#formulaeObject

Returns the value of attribute formulae.



13
14
15
# File 'lib/namo.rb', line 13

def formulae
  @formulae
end

Instance Method Details

#&(other) ⇒ Object



155
156
157
158
159
# File 'lib/namo.rb', line 155

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

#*(other) ⇒ Object



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

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



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

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



143
144
145
146
147
# File 'lib/namo.rb', line 143

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



149
150
151
152
153
# File 'lib/namo.rb', line 149

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

#/(other) ⇒ Object



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

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



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

def <(other)
  raise_unless_namo(other)
  raise_unless_matching_data_dimensions(other)
  proper_subset_of_rows?(other)
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)
  subset_of_rows?(other)
end

#==(other) ⇒ Object



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

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

#===(other) ⇒ Object



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

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

#>(other) ⇒ Object



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

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

#>=(other) ⇒ Object



248
249
250
251
252
# File 'lib/namo.rb', line 248

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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/namo.rb', line 51

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



75
76
77
# File 'lib/namo.rb', line 75

def []=(name, proc)
  @formulae[name] = proc
end

#^(other) ⇒ Object



167
168
169
170
171
# File 'lib/namo.rb', line 167

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



37
38
39
40
41
42
43
44
45
# File 'lib/namo.rb', line 37

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



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

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

#derived_dimensionsObject



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

def derived_dimensions
  @formulae.keys
end

#dimensionsObject



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

def dimensions
  data_dimensions + derived_dimensions
end

#drop(n) ⇒ Object



118
119
120
# File 'lib/namo.rb', line 118

def drop(n)
  self.class.new(@data.drop(n), formulae: @formulae.dup)
end

#drop_while(&block) ⇒ Object



126
127
128
# File 'lib/namo.rb', line 126

def drop_while(&block)
  self.class.new(@data.drop_while{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#each(&block) ⇒ Object



79
80
81
82
# File 'lib/namo.rb', line 79

def each(&block)
  return enum_for(:each) unless block_given?
  @data.each{|row_data| block.call(Row.new(row_data, @formulae))}
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
223
224
# File 'lib/namo.rb', line 220

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

#first(n = nil) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/namo.rb', line 98

def first(n = nil)
  if n
    self.class.new(@data.first(n), formulae: @formulae.dup)
  else
    @data.first ? Row.new(@data.first, @formulae) : nil
  end
end

#hashObject



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

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

#last(n = nil) ⇒ Object



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

def last(n = nil)
  if n
    self.class.new(@data.last(n), formulae: @formulae.dup)
  else
    @data.last ? Row.new(@data.last, @formulae) : nil
  end
end

#partition(&block) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/namo.rb', line 135

def partition(&block)
  matches, non_matches = @data.partition{|row| block.call(Row.new(row, @formulae))}
  [
    self.class.new(matches, formulae: @formulae.dup),
    self.class.new(non_matches, formulae: @formulae.dup),
  ]
end

#reject(&block) ⇒ Object



90
91
92
# File 'lib/namo.rb', line 90

def reject(&block)
  self.class.new(@data.reject{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#select(&block) ⇒ Object Also known as: filter, find_all



84
85
86
# File 'lib/namo.rb', line 84

def select(&block)
  self.class.new(@data.select{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#sort_by(&block) ⇒ Object



94
95
96
# File 'lib/namo.rb', line 94

def sort_by(&block)
  self.class.new(@data.sort_by{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#take(n) ⇒ Object



114
115
116
# File 'lib/namo.rb', line 114

def take(n)
  self.class.new(@data.take(n), formulae: @formulae.dup)
end

#take_while(&block) ⇒ Object



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

def take_while(&block)
  self.class.new(@data.take_while{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#to_aObject



254
255
256
257
258
259
260
# File 'lib/namo.rb', line 254

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

#to_hObject



47
48
49
# File 'lib/namo.rb', line 47

def to_h
  values
end

#uniq(&block) ⇒ Object



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

def uniq(&block)
  rows = block ? @data.uniq{|row| block.call(Row.new(row, @formulae))} : @data.uniq
  self.class.new(rows, formulae: @formulae.dup)
end

#values(*dims) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/namo.rb', line 27

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



161
162
163
164
165
# File 'lib/namo.rb', line 161

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