state_store 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.8.3"
12
+ end
13
+
14
+ group :test do
15
+ gem "rspec", "~>2.9.0"
16
+ end
17
+
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.6.6)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.9.0)
16
+ rspec-core (~> 2.9.0)
17
+ rspec-expectations (~> 2.9.0)
18
+ rspec-mocks (~> 2.9.0)
19
+ rspec-core (2.9.0)
20
+ rspec-expectations (2.9.0)
21
+ diff-lcs (~> 1.1.3)
22
+ rspec-mocks (2.9.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.0.0)
29
+ jeweler (~> 1.8.3)
30
+ rdoc (~> 3.12)
31
+ rspec (~> 2.9.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Arturs Meisters
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = state_store
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to state_store
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Arturs Meisters. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "state_store"
18
+ gem.homepage = "http://github.com/kalifs/state_store"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Convert numeric value to Array of states}
21
+ gem.description = %Q{Create numeric value to Array of states.}
22
+ gem.email = "arturs.meisters@gmail.com"
23
+ gem.authors = ["Arturs Meisters"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "state_store #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,59 @@
1
+ module StateStore
2
+ class BinaryStore
3
+ attr_reader :statuses, :states, :total_positions
4
+
5
+ def initialize(statuses)
6
+ raise ArgumentError.new("Only array is accepted.") unless statuses.is_a?(Array)
7
+ @statuses = statuses
8
+ @states = statuses.size
9
+ @total_positions = 2**@states-1
10
+ end
11
+
12
+ def humanize(value)
13
+ raise ArgumentError.new("Out of range") if self.total_positions < value
14
+ value_to_statuses(value)
15
+ end
16
+
17
+ def has_status?(symbol,value)
18
+ human_array = humanize(value)
19
+ human_array.include?(symbol)
20
+ end
21
+
22
+ def index(index,state)
23
+ statuses[index] if state.to_s == "1"
24
+ end
25
+
26
+ private
27
+
28
+ def value_to_statuses(value)
29
+ result = []
30
+ normalized_array(value_to_binary_array(value)).each_with_index do |state,index|
31
+ if current_status = index(index,state)
32
+ result << current_status
33
+ end
34
+ end
35
+ result
36
+ end
37
+
38
+ def normalized_array(array)
39
+ (Array.new(self.states - array.size, "0")+ array)
40
+ end
41
+
42
+ def value_to_binary_array(value)
43
+ value.to_s(2).split("")
44
+ end
45
+
46
+ class BinaryValue
47
+ attr_reader :value,:store
48
+
49
+ def initialize(store,value)
50
+ @value = value
51
+ @store = store
52
+ end
53
+
54
+ def has_status?(symbol)
55
+ self.store.has_status?(symbol,self.value)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,53 @@
1
+ module StateStore
2
+ module Extension
3
+
4
+ module ClassMethods
5
+ def has_statuses *statuses
6
+ @state_store_options = statuses && statuses.last.is_a?(Hash) && statuses.pop || {}
7
+ raise ArgumentError.new("No statuses given") if statuses.empty?
8
+ raise ArgumentError.new(":in is required") unless @state_store_options[:in]
9
+ @store = StateStore::BinaryStore.new(statuses)
10
+ override_method_store_in_method
11
+ end
12
+
13
+ def state_store_store
14
+ @store
15
+ end
16
+
17
+ def state_store_options
18
+ @state_store_options
19
+ end
20
+
21
+ private
22
+
23
+ def override_method_store_in_method
24
+ class_eval <<-ALIAS,__FILE__,__LINE__+1
25
+ if self.instance_methods.include?(:#{self.state_store_options[:in]})
26
+ alias :status_store_original_#{self.state_store_options[:in]} :#{self.state_store_options[:in]}
27
+ end
28
+ ALIAS
29
+
30
+ self.class_eval do
31
+ define_method self.state_store_options[:in] do
32
+ original_method_name = :"status_store_original_#{self.class.state_store_options[:in]}"
33
+ value = self.respond_to?(original_method_name) ? self.send(original_method_name) : super
34
+ self.class.state_store_store.humanize(value)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ module InstanceMethods
41
+ def self.included(base)
42
+
43
+ def method_missing(method_name, *args)
44
+ if method_name.to_s == "has_#{self.class.state_store_options[:in]}?"
45
+ self.class.state_store_store.has_status?(args[0],self.send(self.class.state_store_options[:in]))
46
+ else
47
+ super
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ require 'state_store/binary_store'
2
+ require 'state_store/extension'
3
+
4
+ module StateStore
5
+ def self.included(base_klass)
6
+ base_klass.extend(StateStore::Extension::ClassMethods)
7
+ base_klass.class_eval do
8
+ include StateStore::Extension::InstanceMethods
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'state_store'
3
+
4
+ describe StateStore::BinaryStore do
5
+ let(:klass){StateStore::BinaryStore}
6
+
7
+ it "should create new store instance with statuses" do
8
+ expect{
9
+ klass.new([])
10
+ klass.new([:read])
11
+ klass.new([:read,:write])
12
+ }.not_to raise_error
13
+ end
14
+
15
+ it "it should raise error when statuses not Array" do
16
+ expect{
17
+ klass.new(Object.new)
18
+ }.to raise_error(ArgumentError,"Only array is accepted.")
19
+ end
20
+
21
+ describe "attributes" do
22
+ let(:statuses){[:one,:two,:five]}
23
+ it "should assign received statuses to statuses" do
24
+ store = klass.new(statuses)
25
+ store.statuses.should eq(statuses)
26
+ end
27
+
28
+ it "should assign states statuses size" do
29
+ store = klass.new(statuses)
30
+ store.states.should eq(statuses.size)
31
+ end
32
+
33
+ it "should calculate and assign total positions to total_positions" do
34
+ store = klass.new(statuses)
35
+ store.total_positions.should eq((2**statuses.size)-1)
36
+ end
37
+ end
38
+
39
+ it "should convert value to array with matching human statuses" do
40
+ store = klass.new([:read,:write,:execute])
41
+ store.humanize(0).should eq([])
42
+ store.humanize(1).should eq([:execute])
43
+ store.humanize(2).should eq([:write])
44
+ store.humanize(3).should eq([:write,:execute])
45
+ store.humanize(4).should eq([:read])
46
+ store.humanize(5).should eq([:read,:execute])
47
+ store.humanize(6).should eq([:read,:write])
48
+ store.humanize(7).should eq([:read,:write,:execute])
49
+ end
50
+
51
+ it "should raise error when given value is greated then store total positions" do
52
+ store = klass.new([:one])
53
+ expect{
54
+ store.humanize(2)
55
+ }.to raise_error(ArgumentError,"Out of range")
56
+ end
57
+
58
+ it "should detect if value include given status" do
59
+ store = klass.new([:read,:write,:execute])
60
+ store.has_status?(:read,5).should be_true
61
+ store.has_status?(:read,3).should be_false
62
+ end
63
+
64
+ it "should return human status for given index when given state is '1'" do
65
+ store = klass.new([:one,:two])
66
+ store.index(1,"1").should eq(:two)
67
+ store.index(1,"0").should be_nil
68
+ end
69
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'state_store'
3
+
4
+ describe StateStore::BinaryStore::BinaryValue do
5
+ let(:store){double("store")}
6
+ let(:klass){StateStore::BinaryStore::BinaryValue}
7
+
8
+ it "should create new with store and value" do
9
+ expect{
10
+ klass.new(store,4)
11
+ }.not_to raise_error
12
+ end
13
+
14
+ describe "attributes" do
15
+ it "should have value" do
16
+ value = klass.new(store,4)
17
+ value.value.should eq(4)
18
+ end
19
+
20
+ it "should have store" do
21
+ value =klass.new(store,4)
22
+ value.store.should eq(store)
23
+ end
24
+ end
25
+
26
+ it "should detect if it has status" do
27
+ value = klass.new(store,4)
28
+ store.should_receive(:has_status?).with(:read,4)
29
+ value.has_status?(:read)
30
+ end
31
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'state_store'
3
+
4
+ describe StateStore::Extension do
5
+ let(:klass){Class.new}
6
+
7
+ it "should add #has_store class method" do
8
+ klass.send(:include,StateStore)
9
+ klass.should.respond_to?(:has_store)
10
+ end
11
+
12
+ describe "ClassMethods" do
13
+ before(:each) do
14
+ klass.send(:include,StateStore)
15
+ end
16
+
17
+ it "should raise error when no statuses is given" do
18
+ expect{
19
+ klass.has_statuses()
20
+ }.to raise_error(ArgumentError,"No statuses given")
21
+ end
22
+
23
+ it "should raise error when no :in key is given" do
24
+ expect{
25
+ klass.has_statuses :read,:write
26
+ }.to raise_error(ArgumentError,":in is required")
27
+ end
28
+
29
+ it "should create store with given statuses" do
30
+ klass.has_statuses :read,:write, :in => :status
31
+ klass.state_store_store.statuses.should eq([:read,:write])
32
+ end
33
+
34
+ it "should store given options in #state_store_options" do
35
+ klass.has_statuses :read,:write, :in => :status
36
+ klass.state_store_options.should eq({:in => :status})
37
+ end
38
+ end
39
+
40
+ describe "InstanceMethods" do
41
+ let(:object){klass.new}
42
+ before(:each){
43
+ klass.class_eval do
44
+ def status_name
45
+ 5
46
+ end
47
+ end
48
+ klass.send(:include,StateStore)
49
+ }
50
+
51
+ it "should call store #has_status? when #has_status_name? is called on klass instance" do
52
+ klass.has_statuses :read,:write,:execute, :in => :status_name
53
+ klass.any_instance.stub(:status_name).and_return(5)
54
+
55
+ object.has_status_name?(:read).should be_true
56
+ object.has_status_name?(:write).should be_false
57
+ end
58
+
59
+ it "should call store #humanize when [status_name] is called on instance of class" do
60
+ klass.has_statuses :read,:write,:execute, :in => :status_name
61
+ object.status_name.should eq([:read,:execute])
62
+ end
63
+
64
+ it "should call super if base class not respond to method" do
65
+
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,11 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "state_store"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Arturs Meisters"]
12
+ s.date = "2012-03-29"
13
+ s.description = "Create numeric value to Array of states."
14
+ s.email = "arturs.meisters@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/state_store.rb",
29
+ "lib/state_store/binary_store.rb",
30
+ "lib/state_store/extension.rb",
31
+ "spec/binary_store_spec.rb",
32
+ "spec/binary_value_spec.rb",
33
+ "spec/extension_spec.rb",
34
+ "spec/spec_helper.rb",
35
+ "state_store.gemspec"
36
+ ]
37
+ s.homepage = "http://github.com/kalifs/state_store"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.10"
41
+ s.summary = "Convert numeric value to Array of states"
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
50
+ else
51
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
59
+ end
60
+ end
61
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arturs Meisters
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &18782820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *18782820
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &18781480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *18781480
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &18799320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18799320
47
+ description: Create numeric value to Array of states.
48
+ email: arturs.meisters@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .rspec
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - LICENSE.txt
60
+ - README.rdoc
61
+ - Rakefile
62
+ - VERSION
63
+ - lib/state_store.rb
64
+ - lib/state_store/binary_store.rb
65
+ - lib/state_store/extension.rb
66
+ - spec/binary_store_spec.rb
67
+ - spec/binary_value_spec.rb
68
+ - spec/extension_spec.rb
69
+ - spec/spec_helper.rb
70
+ - state_store.gemspec
71
+ homepage: http://github.com/kalifs/state_store
72
+ licenses:
73
+ - MIT
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
85
+ - 0
86
+ hash: -2956134080418210540
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.10
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Convert numeric value to Array of states
99
+ test_files: []