Class: RubyPx::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_px/dataset.rb

Constant Summary collapse

METADATA_RECORDS =
['TITLE','UNITS','SOURCE','CONTACT','LAST-UPDATED','CREATION-DATE']
HEADING_RECORD =
'HEADING'
STUB_RECORD =
'STUB'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_uri) ⇒ Dataset

Returns a new instance of Dataset.



11
12
13
14
15
16
17
18
19
# File 'lib/ruby_px/dataset.rb', line 11

def initialize(resource_uri)
  @metadata = {}
  @headings = []
  @stubs = []
  @values = {}
  @data = []

  parse_resource(resource_uri)
end

Instance Attribute Details

#headingsObject (readonly)

Returns the value of attribute headings.



5
6
7
# File 'lib/ruby_px/dataset.rb', line 5

def headings
  @headings
end

#stubsObject (readonly)

Returns the value of attribute stubs.



5
6
7
# File 'lib/ruby_px/dataset.rb', line 5

def stubs
  @stubs
end

Instance Method Details

#contactObject



33
34
35
# File 'lib/ruby_px/dataset.rb', line 33

def contact
  @metadata['CONTACT']
end

#creation_dateObject



41
42
43
# File 'lib/ruby_px/dataset.rb', line 41

def creation_date
  @metadata['CREATION-DATE']
end

#data(options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby_px/dataset.rb', line 53

def data(options)
  # Validate parameters
  options.each do |k,v|
    raise "Invalid value #{v} for dimension #{k}" unless dimension(k).include?(v)
  end

  # Return a single value
  # 2D array[ i*M + j]
  # 3D array[ i*(N*M) + j*M + k ]
  # 4D array[ i*(N*M*R) + j*M*R + k*R + l]
  if options.length == dimensions.length
    offset = 0

    # positions are i, j, k
    positions = (stubs + headings).map do |dimension_name|
      self.dimension(dimension_name).index(options[dimension_name])
    end

    # dimension_sizes are from all dimensions except the first one
    dimension_sizes = (stubs + headings)[1..-1].map do |dimension_name|
      self.dimension(dimension_name).length
    end

    positions.each_with_index do |p, i|
      d = dimension_sizes[i..-1].reduce(&:*)
      offset += (d ? p*d : p)
    end

    return @data[offset]

  # Return an array of options
  elsif options.length == dimensions.length - 1
    result = []

    missing_dimension = (dimensions - options.keys).first
    dimension(missing_dimension).each do |dimension_value|
      result << data(options.merge(missing_dimension => dimension_value))
    end

    return result
  else
    raise "Not implented yet, sorry"
  end
end

#dimension(name) ⇒ Object



45
46
47
# File 'lib/ruby_px/dataset.rb', line 45

def dimension(name)
  @values[name] || raise("Missing dimension #{name}")
end

#dimensionsObject



49
50
51
# File 'lib/ruby_px/dataset.rb', line 49

def dimensions
  @values.keys
end

#inspectObject



98
99
100
# File 'lib/ruby_px/dataset.rb', line 98

def inspect
  "#<#{self.class.name}:#{self.object_id}>"
end

#last_updatedObject



37
38
39
# File 'lib/ruby_px/dataset.rb', line 37

def last_updated
  @metadata['LAST-UPDATED']
end

#sourceObject



29
30
31
# File 'lib/ruby_px/dataset.rb', line 29

def source
  @metadata['SOURCE']
end

#titleObject



21
22
23
# File 'lib/ruby_px/dataset.rb', line 21

def title
  @metadata['TITLE']
end

#unitsObject



25
26
27
# File 'lib/ruby_px/dataset.rb', line 25

def units
  @metadata['UNITS']
end