Class: Alf::Tuple

Inherits:
Object
  • Object
show all
Defined in:
lib/alf/tuple.rb

Constant Summary collapse

EMPTY =
Tuple[{}].new({})
DUM =
EMPTY

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.heading_based_factoredObject


19
20
21
22
23
24
25
26
27
# File 'lib/alf/tuple.rb', line 19

def self.heading_based_factored
  heading.each do |attrname,attrtype|
    define_method(attrname) do |*args, &bl|
      return super(*args, &bl) unless args.empty? && bl.nil?
      reused_instance[attrname]
    end
  end
  self
end

Instance Method Details

#allbut(attr_list) ⇒ Object


57
58
59
# File 'lib/alf/tuple.rb', line 57

def allbut(attr_list)
  split(attr_list).last
end

#check_internal_representation!Object


29
30
31
32
33
34
35
36
37
38
# File 'lib/alf/tuple.rb', line 29

def check_internal_representation!
  error = lambda{|msg| raise TypeError, msg }
  unless reused_instance.is_a?(Hash)
    error.call("Hash expected for representation")
  end
  TypeCheck.new(self.class.heading, true).check!(reused_instance)
  self
rescue TypeCheck::Error => ex
  error.call(ex.message)
end

#extend(computation) ⇒ Object


66
67
68
69
70
71
72
# File 'lib/alf/tuple.rb', line 66

def extend(computation)
  computation   = TupleComputation.coerce(computation)
  scope         = Support::TupleScope.new(self)
  computed      = computation.evaluate(scope)
  res_heading   = heading.merge(Heading.infer(computed))
  Tuple[res_heading].new(reused_instance.merge(computed))
end

#headingObject


40
41
42
# File 'lib/alf/tuple.rb', line 40

def heading
  self.class.heading
end

#merge(other) ⇒ Object


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

def merge(other)
  res = reused_instance.merge(other.to_hash)
  Tuple[Heading.infer(res)].new(res)
end

#project(attr_list) ⇒ Object


53
54
55
# File 'lib/alf/tuple.rb', line 53

def project(attr_list)
  split(attr_list).first
end

#remap(&bl) ⇒ Object


74
75
76
77
# File 'lib/alf/tuple.rb', line 74

def remap(&bl)
  res = reused_instance.each_with_object({}){|(k,v),n| n[k] = bl[k, v]}
  Tuple[Heading.infer(res)].new(res)
end

#rename(renaming) ⇒ Object


61
62
63
64
# File 'lib/alf/tuple.rb', line 61

def rename(renaming)
  renaming = Renaming.coerce(renaming)
  self.class.rename(renaming).new(renaming.rename_tuple(reused_instance))
end

#split(attr_list) ⇒ Object


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

def split(attr_list)
  return [ EMPTY, self ] if attr_list.empty?
  left, right = {}, to_hash(true)
  (heading.to_attr_list & attr_list).each do |a|
    left[a] = right.delete(a)
  end
  self.class.split(attr_list).zip([left, right]).map{|t,v| t.new(v) }
end

#to_attr_listObject


92
93
94
# File 'lib/alf/tuple.rb', line 92

def to_attr_list
  heading.to_attr_list
end

#to_hash(dup = true) ⇒ Object


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

def to_hash(dup = true)
  dup ? reused_instance.dup : reused_instance
end

#to_json(*args, &bl) ⇒ Object


88
89
90
# File 'lib/alf/tuple.rb', line 88

def to_json(*args, &bl)
  to_hash.to_json(*args, &bl)
end

#to_ruby_literalObject Also known as: inspect, to_s


96
97
98
# File 'lib/alf/tuple.rb', line 96

def to_ruby_literal
  "Tuple(#{Support.to_ruby_literal(to_hash)})"
end