affairs_of_state 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kevin McPhillips
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,74 @@
1
+ # AffairsOfState
2
+
3
+ You have an Active Record model. It nees to have multiple states, but not complex rules. This gem gives you validation, easy check and change methods, and a single configuration line.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'affairs_of_state'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install affairs_of_state
18
+
19
+ ## Usage
20
+
21
+ The gem assumes you have a string column named "status" on your model:
22
+
23
+ add_column :model_name, :status, :default => "active"
24
+
25
+ Then you just list your states in the model:
26
+
27
+ affairs_of_state :active, :inactive
28
+
29
+ If you'd like to use another column, lets say "state", pass it in as a configuration option:
30
+
31
+ affairs_of_state :active, :inactive, :column => :state
32
+
33
+ You can also turn off validation:
34
+
35
+ affairs_of_state :active, :inactive, :allow_blank => true
36
+
37
+ Or give it a long list of statuses:
38
+
39
+ affairs_of_state :ordered, :cancelled, :shipped, :lost, :in_transit
40
+
41
+
42
+ ## Methods
43
+
44
+ The gem provides methods for checking and setting your status. The question mark method returns a boolean, and the bang method changes to that status. Lets assume you have "active" and "cancelled" as defined status:
45
+
46
+ widget = Widget.first
47
+
48
+ widget.cancelled! if widget.active?
49
+
50
+ You can also access all your statuses on the model like so:
51
+
52
+ Widget::STATUES # -> ["active", "cancelled"]
53
+
54
+
55
+ ## "But I want callbacks and validations and other things."
56
+
57
+ Then this gem isn't for you. Consider:
58
+
59
+ https://github.com/rubyist/aasm
60
+
61
+ https://github.com/pluginaweek/state_machine
62
+
63
+
64
+ ## Tests
65
+
66
+ Just run rspec:
67
+
68
+ rspec
69
+
70
+
71
+ ## The usual
72
+
73
+ Author: Kevin McPhillips - github@kevinmcphillips.ca
74
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/affairs_of_state/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Kevin McPhillips"]
6
+ gem.email = ["github@kevinmcphillips.ca"]
7
+ gem.description = %q{Add a simple state to a gem, without all the hassle of a complex state machine.}
8
+ gem.summary = %q{You have an Active Record model. It nees to have multiple states, but not complex rules. This gem gives you validation, easy check and change methods, and a single configuration line.}
9
+ gem.homepage = "http://github.com/kimos/affairs_of_state"
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 = "affairs_of_state"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = AffairsOfState::VERSION
17
+
18
+ gem.add_dependency "activerecord", "~> 3.0"
19
+
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "sqlite3"
22
+ gem.add_development_dependency "pry"
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module AffairsOfState
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "affairs_of_state/version"
2
+
3
+ module AffairsOfState
4
+
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def affairs_of_state(*args)
11
+ @_status_options = ({:column => :status, :allow_blank => false}).merge(args.extract_options!)
12
+ @_statuses = args.map(&:to_s)
13
+
14
+ const_set("STATUSES", @_statuses)
15
+
16
+ validates(@_status_options[:column], :presence => true) unless @_status_options[:allow_blank]
17
+
18
+ include InstanceMethods
19
+ extend SingletonMethods
20
+ true
21
+ end
22
+ end
23
+
24
+ module InstanceMethods
25
+ def method_missing(method, *args)
26
+ if self.class::STATUSES.map{|s| "#{s}?".to_sym }.include?(method)
27
+ self.class.send(:define_method, method) do
28
+ self.status == method.to_s.gsub(/\?$/, "")
29
+ end
30
+
31
+ send method
32
+
33
+ elsif self.class::STATUSES.map{|s| "#{s}!".to_sym }.include?(method)
34
+ self.class.send(:define_method, method) do
35
+ self.update_attribute(self.class.instance_variable_get('@_status_options')[:column], method.to_s.gsub(/\!$/, ""))
36
+ end
37
+
38
+ send method
39
+ else
40
+ super
41
+ end
42
+ end
43
+ end
44
+
45
+ module SingletonMethods
46
+ def statuses_for_select
47
+ @_statuses.map{|s| [s.humanize, s]}
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ ActiveRecord::Base.send :include, AffairsOfState
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe AffairsOfState do
4
+
5
+ describe "with a simple configuration" do
6
+ class Pie < ActiveRecord::Base
7
+ affairs_of_state :active, :inactive, :cancelled
8
+ end
9
+
10
+ it "should set the constant" do
11
+ Pie::STATUSES.should == ["active", "inactive", "cancelled"]
12
+ end
13
+
14
+ it "should validate the column is set" do
15
+ p = Pie.new :status => nil
16
+ p.should_not be_valid
17
+ end
18
+
19
+ describe "boolean methods" do
20
+ it "should find the set status" do
21
+ p = Pie.new :status => "active"
22
+ p.active?.should be_true
23
+ end
24
+
25
+ it "should not find if a different status is set" do
26
+ p = Pie.new :status => "inactive"
27
+ p.cancelled?.should be_false
28
+ end
29
+ end
30
+
31
+ describe "update methods" do
32
+ it "should set the value" do
33
+ p = Pie.create! :status => "active"
34
+ p.inactive!.should be_true
35
+ p.status.should == "inactive"
36
+ end
37
+ end
38
+
39
+ it "should provide a method to pass to dropdowns" do
40
+ Pie.statuses_for_select.should == [["Active", "active"], ["Inactive", "inactive"], ["Cancelled", "cancelled"]]
41
+ end
42
+ end
43
+
44
+ describe "with a non-standard column name" do
45
+ class Pie2 < ActiveRecord::Base
46
+ self.table_name = "pies"
47
+
48
+ affairs_of_state :active, :inactive, :column => :super_status
49
+ end
50
+
51
+ it "should validate the column is set" do
52
+ p = Pie2.new :status => nil, :super_status => "active"
53
+ p.should be_valid
54
+ end
55
+ end
56
+
57
+ describe "without validations" do
58
+ class Pie3 < ActiveRecord::Base
59
+ self.table_name = "pies"
60
+
61
+ affairs_of_state :active, :inactive, :allow_blank => true
62
+ end
63
+
64
+ it "should validate the column is set" do
65
+ p = Pie3.new :status => nil
66
+ p.should be_valid
67
+ end
68
+ end
69
+
70
+ end
data/spec/db/.gitkeep ADDED
File without changes
Binary file
@@ -0,0 +1,22 @@
1
+ require 'active_record'
2
+ require 'affairs_of_state'
3
+ require 'pry'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ end
9
+
10
+
11
+ ## Create an AR model to test with
12
+
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter => "sqlite3",
15
+ :database => "#{File.expand_path(File.join(File.dirname(__FILE__), '..'))}/spec/db/test.sqlite3"
16
+ )
17
+
18
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'pies'")
19
+ ActiveRecord::Base.connection.create_table(:pies) do |t|
20
+ t.string :status
21
+ t.string :super_status
22
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: affairs_of_state
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin McPhillips
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &75399850 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *75399850
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &75399640 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *75399640
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &75399410 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *75399410
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: &75399200 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *75399200
58
+ description: Add a simple state to a gem, without all the hassle of a complex state
59
+ machine.
60
+ email:
61
+ - github@kevinmcphillips.ca
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - affairs_of_state.gemspec
73
+ - lib/affairs_of_state.rb
74
+ - lib/affairs_of_state/version.rb
75
+ - spec/affairs_of_state_spec.rb
76
+ - spec/db/.gitkeep
77
+ - spec/db/test.sqlite3
78
+ - spec/spec_helper.rb
79
+ homepage: http://github.com/kimos/affairs_of_state
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.17
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: You have an Active Record model. It nees to have multiple states, but not
103
+ complex rules. This gem gives you validation, easy check and change methods, and
104
+ a single configuration line.
105
+ test_files: []
106
+ has_rdoc: