multidimensional_table 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -15
- data/lib/multidimensional_table/multidimensional_table.rb +10 -2
- data/lib/multidimensional_table/test_class.rb +16 -0
- data/lib/multidimensional_table/version.rb +1 -1
- data/spec/multidimensional_table/multidimensional_table_spec.rb +10 -10
- data/spec/multidimensional_table/test_class_spec.rb +17 -0
- metadata +4 -1
data/README.md
CHANGED
@@ -38,27 +38,31 @@ Or install it yourself as:
|
|
38
38
|
$ gem install multidimensional_table
|
39
39
|
|
40
40
|
## Usage
|
41
|
-
|
42
41
|
```
|
43
42
|
class MaterialConsumption
|
44
43
|
include MultidimensionalTable
|
45
44
|
|
46
|
-
dimensions
|
47
|
-
|
48
|
-
|
45
|
+
def dimensions
|
46
|
+
{
|
47
|
+
:year => [:year_1994, :year_1995],
|
48
|
+
:city => [:buenos_aires],
|
49
|
+
:material => [:coal, :potassium]
|
50
|
+
}
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
53
|
+
def data
|
54
|
+
table_data do
|
55
|
+
year_1994 do
|
56
|
+
buenos_aires do
|
57
|
+
coal '8t'
|
58
|
+
potassium '5t'
|
59
|
+
end
|
55
60
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
potassium '6t'
|
61
|
+
year_1995 do
|
62
|
+
buenos_aires do
|
63
|
+
coal '8t'
|
64
|
+
potassium '6t'
|
65
|
+
end
|
62
66
|
end
|
63
67
|
end
|
64
68
|
end
|
@@ -3,11 +3,17 @@ require 'multidimensional_table/non_valid_dimension'
|
|
3
3
|
|
4
4
|
module MultidimensionalTable
|
5
5
|
|
6
|
-
def
|
6
|
+
def initialize
|
7
|
+
set_dimensions(dimensions)
|
8
|
+
data
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_dimensions(map)
|
7
12
|
@dimensions = map
|
8
13
|
check_duplicates(map)
|
9
14
|
@dimensions.each do |key, value|
|
10
15
|
value.each do |possible_value|
|
16
|
+
Kernel.class_eval do
|
11
17
|
define_method possible_value do |value = nil, &block|
|
12
18
|
if value.nil? && !block.nil?
|
13
19
|
@index_level += 1
|
@@ -23,6 +29,7 @@ module MultidimensionalTable
|
|
23
29
|
@table_rules[value] = context << ["@attributes[:#{key}] == :#{possible_value}"]
|
24
30
|
end
|
25
31
|
end
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
@@ -55,7 +62,7 @@ module MultidimensionalTable
|
|
55
62
|
end
|
56
63
|
|
57
64
|
def table_result
|
58
|
-
@table_rules.each { |key, condition| return key if
|
65
|
+
@table_rules.each { |key, condition| return key if eval(condition.join(' && ')) == true }
|
59
66
|
end
|
60
67
|
|
61
68
|
def table_data
|
@@ -69,4 +76,5 @@ module MultidimensionalTable
|
|
69
76
|
def dimensions
|
70
77
|
@dimensions ||= {}
|
71
78
|
end
|
79
|
+
|
72
80
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'multidimensional_table/multidimensional_table'
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
include MultidimensionalTable
|
5
|
+
|
6
|
+
def dimensions
|
7
|
+
{ :test_material => [:test_potassium, :coal, :sugar] }
|
8
|
+
end
|
9
|
+
|
10
|
+
def data
|
11
|
+
table_data do
|
12
|
+
coal '8t'
|
13
|
+
sugar '9t'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -6,13 +6,13 @@ module MultidimensionalTable
|
|
6
6
|
context 'basic setup' do
|
7
7
|
it 'knows dimenions it has' do
|
8
8
|
subject.extend(MultidimensionalTable)
|
9
|
-
subject.
|
9
|
+
subject.set_dimensions ( { :material => [:potassium, :coal, :sugar] })
|
10
10
|
subject.dimensions[:material].should == [:potassium, :coal, :sugar]
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'creates methods for dimensions' do
|
14
14
|
subject.extend(MultidimensionalTable)
|
15
|
-
subject.
|
15
|
+
subject.set_dimensions ( { :material => [:potassium, :coal, :sugar] })
|
16
16
|
subject.respond_to?(:potassium).should == true
|
17
17
|
subject.respond_to?(:coal).should == true
|
18
18
|
subject.respond_to?(:sugar).should == true
|
@@ -20,7 +20,7 @@ module MultidimensionalTable
|
|
20
20
|
|
21
21
|
it 'should be able to find data from table' do
|
22
22
|
subject.extend(MultidimensionalTable)
|
23
|
-
subject.
|
23
|
+
subject.set_dimensions ( { :material => [:potassium, :coal, :sugar] })
|
24
24
|
subject.table_data do
|
25
25
|
subject.coal '8t'
|
26
26
|
subject.sugar '9t'
|
@@ -35,7 +35,7 @@ module MultidimensionalTable
|
|
35
35
|
context 'two dimensional' do
|
36
36
|
before(:each) do
|
37
37
|
subject.extend(MultidimensionalTable)
|
38
|
-
subject.
|
38
|
+
subject.set_dimensions ( { :material => [:potassium, :coal, :sugar], :city => [:zagreb, :zadar] })
|
39
39
|
subject.table_data do
|
40
40
|
subject.zagreb do
|
41
41
|
subject.coal '8t'
|
@@ -62,9 +62,9 @@ module MultidimensionalTable
|
|
62
62
|
context 'three dimensional' do
|
63
63
|
before(:each) do
|
64
64
|
subject.extend(MultidimensionalTable)
|
65
|
-
subject.
|
65
|
+
subject.set_dimensions ( { :material => [:potassium, :coal, :sugar],
|
66
66
|
:city => [:zagreb, :zadar],
|
67
|
-
:time_of_day => [:morning, :evening] }
|
67
|
+
:time_of_day => [:morning, :evening] } )
|
68
68
|
subject.table_data do
|
69
69
|
subject.zagreb do
|
70
70
|
subject.morning do
|
@@ -101,9 +101,9 @@ module MultidimensionalTable
|
|
101
101
|
|
102
102
|
it 'should complain if dimension does not exist' do
|
103
103
|
subject.extend(MultidimensionalTable)
|
104
|
-
subject.
|
104
|
+
subject.set_dimensions( { :material => [:potassium, :coal, :sugar],
|
105
105
|
:city => [:zagreb, :zadar],
|
106
|
-
:time_of_day => [:morning, :evening] }
|
106
|
+
:time_of_day => [:morning, :evening] })
|
107
107
|
|
108
108
|
expect do
|
109
109
|
subject.table_data do
|
@@ -120,9 +120,9 @@ module MultidimensionalTable
|
|
120
120
|
it 'should complain if same dimension name exists' do
|
121
121
|
subject.extend(MultidimensionalTable)
|
122
122
|
expect do
|
123
|
-
subject.
|
123
|
+
subject.set_dimensions ({ :material => [:potassium, :coal, :sugar],
|
124
124
|
:city => [:zagreb, :zadar],
|
125
|
-
:time_of_day => [:coal, :zagreb] }
|
125
|
+
:time_of_day => [:coal, :zagreb] })
|
126
126
|
end.to raise_error(NonValidDimension, 'Multiple definitions are not allowed : coal for dimension material and time_of_day, zagreb for dimension city and time_of_day')
|
127
127
|
end
|
128
128
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'multidimensional_table/test_class'
|
2
|
+
|
3
|
+
describe TestClass do
|
4
|
+
context 'basic setup' do
|
5
|
+
it 'knows dimensions it has' do
|
6
|
+
subject.dimensions[:test_material].should == [:test_potassium, :coal, :sugar]
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'can pinpoint right one' do
|
10
|
+
subject.update_attributes(:test_material => :sugar)
|
11
|
+
subject.table_result.should == '9t'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
end
|
17
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multidimensional_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -28,9 +28,11 @@ files:
|
|
28
28
|
- lib/multidimensional_table/multidimensional_table.rb
|
29
29
|
- lib/multidimensional_table/non_existant_dimension_attribute.rb
|
30
30
|
- lib/multidimensional_table/non_valid_dimension.rb
|
31
|
+
- lib/multidimensional_table/test_class.rb
|
31
32
|
- lib/multidimensional_table/version.rb
|
32
33
|
- multidimensional_table.gemspec
|
33
34
|
- spec/multidimensional_table/multidimensional_table_spec.rb
|
35
|
+
- spec/multidimensional_table/test_class_spec.rb
|
34
36
|
homepage: https://github.com/drKreso/multidimensional_table
|
35
37
|
licenses: []
|
36
38
|
post_install_message:
|
@@ -57,3 +59,4 @@ specification_version: 3
|
|
57
59
|
summary: Attempt to make generic API for describing multidimensional data in Ruby
|
58
60
|
test_files:
|
59
61
|
- spec/multidimensional_table/multidimensional_table_spec.rb
|
62
|
+
- spec/multidimensional_table/test_class_spec.rb
|