md_data 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,21 +25,47 @@ Can be written down as:
25
25
  class MaterialConsumption
26
26
  include MdData
27
27
 
28
- table_data do
29
- context "year == 1994 && city == :buenos_aires" do
30
- add "8t", "meterial == :coal"
31
- add "5t", "meterial == :potassium"
32
- end
33
-
34
- context "year == 1995 && city == :buenos_aires" do
35
- add "8t", "meterial == :coal"
36
- add "5t", "meterial == :potassium"
37
- end
38
- end
28
+ dimension :year, [:year_1994, :year_1995]
29
+ dimension :city, [:buenos_aires, :zagreb]
30
+ dimension :material, [:coal, :potassium]
31
+
32
+ table_data do
33
+ context 'year_1994 && buenos_aires' do
34
+ add '8t', 'coal'
35
+ add '5t', 'potassium'
36
+ end
37
+ context 'year_1995 && buenos_aires' do
38
+ add '8t', 'coal'
39
+ add '5t', 'potassium'
40
+ end
41
+ end
39
42
  end
40
43
 
41
44
  MaterialConsumption.select(:year => 1994, :city => :buenos_aires, :material => :coal) #=> '8t'
42
45
  ```
46
+ ##Usage
47
+ By defining dimensions you get helper methods that can be used in 'context' and 'add' conditionals.
48
+
49
+ ```
50
+ context 'year_1994 && buenos_aires' do
51
+ add '8t', 'coal'
52
+ end
53
+
54
+ ```
55
+
56
+ eqauls
57
+
58
+ ```
59
+ context 'year == :year_1994 && city == :buenos_aires' do
60
+ add '8t', 'material == :coal'
61
+ end
62
+ ```
63
+
64
+ Important thing here is that this is still just Ruby in quotes. You can add any conditionals that you need
65
+ while you still have basic scenario covered and simplified.
66
+
67
+ ##Usage
68
+ For any type of ruled based queryingr, when you want to pull out specific data based on attributes and rules.
43
69
 
44
70
  ## Installation
45
71
 
@@ -59,6 +85,7 @@ Or install it yourself as:
59
85
 
60
86
  * No nested context allowed(yet)
61
87
  * Tests for misformed data definition
88
+ * Support for same dimension instance value and resolving of ambiguity
62
89
 
63
90
  ## Contributing
64
91
 
@@ -3,7 +3,8 @@ module MdData
3
3
  base.extend MdDataClassMethods
4
4
  end
5
5
 
6
- module MdDataClassMethods
6
+ module MdDataClassMethods
7
+
7
8
  def table_data(&block)
8
9
  @table_data_block = block
9
10
  end
@@ -13,30 +14,22 @@ module MdData
13
14
  @current_context = nil
14
15
  load_rules
15
16
  container = self.new
16
- attributes.each do |key,value|
17
- self.send(:define_method, key) do
18
- value
19
- end
20
- end
21
- self.send(:define_method, :select_from_rules) do |rules|
22
- result = nil
23
- rules.each do |rule|
24
- begin
25
- rule_is_satisfied = eval(rule[1])
26
- if rule_is_satisfied
27
- result = rule[0]
28
- break
29
- end
30
- rescue
31
- #nasty, allows me not to define dimensions for now TODO: fix
32
- end
33
- end
34
- result
35
- end
17
+ define_helpers_methods(attributes)
18
+ define_dimension_values_methods
19
+ define_select_from_rules
36
20
  container.select_from_rules(@rules)
37
21
  end
38
22
 
39
- private
23
+ def dimension(name, allowed_values)
24
+ dimensions[name] = allowed_values
25
+ end
26
+
27
+ def dimensions
28
+ @dimensions ||= {}
29
+ end
30
+
31
+
32
+ private
40
33
  def load_rules
41
34
  @table_data_block.call
42
35
  end
@@ -50,6 +43,40 @@ module MdData
50
43
  @current_context = condition
51
44
  block.call
52
45
  end
53
-
46
+
47
+ def define_helpers_methods(attributes)
48
+ attributes.each do |key,value|
49
+ self.send(:define_method, key) do
50
+ value
51
+ end
52
+ end
53
+ end
54
+
55
+ def define_dimension_values_methods
56
+ unless @dimensions.nil?
57
+ @dimensions.each do |name, values|
58
+ values.each do |value|
59
+ self.send(:define_method, value) do
60
+ eval "#{name} == :#{value}"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ def define_select_from_rules
68
+ self.send(:define_method, :select_from_rules) do |rules|
69
+ result = nil
70
+ rules.each do |rule|
71
+ rule_is_satisfied = eval(rule[1])
72
+ if rule_is_satisfied
73
+ result = rule[0]
74
+ break
75
+ end
76
+ end
77
+ result
78
+ end
79
+ end
80
+
54
81
  end
55
82
  end
@@ -1,3 +1,3 @@
1
1
  module MdData
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/md_data.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["kresimir.bojcic@gmail.com"]
7
7
  gem.description = %q{Describe multidimensional data with simple notation}
8
8
  gem.summary = %q{Easy way to descriebe and find values from multidimensional table.}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/drKreso/md_data"
10
10
 
11
11
  gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
12
  gem.files = `git ls-files`.split("\n")
@@ -95,4 +95,35 @@ describe MdData do
95
95
  TestClass.select(:year => 1995, :time_of_day => :evening).should == "8t"
96
96
  end
97
97
 
98
+ it 'is aware of dimensions' do
99
+ class TestClass
100
+ dimension :year, [:year_1994, :year_1995]
101
+ dimension :time_of_day, [:morning, :evening]
102
+ end
103
+
104
+ TestClass.dimensions.should == {:year => [:year_1994, :year_1995],
105
+ :time_of_day => [:morning, :evening] }
106
+ end
107
+
108
+
109
+ it 'is creates helper methods for dimensions values' do
110
+ class TestClass
111
+
112
+ dimension :year, [:year_1994, :year_1995]
113
+ dimension :time_of_day, [:morning, :evening]
114
+
115
+ table_data do
116
+ context "morning" do
117
+ add "18t", "year_1995"
118
+ add "16t", "year_1994"
119
+ end
120
+ context "evening" do
121
+ add "8t", "year_1995"
122
+ end
123
+ end
124
+ end
125
+ TestClass.select(:year => :year_1995, :time_of_day => :morning).should == "18t"
126
+ end
127
+
128
+
98
129
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -29,7 +29,7 @@ files:
29
29
  - lib/md_data/version.rb
30
30
  - md_data.gemspec
31
31
  - spec/md_data/md_data_spec.rb
32
- homepage: ''
32
+ homepage: https://github.com/drKreso/md_data
33
33
  licenses: []
34
34
  post_install_message:
35
35
  rdoc_options: []