manhattan 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/manhattan/version.rb +3 -0
- data/lib/manhattan.rb +82 -0
- data/manhattan.gemspec +19 -0
- data/spec/mahattan_spec.rb +58 -0
- data/spec/mysterious_box.rb +16 -0
- data/spec/spec_helper.rb +2 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alvaro Pereyra
|
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,29 @@
|
|
1
|
+
# Manhattan
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'manhattan'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install manhattan
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/manhattan.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "manhattan/version"
|
2
|
+
|
3
|
+
module Manhattan
|
4
|
+
|
5
|
+
def has_statuses(*statuses)
|
6
|
+
@statuses_names = localize_names(statuses)
|
7
|
+
|
8
|
+
statuses.each_with_index do |status, index|
|
9
|
+
add_to_statuses_hash(status, index)
|
10
|
+
create_query_methods(status)
|
11
|
+
end
|
12
|
+
|
13
|
+
create_status_accessors
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def localize_names(args)
|
19
|
+
args.map(&:to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_to_statuses_hash(status, index)
|
23
|
+
@statuses ||= {}
|
24
|
+
@statuses[status] = @statuses_names[index]
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_query_methods(status)
|
28
|
+
define_query_methods(status)
|
29
|
+
define_mark_method(status)
|
30
|
+
end
|
31
|
+
|
32
|
+
def define_query_methods(status)
|
33
|
+
|
34
|
+
query = "#{status}?".to_sym
|
35
|
+
negative_query = "not_#{query}".to_sym
|
36
|
+
alternative_negative_query = "un#{query}".to_sym
|
37
|
+
|
38
|
+
define_method query do
|
39
|
+
self.status == self.class.status(status.to_sym)
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method negative_query do
|
43
|
+
!self.send(query)
|
44
|
+
end
|
45
|
+
alias_method alternative_negative_query, negative_query
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_mark_method(status)
|
49
|
+
|
50
|
+
marking_name = "mark_as_#{status}".to_sym
|
51
|
+
before_marking = "before_#{status}".to_sym
|
52
|
+
after_marking = "after_#{status}".to_sym
|
53
|
+
|
54
|
+
define_method marking_name do
|
55
|
+
send_if_exists(before_marking)
|
56
|
+
self.status = self.class.status(status)
|
57
|
+
send_if_exists(:save)
|
58
|
+
send_if_exists(after_marking)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def create_status_accessors
|
64
|
+
class << self
|
65
|
+
define_method "status" do |queried_status|
|
66
|
+
@statuses[queried_status]
|
67
|
+
end
|
68
|
+
define_method "statuses" do
|
69
|
+
@statuses.values
|
70
|
+
end
|
71
|
+
end
|
72
|
+
define_method "statuses" do
|
73
|
+
self.class.statuses
|
74
|
+
end
|
75
|
+
|
76
|
+
define_method :send_if_exists do |method|
|
77
|
+
self.send(method) if self.respond_to? method
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
data/manhattan.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
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
|
@@ -0,0 +1,58 @@
|
|
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 asign 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
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: manhattan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alvaro Pereyra
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Easy way to define states on your classes
|
15
|
+
email:
|
16
|
+
- alvaro@xendacentral.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/manhattan.rb
|
27
|
+
- lib/manhattan/version.rb
|
28
|
+
- manhattan.gemspec
|
29
|
+
- spec/mahattan_spec.rb
|
30
|
+
- spec/mysterious_box.rb
|
31
|
+
- spec/spec_helper.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
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
none: false
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
none: false
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.23
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Extend your model with Manhattan, and enjoy a simple state machine and value
|
56
|
+
accessors and queries
|
57
|
+
test_files:
|
58
|
+
- spec/mahattan_spec.rb
|
59
|
+
- spec/mysterious_box.rb
|
60
|
+
- spec/spec_helper.rb
|