Class: RubyPx::Dataset
- Inherits:
-
Object
- Object
- RubyPx::Dataset
- 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
-
#headings ⇒ Object
readonly
Returns the value of attribute headings.
-
#stubs ⇒ Object
readonly
Returns the value of attribute stubs.
Instance Method Summary collapse
- #contact ⇒ Object
- #creation_date ⇒ Object
- #data(options) ⇒ Object
- #dimension(name) ⇒ Object
- #dimensions ⇒ Object
-
#initialize(file) ⇒ Dataset
constructor
A new instance of Dataset.
- #inspect ⇒ Object
- #last_updated ⇒ Object
- #source ⇒ Object
- #title ⇒ Object
- #units ⇒ Object
Constructor Details
#initialize(file) ⇒ Dataset
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ruby_px/dataset.rb', line 9 def initialize(file) if !File.readable?(file) raise "File #{file} not readable" end = {} @headings = [] @stubs = [] @values = {} @data = [] parse_file(file) end |
Instance Attribute Details
#headings ⇒ Object (readonly)
Returns the value of attribute headings.
3 4 5 |
# File 'lib/ruby_px/dataset.rb', line 3 def headings @headings end |
#stubs ⇒ Object (readonly)
Returns the value of attribute stubs.
3 4 5 |
# File 'lib/ruby_px/dataset.rb', line 3 def stubs @stubs end |
Instance Method Details
#contact ⇒ Object
35 36 37 |
# File 'lib/ruby_px/dataset.rb', line 35 def contact ['CONTACT'] end |
#creation_date ⇒ Object
43 44 45 |
# File 'lib/ruby_px/dataset.rb', line 43 def creation_date ['CREATION-DATE'] end |
#data(options) ⇒ Object
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 97 98 |
# File 'lib/ruby_px/dataset.rb', line 55 def data() # Validate parameters .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 .length == dimensions.length offset = 0 # positions are i, j, k positions = (stubs + headings).map do |dimension_name| self.dimension(dimension_name).index([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 .length == dimensions.length - 1 result = [] missing_dimension = (dimensions - .keys).first dimension(missing_dimension).each do |dimension_value| result << data(.merge(missing_dimension => dimension_value)) end return result else raise "Not implented yet, sorry" end end |
#dimension(name) ⇒ Object
47 48 49 |
# File 'lib/ruby_px/dataset.rb', line 47 def dimension(name) @values[name] || raise("Missing dimension #{name}") end |
#dimensions ⇒ Object
51 52 53 |
# File 'lib/ruby_px/dataset.rb', line 51 def dimensions @values.keys end |
#inspect ⇒ Object
100 101 102 |
# File 'lib/ruby_px/dataset.rb', line 100 def inspect "#<#{self.class.name}:#{self.object_id}>" end |
#last_updated ⇒ Object
39 40 41 |
# File 'lib/ruby_px/dataset.rb', line 39 def last_updated ['LAST-UPDATED'] end |
#source ⇒ Object
31 32 33 |
# File 'lib/ruby_px/dataset.rb', line 31 def source ['SOURCE'] end |
#title ⇒ Object
23 24 25 |
# File 'lib/ruby_px/dataset.rb', line 23 def title ['TITLE'] end |
#units ⇒ Object
27 28 29 |
# File 'lib/ruby_px/dataset.rb', line 27 def units ['UNITS'] end |