Class: Tuple

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tuple

Returns a new instance of Tuple.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/values/tuple.rb', line 7

def initialize *args
  
  # Fix so that we have the input as a hash
  hash = args.first
  unless(hash.is_a?(Hash) || hash.is_a?(ImmutableHash))  
    hash = {args[0] => args[1]}
  end
  
  if args.length == 0
    hash = {}
  end
  
  
  # Validate and put the input as we want it
  values = {}
  heading = []
  
  hash.each do |name_or_attribute,value| 
    if name_or_attribute.is_a? Attribute
      
      if value.is_a? Relation
        throw "#{value} is not the same as #{name_or_attribute.type}" if value.heading != name_or_attribute.type
      else
        throw "#{value} is not the same as #{name_or_attribute.type}" if value.class != name_or_attribute.type
      end
      
      heading << name_or_attribute
    else
      if value.is_a? Relation
        heading << Attribute.new(name_or_attribute,value.heading)
      else
        heading << Attribute.new(name_or_attribute,value.class)
      end
    end
    
    values[heading.last] = value
  end
  
  # set it
  @hash = ImmutableHash.new values
  @heading = Heading.new heading
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, &args) ⇒ Object



148
149
150
# File 'lib/values/tuple.rb', line 148

def method_missing name,&args
  self[name.to_sym]
end

Instance Method Details

#==(object) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/values/tuple.rb', line 138

def == object
  if object.equal?(self)
    true
  elsif !self.class.equal?(object.class)
    false
  else
    self.inner_hash.eql?(object.inner_hash)
  end
end

#[](attribute_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/values/tuple.rb', line 59

def [] attribute_name
  if attribute_name.is_a? String
    @hash[self.heading[attribute_name]]
  elsif attribute_name.is_a? Attribute
    @hash[attribute_name]
  elsif attribute_name.is_a? Symbol
    @hash[self.heading[attribute_name.to_s]]
  else
    throw "What should i do with this ? attribute_name=\"#{attribute_name.inspect}\""
  end
end

#add(*values) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/values/tuple.rb', line 78

def add *values
  
  if values.length == 1 and values[0].is_a? Tuple
    to_return = self
    values[0].each do |attribute,value|
      to_return = to_return.add(attribute,value)
    end
    
    to_return
  else
    if values[0].is_a? Attribute
      throw "already exists with this name" unless self.heading[values[0].name].nil?
    elsif values[0].is_a? String
      throw "already exists with this name" unless self.heading[values[0]].nil?
    end
    Tuple.new(@hash.add(*values))
  end
end

#countObject



51
52
53
# File 'lib/values/tuple.rb', line 51

def count
  @hash.count
end

#each(&block) ⇒ Object



72
73
74
75
76
# File 'lib/values/tuple.rb', line 72

def each &block
  @hash.each do |key,value|
    block.call key,value
  end
end

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/values/tuple.rb', line 134

def eql? object
  self == object
end

#hashObject



97
98
99
# File 'lib/values/tuple.rb', line 97

def hash
  @hash.hash
end

#headingObject



55
56
57
# File 'lib/values/tuple.rb', line 55

def heading
  @heading
end

#remove(*values) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/values/tuple.rb', line 112

def remove *values
  
  to_return = @hash
  
  values.each do |attribute_or_name|
    if attribute_or_name.is_a? Attribute
      to_return = to_return.delete(attribute_or_name)
    elsif attribute_or_name.is_a? Hash
      attribute_or_name.each do |key,value|
        to_return = to_return.delete(heading[key.to_s])
      end
    elsif attribute_or_name.is_a? Array
      attribute_or_name.each do |name|
        to_return = to_return.delete(heading[name.to_s])
      end
    else
      to_return = to_return.delete(heading[attribute_or_name.to_s])
    end
  end  
  Tuple.new to_return
end

#rename(from, to) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/values/tuple.rb', line 101

def rename from,to
  throw "Missing from" if self[from].nil?
  throw "to already exists" unless self[to].nil?
  
  to_return = Tuple.new()
  to_return._heading = @heading.rename(from,to)
  to_return._inner_hash = @hash.delete(self.heading[from]).add(Attribute.new(to,self.heading[from].type) ,self[from])
  
  to_return
end