Class: Pest::DataSet::Hash

Inherits:
Object
  • Object
show all
Includes:
Pest::DataSet
Defined in:
lib/pest/data_set/hash.rb

Defined Under Namespace

Classes: VectorEnumerable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pest::DataSet

#destroy, included, #variable_array

Constructor Details

#initialize(hash) ⇒ Hash

Returns a new instance of Hash.



26
27
28
29
30
31
32
# File 'lib/pest/data_set/hash.rb', line 26

def initialize(hash)
  @hash = hash
  @variables = {}
  hash.keys().each do |name|
    @variables[name] = Pest::Variable.new(:name => name)
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



24
25
26
# File 'lib/pest/data_set/hash.rb', line 24

def hash
  @hash
end

#variablesObject (readonly)

Returns the value of attribute variables.



24
25
26
# File 'lib/pest/data_set/hash.rb', line 24

def variables
  @variables
end

Class Method Details

.from_file(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pest/data_set/hash.rb', line 12

def self.from_file(file)
  file = File.open(file.to_s, 'r') if file.kind_of?(String)

  object = Marshal.restore(file)

  if object.kind_of?(::Hash)
    self.new(object)
  else
    raise "File does not seem to contain valid data"
  end
end

.translatorsObject



4
5
6
7
8
9
10
# File 'lib/pest/data_set/hash.rb', line 4

def self.translators
  {
    File    => :from_file,
    String  => :from_file,
    Symbol  => :from_file
  }
end

Instance Method Details

#data_vectors(variables = nil) ⇒ Object



38
39
40
# File 'lib/pest/data_set/hash.rb', line 38

def data_vectors(variables=nil)
  VectorEnumerable.new(self,variables)
end

#lengthObject



42
43
44
# File 'lib/pest/data_set/hash.rb', line 42

def length
  @hash.values.first.length
end

#save(file = nil) ⇒ Object



46
47
48
49
50
# File 'lib/pest/data_set/hash.rb', line 46

def save(file=nil)
  file ||= Tempfile.new('pest_hash_dataset')
  file = File.open(file, 'w') if file.kind_of?(String)
  Marshal.dump(@hash, file)
end

#to_hashObject



34
35
36
# File 'lib/pest/data_set/hash.rb', line 34

def to_hash
  @hash
end