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.6.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#formulaeObject

Returns the value of attribute formulae.



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

def formulae
  @formulae
end

Instance Method Details

#&(other) ⇒ Object



71
72
73
74
75
# File 'lib/namo.rb', line 71

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

#+(other) ⇒ Object



59
60
61
62
63
# File 'lib/namo.rb', line 59

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

#-(other) ⇒ Object



65
66
67
68
69
# File 'lib/namo.rb', line 65

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

#<(other) ⇒ Object



110
111
112
113
114
# File 'lib/namo.rb', line 110

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

#<=(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#===(other) ⇒ Object



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

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

#>(other) ⇒ Object



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

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

#>=(other) ⇒ Object



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

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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/namo.rb', line 26

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 = 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



50
51
52
# File 'lib/namo.rb', line 50

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

#^(other) ⇒ Object



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

def ^(other)
  raise_unless_namo(other)
  raise_unless_matching_dimensions(other)
  self.class.new((@data - other.data) + (other.data - @data), formulae: other.formulae.merge(@formulae))
end

#coordinatesObject



18
19
20
21
22
23
24
# File 'lib/namo.rb', line 18

def coordinates
  @coordinates ||= (
    dimensions.each_with_object({}) do |dimension, hash|
      hash[dimension] = @data.map{|row| row[dimension]}.uniq
    end
  )
end

#dimensionsObject



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

def dimensions
  @dimensions ||= @data.first.keys
end

#each(&block) ⇒ Object



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

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)


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

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

#hashObject



106
107
108
# File 'lib/namo.rb', line 106

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

#to_aObject



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

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

#|(other) ⇒ Object



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

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