manhattan 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,4 @@
1
- Copyright (c) 2013 Alvaro Pereyra
2
-
3
- MIT License
1
+ Copyright 2013 Alvaro Pereyra
4
2
 
5
3
  Permission is hereby granted, free of charge, to any person obtaining
6
4
  a copy of this software and associated documentation files (the
@@ -19,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
19
  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.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -26,4 +26,4 @@ TODO: Write usage instructions here
26
26
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
27
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
28
  4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
29
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,27 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Manhattan'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -1,3 +1,3 @@
1
1
  module Manhattan
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/manhattan.rb CHANGED
@@ -2,81 +2,117 @@ require "manhattan/version"
2
2
 
3
3
  module Manhattan
4
4
 
5
- def has_statuses(*statuses)
6
- @statuses_names = localize_names(statuses)
5
+ extend ActiveSupport::Concern
7
6
 
8
- statuses.each_with_index do |status, index|
9
- add_to_statuses_hash(status, index)
10
- create_query_methods(status)
11
- end
7
+ def status_column_value
8
+ self.send(self.class.status_column_name)
9
+ end
12
10
 
13
- create_status_accessors
11
+ def status_value(status)
12
+ self.class.status(status)
14
13
  end
15
14
 
16
- private
15
+ def statuses
16
+ self.class.statuses
17
+ end
17
18
 
18
- def localize_names(args)
19
- args.map(&:to_s)
19
+ def status_write_method
20
+ "#{self.class.status_column_name}="
20
21
  end
21
22
 
22
- def add_to_statuses_hash(status, index)
23
- @statuses ||= {}
24
- @statuses[status] = @statuses_names[index]
23
+ def default_status_value
24
+ self.class.default_status_value
25
25
  end
26
26
 
27
- def create_query_methods(status)
28
- define_query_methods(status)
29
- define_mark_method(status)
27
+ def send_if_exists(method)
28
+ self.send(method) if self.respond_to? method
30
29
  end
31
30
 
32
- def define_query_methods(status)
31
+ def set_default_value
32
+ self.send(status_write_method, status_value(default_status_value)) unless self.status_column_value
33
+ end
34
+
35
+ module ClassMethods
36
+
37
+ attr_accessor :status_column_name, :default_status_value
38
+
39
+ def has_statuses(*statuses)
40
+ options = statuses.pop if statuses.last.is_a? Hash
41
+ options ||= {}
42
+ @status_column_name = options[:column_name]
43
+ @status_column_name ||= :status
33
44
 
34
- query = "#{status}?".to_sym
35
- negative_query = "not_#{query}".to_sym
36
- alternative_negative_query = "un#{query}".to_sym
37
- alternate_negative_query = "in#{query}".to_sym
45
+ @default_status_value = options[:default_value]
46
+ @statuses_names = localize_names(statuses)
47
+
48
+ after_initialize :set_default_value
49
+
50
+ statuses.each_with_index do |status, index|
51
+ add_to_statuses_hash(status, index)
52
+ create_query_methods(status)
53
+ end
38
54
 
39
- define_method query do
40
- self.status == self.class.status(status.to_sym)
41
55
  end
42
56
 
43
- define_method negative_query do
44
- !self.send(query)
57
+ def status(queried_status)
58
+ @statuses[queried_status]
45
59
  end
46
- alias_method alternative_negative_query, negative_query
47
- alias_method alternate_negative_query, negative_query
48
- end
49
60
 
50
- def define_mark_method(status)
61
+ def statuses
62
+ @statuses.values
63
+ end
51
64
 
52
- marking_name = "mark_as_#{status}".to_sym
53
- before_marking = "before_#{status}".to_sym
54
- after_marking = "after_#{status}".to_sym
65
+ private
55
66
 
56
- define_method marking_name do
57
- send_if_exists(before_marking)
58
- self.status = self.class.status(status)
59
- send_if_exists(:save)
60
- send_if_exists(after_marking)
67
+ def localize_names(args)
68
+ args.map(&:to_s)
61
69
  end
62
- end
63
70
 
71
+ def add_to_statuses_hash(status, index)
72
+ @statuses ||= {}
73
+ @statuses[status] = @statuses_names[index]
74
+ end
64
75
 
65
- def create_status_accessors
66
- class << self
67
- define_method "status" do |queried_status|
68
- @statuses[queried_status]
76
+ def create_query_methods(status)
77
+ define_query_methods(status)
78
+ define_mark_method(status)
79
+ end
80
+
81
+ def define_query_methods(status)
82
+
83
+ status_sym = status.to_sym
84
+ query = "#{status}?".to_sym
85
+ negative_query = "not_#{query}".to_sym
86
+ alternative_negatives = ["un#{query}".to_sym, "in#{query}".to_sym]
87
+
88
+ define_method query do
89
+ status_column_value == status_value(status_sym)
69
90
  end
70
- define_method "statuses" do
71
- @statuses.values
91
+
92
+ define_method negative_query do
93
+ !self.send(query)
72
94
  end
73
- end
74
- define_method "statuses" do
75
- self.class.statuses
95
+
96
+ scope status_sym, where(@status_column_name => status(status_sym))
97
+
98
+ alternative_negatives.each do |alternative|
99
+ alias_method alternative, negative_query
100
+ end
101
+
76
102
  end
77
103
 
78
- define_method :send_if_exists do |method|
79
- self.send(method) if self.respond_to? method
104
+ def define_mark_method(status)
105
+
106
+ marking_name = "mark_as_#{status}".to_sym
107
+ before_marking = "before_#{status}".to_sym
108
+ after_marking = "after_#{status}".to_sym
109
+
110
+ define_method marking_name do
111
+ send_if_exists(before_marking)
112
+ self.send(status_write_method,self.class.status(status))
113
+ self.save
114
+ send_if_exists(after_marking)
115
+ end
80
116
  end
81
117
 
82
118
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :manhattan do
3
+ # # Task goes here
4
+ # end
metadata CHANGED
@@ -2,15 +2,47 @@
2
2
  name: manhattan
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alvaro Pereyra
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-05 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.10
20
+ none: false
21
+ name: rails
22
+ type: :runtime
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 3.2.10
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ name: sqlite3
38
+ type: :development
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
14
46
  description: Easy way to define states on your classes
15
47
  email:
16
48
  - alvaro@xendacentral.com
@@ -18,17 +50,12 @@ executables: []
18
50
  extensions: []
19
51
  extra_rdoc_files: []
20
52
  files:
21
- - .gitignore
22
- - Gemfile
23
- - LICENSE.txt
24
- - README.md
25
- - Rakefile
26
- - lib/manhattan.rb
27
53
  - lib/manhattan/version.rb
28
- - manhattan.gemspec
29
- - spec/manhattan_spec.rb
30
- - spec/mysterious_box.rb
31
- - spec/spec_helper.rb
54
+ - lib/manhattan.rb
55
+ - lib/tasks/manhattan_tasks.rake
56
+ - MIT-LICENSE
57
+ - Rakefile
58
+ - README.rdoc
32
59
  homepage: ''
33
60
  licenses: []
34
61
  post_install_message:
@@ -54,7 +81,4 @@ signing_key:
54
81
  specification_version: 3
55
82
  summary: Extend your model with Manhattan, and enjoy a simple state machine and value
56
83
  accessors and queries
57
- test_files:
58
- - spec/manhattan_spec.rb
59
- - spec/mysterious_box.rb
60
- - spec/spec_helper.rb
84
+ test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
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
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in manhattan.gemspec
4
- gemspec
data/manhattan.gemspec DELETED
@@ -1,19 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'manhattan/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "manhattan"
8
- gem.version = Manhattan::VERSION
9
- gem.authors = ["Alvaro Pereyra"]
10
- gem.email = ["alvaro@xendacentral.com"]
11
- gem.description = "Easy way to define states on your classes"
12
- gem.summary = "Extend your model with Manhattan, and enjoy a simple state machine and value accessors and queries"
13
- gem.homepage = ""
14
-
15
- gem.files = `git ls-files`.split($/)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
19
- end
@@ -1,58 +0,0 @@
1
- require "spec_helper"
2
- require 'manhattan'
3
- require 'mysterious_box'
4
-
5
- describe Manhattan do
6
-
7
- let(:box) { MysteriousBox.new }
8
-
9
- it "creates a status map through the class method" do
10
- box.statuses.count.should be(3)
11
- end
12
-
13
- it "lists statuses" do
14
- MysteriousBox.statuses.should eq(["opened", "closed", "glowing"])
15
- end
16
-
17
- it "creates query methods for each state" do
18
- box.should respond_to(:opened?)
19
- box.should respond_to(:closed?)
20
- box.should_not respond_to(:eated?)
21
- end
22
-
23
- it "creates query methods that negated each state" do
24
- box.should respond_to(:not_opened?)
25
- box.should respond_to(:unopened?)
26
- end
27
-
28
- it "sets a new status" do
29
- box.mark_as_opened
30
- box.status.should == "opened"
31
- end
32
-
33
- describe "when setting an state" do
34
-
35
- before do
36
- box.mark_as_opened
37
- end
38
-
39
- it "allows to query current state" do
40
- box.opened?.should eq(true)
41
- box.closed?.should eq(false)
42
- end
43
-
44
- it "allows to query the negated state" do
45
- box.unopened?.should eq(false)
46
- box.not_closed?.should eq(true)
47
- end
48
-
49
- it "calls a before and after state method only if it exists" do
50
- box.should_receive(:before_glowing)
51
- box.mark_as_glowing
52
- end
53
-
54
- end
55
-
56
-
57
-
58
- end
@@ -1,16 +0,0 @@
1
- class MysteriousBox
2
- extend Manhattan
3
-
4
- attr_accessor :status
5
-
6
- has_statuses :opened, :closed, :glowing
7
-
8
- def before_glowing
9
- "Expectations"
10
- end
11
-
12
- def after_closed
13
- "Dreams"
14
- end
15
-
16
- end
data/spec/spec_helper.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'rubygems'
2
- require 'rspec/autorun'