md_data 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.un~
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mt_data.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 drKreso
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ #md_data
2
+
3
+ An easy notation for describing multidimensional data.
4
+
5
+ For example:
6
+
7
+ ```
8
+ 1994
9
+ BuenosAires
10
+ Coal
11
+ 19t
12
+ Potassium
13
+ 5t
14
+ 1995
15
+ BuenosAires
16
+ Coal
17
+ 8t
18
+ Potassium
19
+ 6t
20
+ ```
21
+
22
+ Can be written down as:
23
+
24
+ ```
25
+ class MaterialConsumption
26
+ include MdData
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
39
+ end
40
+
41
+ MaterialConsumption.select(:year => 1994, :city => :buenos_aires, :material => :coal) #=> '8t'
42
+ ```
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ gem 'md_data'
49
+
50
+ And then execute:
51
+
52
+ $ bundle
53
+
54
+ Or install it yourself as:
55
+
56
+ $ gem install md_data
57
+
58
+ ## Limitations
59
+
60
+ * No nested context allowed(yet)
61
+ * Tests for misformed data definition
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,55 @@
1
+ module MdData
2
+ def self.included(base)
3
+ base.extend MdDataClassMethods
4
+ end
5
+
6
+ module MdDataClassMethods
7
+ def table_data(&block)
8
+ @table_data_block = block
9
+ end
10
+
11
+ def select(attributes)
12
+ @rules = []
13
+ @current_context = nil
14
+ load_rules
15
+ 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
36
+ container.select_from_rules(@rules)
37
+ end
38
+
39
+ private
40
+ def load_rules
41
+ @table_data_block.call
42
+ end
43
+
44
+ def add(value, condition)
45
+ context_condition = @current_context.nil? ? '' : " && #{@current_context} "
46
+ @rules << ( [value , (condition + context_condition)] )
47
+ end
48
+
49
+ def context(condition, &block)
50
+ @current_context = condition
51
+ block.call
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module MdData
2
+ VERSION = "1.0.0"
3
+ end
data/lib/md_data.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "md_data/version"
2
+ require "md_data/md_data"
3
+
data/md_data.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/md_data/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["drKreso"]
6
+ gem.email = ["kresimir.bojcic@gmail.com"]
7
+ gem.description = %q{Describe multidimensional data with simple notation}
8
+ gem.summary = %q{Easy way to descriebe and find values from multidimensional table.}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "md_data"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MdData::VERSION
17
+ end
@@ -0,0 +1,98 @@
1
+ require 'md_data'
2
+
3
+ describe MdData do
4
+
5
+ class TestClass; include MdData end
6
+
7
+ it 'adds table_data class method when included' do
8
+ TestClass.respond_to?(:table_data).should == true
9
+ end
10
+
11
+ it 'adds select class method when included' do
12
+ TestClass.respond_to?(:select).should == true
13
+ end
14
+
15
+ it 'should store block for latter usage and allow change' do
16
+ class TestClass
17
+ table_data { "LATER" }
18
+ end
19
+ TestClass.send(:load_rules).should == "LATER"
20
+ class TestClass
21
+ table_data { "ALIGATOR" }
22
+ end
23
+ TestClass.send(:load_rules).should == "ALIGATOR"
24
+ end
25
+
26
+ it 'rules can be created with add' do
27
+ class TestClass
28
+ table_data { add "8t", "year == 1994" }
29
+ end
30
+ TestClass.select(:year => 1994).should == "8t"
31
+ end
32
+
33
+ it 'multiple rules can be created with add' do
34
+ class TestClass
35
+ table_data do
36
+ add "8t", "year == 1994"
37
+ add "9t", "year == 1995"
38
+ end
39
+ end
40
+ TestClass.select(:year => 1994).should == "8t"
41
+ TestClass.select(:year => 1995).should == "9t"
42
+ end
43
+
44
+ it 'should create pull out item based on rules and attributes' do
45
+ class TestClass
46
+ table_data { add "8t" , "year == 1994" }
47
+ end
48
+ TestClass.select(:year => 1994).should == "8t"
49
+ end
50
+
51
+ it 'item can be anythinig' do
52
+ class TestClass
53
+ table_data { add TestClass.new , "year == 1994" }
54
+ end
55
+ TestClass.select(:year => 1994).class.should == TestClass
56
+ end
57
+
58
+
59
+ it 'rules can be created with add in context' do
60
+ class TestClass
61
+ table_data do
62
+ context "time_of_day == :morning" do
63
+ add "8t", "year == 1994"
64
+ end
65
+ end
66
+ end
67
+ TestClass.select(:year => 1994, :time_of_day => :morning).should == "8t"
68
+ end
69
+
70
+ it 'rules can be created to comapre with string' do
71
+ class TestClass
72
+ table_data do
73
+ context "time_of_day == 'morning'" do
74
+ add "8t", "year == 1995"
75
+ add "6t", "year == 1994"
76
+ end
77
+ end
78
+ end
79
+ TestClass.select(:year => 1994, :time_of_day => 'morning').should == "6t"
80
+ end
81
+
82
+ it 'can have multiple contexts' do
83
+ class TestClass
84
+ table_data do
85
+ context "time_of_day == :morning" do
86
+ add "18t", "year == 1995"
87
+ add "16t", "year == 1994"
88
+ end
89
+ context "time_of_day == :evening" do
90
+ add "8t", "year == 1995"
91
+ end
92
+ end
93
+ end
94
+ TestClass.select(:year => 1995, :time_of_day => :morning).should == "18t"
95
+ TestClass.select(:year => 1995, :time_of_day => :evening).should == "8t"
96
+ end
97
+
98
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: md_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - drKreso
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-27 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Describe multidimensional data with simple notation
15
+ email:
16
+ - kresimir.bojcic@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/md_data.rb
28
+ - lib/md_data/md_data.rb
29
+ - lib/md_data/version.rb
30
+ - md_data.gemspec
31
+ - spec/md_data/md_data_spec.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.17
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Easy way to descriebe and find values from multidimensional table.
56
+ test_files:
57
+ - spec/md_data/md_data_spec.rb