Class: Tuple
- Inherits:
-
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
|
# File 'lib/values/tuple.rb', line 7
def initialize *args
hash = ImmutableHash.new *args
@hash = ImmutableHash.new
@heading = Heading.new
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 = @heading.add(name_or_attribute)
@hash = @hash.add(name_or_attribute,value)
else
if value.is_a? Relation
@heading = @heading.add(Attribute.new(name_or_attribute,value.heading))
@hash = @hash.add(Attribute.new(name_or_attribute,value.heading),value)
else
@heading = @heading.add(Attribute.new(name_or_attribute,value.class))
@hash = @hash.add(Attribute.new(name_or_attribute,value.class),value)
end
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, &args) ⇒ Object
131
132
133
|
# File 'lib/values/tuple.rb', line 131
def method_missing name,&args
self[name.to_sym]
end
|
Instance Method Details
#==(object) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/values/tuple.rb', line 121
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
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/values/tuple.rb', line 42
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/values/tuple.rb', line 61
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
|
#count ⇒ Object
34
35
36
|
# File 'lib/values/tuple.rb', line 34
def count
@hash.count
end
|
#each(&block) ⇒ Object
55
56
57
58
59
|
# File 'lib/values/tuple.rb', line 55
def each &block
@hash.each do |key,value|
block.call key,value
end
end
|
#eql?(object) ⇒ Boolean
117
118
119
|
# File 'lib/values/tuple.rb', line 117
def eql? object
self == object
end
|
#hash ⇒ Object
80
81
82
|
# File 'lib/values/tuple.rb', line 80
def hash
@hash.hash
end
|
#heading ⇒ Object
38
39
40
|
# File 'lib/values/tuple.rb', line 38
def heading
@heading
end
|
#remove(*values) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/values/tuple.rb', line 95
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
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/values/tuple.rb', line 84
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
|