Class: Jtl::DataSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/jtl/data_set.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#frequencies, #frequencies_orig, #to_gruff_labels

Constructor Details

#initialize(data_set, jtl) ⇒ DataSet

Returns a new instance of DataSet.



17
18
19
20
# File 'lib/jtl/data_set.rb', line 17

def initialize(data_set, jtl)
  @data_set = data_set
  @jtl = jtl
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/jtl/data_set.rb', line 74

def method_missing(name, *args, &block)
  if (ary = self.to_a).respond_to?(name)
    ary.send(name, *args, &block)
  elsif args.empty?
    self[name.to_s, &block]
  else
    super
  end
end

Class Method Details

.create(data_set, jtl) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/jtl/data_set.rb', line 7

def self.create(data_set, jtl)
  obj = self.new(data_set, jtl)

  if block_given?
    obj.to_a.map {|i| yield(i) }
  else
    obj
  end
end

Instance Method Details

#[](label, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jtl/data_set.rb', line 42

def [](label, &block)
  new_data_set = @data_set.first[1].kind_of?(Jtl::LabeledValue) ? [] : OrderedHash.new

  @data_set.each do |mark, values|
    if values.kind_of?(Jtl::LabeledValue)
      if (label.kind_of?(Regexp) and values.label =~ label) or values.label == label.to_s
        new_data_set << [mark, values]
      end
    else
      new_data_set[mark] = values.select do |lv|
        if label.kind_of?(Regexp)
          lv.label =~ label
        else
          lv.label == label.to_s
        end
      end
    end
  end

  self.class.create(new_data_set, @jtl, &block)
end

#eachObject



64
65
66
67
68
69
70
71
72
# File 'lib/jtl/data_set.rb', line 64

def each
  @data_set.each do |mark, values|
    if values.kind_of?(Jtl::LabeledValue)
      yield(values.value)
    else
      yield(values.map {|lv| lv.value})
    end
  end
end

#to_hashObject Also known as: inspect



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jtl/data_set.rb', line 22

def to_hash
  hash = {}

  @data_set.each do |mark, values|
    if values.kind_of?(Jtl::LabeledValue)
      hash[values.label] ||= []
      hash[values.label] << [mark, values.value]
    else
      values.each do |lv|
        hash[lv.label] ||= {}
        hash[lv.label][mark] ||= []
        hash[lv.label][mark] << lv.value
      end
    end
  end

  return hash
end