Class: Pest::DataSet::Hash
- Inherits:
-
Object
- Object
- Pest::DataSet::Hash
show all
- Includes:
- Pest::DataSet
- Defined in:
- lib/pest/data_set/hash.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #destroy, #except, included, #save
Constructor Details
#initialize(*args) ⇒ Hash
Returns a new instance of Hash.
20
21
22
23
|
# File 'lib/pest/data_set/hash.rb', line 20
def initialize(*args)
super *args
@hash = {}
end
|
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
18
19
20
|
# File 'lib/pest/data_set/hash.rb', line 18
def hash
@hash
end
|
#variables ⇒ Object
Returns the value of attribute variables.
18
19
20
|
# File 'lib/pest/data_set/hash.rb', line 18
def variables
@variables
end
|
Class Method Details
.from_hash(hash) ⇒ Object
11
12
13
14
15
16
|
# File 'lib/pest/data_set/hash.rb', line 11
def self.from_hash(hash)
data_set = new
data_set.variables += hash.keys
data_set.instance_variable_set(:@hash, hash)
data_set
end
|
.translators ⇒ Object
4
5
6
7
8
9
|
# File 'lib/pest/data_set/hash.rb', line 4
def self.translators
{
String => :from_file,
Symbol => :from_file
}
end
|
Instance Method Details
#+(other) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/pest/data_set/hash.rb', line 53
def +(other)
unless other.variables == variables
raise ArgumentError, "DataSets have different variables"
end
union = self.class.new
union.variables = variables
variables.each do |var|
union.hash[var.name] = hash[var.name] + other.hash[var.name]
end
union
end
|
#[](*args) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/pest/data_set/hash.rb', line 37
def [](*args)
unless args.any?
raise ArgumentError, "Indices not specified"
end
args.map do |arg|
subset = self.class.new
subset.variables = self.variables
variables.each do |var|
subset.hash[var.name] = hash[var.name][arg]
end
subset
end.inject(:+)
end
|
#data ⇒ Object
25
26
27
|
# File 'lib/pest/data_set/hash.rb', line 25
def data
hash.values
end
|
#dup ⇒ Object
84
85
86
87
88
89
|
# File 'lib/pest/data_set/hash.rb', line 84
def dup
instance = self.class.new
instance.variables = variables.dup
instance.instance_variable_set(:@hash, hash.dup)
instance
end
|
#each(&block) ⇒ Object
80
81
82
|
# File 'lib/pest/data_set/hash.rb', line 80
def each(&block)
(0..length-1).to_a.each do |i| yield variables.map {|var| hash[var][i]} end
end
|
#length ⇒ Object
33
34
35
|
# File 'lib/pest/data_set/hash.rb', line 33
def length
@hash.values.first.length
end
|
#merge(other) ⇒ Object
91
92
93
|
# File 'lib/pest/data_set/hash.rb', line 91
def merge(other)
dup.merge!(other)
end
|
#merge!(other) ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/pest/data_set/hash.rb', line 95
def merge!(other)
other = self.class.from_hash(other) if other.kind_of?(::Hash)
raise ArgumentError, "Lengths must be the same" if other.length != length
@variables += other.variables
hash.merge! other.hash
self
end
|
#pick(*args) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/pest/data_set/hash.rb', line 66
def pick(*args)
unless args.any?
raise ArgumentError, "You didn't specify any variables to pick"
end
subset = self.class.new
subset.variables += args
args.each do |var|
raise ArgumentError, "Dataset doesn't include '#{var}'" unless hash.has_key?(var)
subset.hash[var] = hash[var]
end
subset
end
|
#to_hash ⇒ Object
29
30
31
|
# File 'lib/pest/data_set/hash.rb', line 29
def to_hash
hash
end
|