etat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ce676012deefc95e65555d86ec981b3a0e61ccb
4
+ data.tar.gz: e0b86cd39e02bccd8dc4abf73590dfe32ced0f72
5
+ SHA512:
6
+ metadata.gz: ba4f0ea7a05b86a7c502ad7aa5b57a947d14b07e402b5edf82d3c1cf0130be60c58dcf02f5cecd3ba575727ab7364736e6aef729d2d40bfb8e9e4e3a89213968
7
+ data.tar.gz: 21f5f8a7fe2166e4d6ef1eae7e3ba4c4a7a00981d581ebcbfe494672266298d9a82c334db153ab29b22c4ad377803c9c816d7ae19815ce15594c522b87a85dbe
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+
19
+ # Mac specific
20
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in etat.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jean-Philippe Boily
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,43 @@
1
+ # Etat
2
+
3
+ Etat is a minimalistic gem to help you (me, for now) manage some states in your app. It is built for Rails 4 and assumes the use of ActiveRecord 4. See the 'usage' section for more.
4
+
5
+ Etat means "State" in French.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'etat'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ class MyModel < ActiveRecord::Base
21
+ has_states :draft, :published, :deleted
22
+ event :publish do
23
+ # some code to happen on publish...
24
+ self.state = :publish
25
+ self.save!
26
+ end
27
+ end
28
+ ```
29
+
30
+ Etat will generate a few methods and scopes that will help you:
31
+ `MyModel.states` will give you an array of the states. # [:draft, :published, :deleted]
32
+ `MyModel.all_published`: scope that will return all records with that state (and equivalents for each states).
33
+ `MyModel.all_but_published`: scope that will return all records except the ones with that state (and equivalents for each states).
34
+ `my_model_instance.state`: it will return the current state, as a symbol, always.
35
+ `my_model_instance.published?`: boolean. Will tell you if current record has that state or not (and equivalents for each states).
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/etat.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'etat/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "etat"
8
+ spec.version = Etat::VERSION
9
+ spec.authors = ["Jean-Philippe Boily"]
10
+ spec.email = ["j@jipi.ca"]
11
+ spec.description = %q{Etat is a minimalistic gem to help you (me, for now) manage some states in your Rails app.}
12
+ spec.summary = %q{Etat is a minimalistic gem to help you (me, for now) manage some states in your Rails app. It is built for Rails 4 and assumes the use of ActiveRecord 4.}
13
+ spec.homepage = "http://github.com/jipiboily/etat"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", ">= 4.0.0"
22
+
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "factory_girl_rails"
25
+ spec.add_development_dependency "sqlite3"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ end
data/lib/etat.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "etat/version"
2
+
3
+ require 'active_record'
4
+ require 'active_support/concern'
5
+
6
+ require 'etat/active_record'
7
+
8
+ module Etat
9
+ end
@@ -0,0 +1,53 @@
1
+ module Etat
2
+ module ActiveRecord
3
+ extend ActiveSupport::Concern
4
+
5
+ def state
6
+ if attributes['state']
7
+ attributes['state'].to_sym
8
+ else
9
+ attributes['state']
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def has_states *states
15
+ @states = []
16
+ states.each do |state|
17
+ if state.is_a?(Symbol)
18
+ create_state_with state
19
+ create_scopes state
20
+ @states << state
21
+ else
22
+ raise 'States should be symbol'
23
+ end
24
+ end
25
+ end
26
+
27
+ def event event_name, &block
28
+ define_method(event_name) do
29
+ instance_eval &block
30
+ end
31
+ end
32
+
33
+ def states
34
+ @states
35
+ end
36
+
37
+ private
38
+ def create_scopes state
39
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
40
+ scope "all_#{state}", -> { where(state: state) }
41
+ scope "all_but_#{state}", -> { where("state != ?", state) }
42
+ RUBY
43
+ end
44
+
45
+ private
46
+ def create_state_with state
47
+ define_method("#{state}?") do
48
+ self.state == state.to_sym
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Etat
2
+ VERSION = "0.0.1"
3
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,13 @@
1
+ FactoryGirl.define do
2
+ factory :draft, class: FakeModel do
3
+ state :draft
4
+ end
5
+
6
+ factory :published, class: FakeModel do
7
+ state :published
8
+ end
9
+
10
+ factory :deleted, class: FakeModel do
11
+ state :deleted
12
+ end
13
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Etat::ActiveRecord do
4
+ describe '.states' do
5
+ it { expect(FakeModel.states).to eq [:draft, :published, :deleted] }
6
+ end
7
+
8
+ describe :scopes do
9
+ before do
10
+ published = create_list(:published, 4)
11
+ drafts = create_list(:draft, 3)
12
+ deleted = create_list(:deleted, 2)
13
+ @counts_per_state = {
14
+ published: published.size,
15
+ deleted: deleted.size,
16
+ draft: drafts.size,
17
+ total: (published.size + deleted.size + drafts.size)
18
+ }
19
+ end
20
+
21
+ FakeModel.states.each do |state|
22
+ describe ".all_#{state}" do
23
+ it "returns only '#{state}' records" do
24
+ expect(FakeModel.send("all_#{state}").count).to eq @counts_per_state[state]
25
+ end
26
+ end
27
+
28
+ describe ".all_but_#{state}" do
29
+ it "returns all records besides '#{state}' ones" do
30
+ expect(FakeModel.send("all_but_#{state}").count).to eq (@counts_per_state[:total] - @counts_per_state[state])
31
+ end
32
+ end
33
+
34
+ describe "##{state}?" do
35
+ it 'returns true if this is the current state' do
36
+ expect(FakeModel.new(state: state).send("#{state}?")).to be_true
37
+ end
38
+
39
+ it 'returns false if this is not the current state' do
40
+ expect(FakeModel.new(state: 'invalid state').send("#{state}?")).to be_false
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '.event' do
47
+ it { expect(FakeModel.new.publish.draft?).to be_false }
48
+ it { expect(FakeModel.new.publish.published?).to be_true }
49
+ it { expect(FakeModel.new.publish.deleted?).to be_false }
50
+
51
+ it { expect(FakeModel.new.unpublish.draft?).to be_true }
52
+ it { expect(FakeModel.new.unpublish.published?).to be_false }
53
+ it { expect(FakeModel.new.unpublish.deleted?).to be_false }
54
+
55
+ it { expect(FakeModel.new.delete.draft?).to be_false }
56
+ it { expect(FakeModel.new.delete.published?).to be_false }
57
+ it { expect(FakeModel.new.delete.deleted?).to be_true }
58
+ end
59
+
60
+ describe '#state' do
61
+ it { expect(FakeModel.new(state: 'draft').state).to be_a Symbol }
62
+ it { expect(FakeModel.new(state: 'draft').state).to eq :draft }
63
+ end
64
+ end
@@ -0,0 +1,27 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+
3
+ require 'rspec'
4
+ require 'sqlite3'
5
+ require 'factory_girl_rails'
6
+
7
+ require 'etat'
8
+
9
+
10
+ # Require support directory
11
+ Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].map(&method(:require))
12
+
13
+ require 'factories'
14
+
15
+ RSpec.configure do |config|
16
+ config.before(:each) do
17
+ FakeModel.delete_all
18
+ end
19
+
20
+ config.before(:all) do
21
+ Etat::SpecHelper::Database.create_database
22
+ end
23
+
24
+ config.after(:all) do
25
+ Etat::SpecHelper::Database.delete_database
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module Etat::SpecHelper
2
+ module Database
3
+ def self.create_database
4
+ SQLite3::Database.new FileUtils.touch(Etat::SpecHelper::Database.db_file).first
5
+ self.connect
6
+ self.create_fake_table
7
+ end
8
+
9
+ def self.delete_database
10
+ FileUtils.rm(Etat::SpecHelper::Database.db_file) if File.exists?(Etat::SpecHelper::Database.db_file)
11
+ end
12
+
13
+ def self.db_file
14
+ File.expand_path('../../test.db', __FILE__)
15
+ end
16
+
17
+ private
18
+ class CreateFakeModel < ActiveRecord::Migration
19
+ def up
20
+ create_table(:fake_models) do |t|
21
+ t.string :state
22
+ t.timestamp
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.create_fake_table
28
+ CreateFakeModel.new.up
29
+ end
30
+
31
+ def self.connect
32
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: Etat::SpecHelper::Database.db_file)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ RSpec.configure do |config|
2
+ config.include FactoryGirl::Syntax::Methods
3
+ end
@@ -0,0 +1,20 @@
1
+ class FakeModel < ActiveRecord::Base
2
+ include Etat::ActiveRecord
3
+
4
+ has_states :draft, :published, :deleted
5
+
6
+ event :publish do
7
+ self.state = :published
8
+ self
9
+ end
10
+
11
+ event :unpublish do
12
+ self.state = :draft
13
+ self
14
+ end
15
+
16
+ event :delete do
17
+ self.state = :deleted
18
+ self
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: etat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jean-Philippe Boily
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl_rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Etat is a minimalistic gem to help you (me, for now) manage some states
98
+ in your Rails app.
99
+ email:
100
+ - j@jipi.ca
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - etat.gemspec
111
+ - lib/etat.rb
112
+ - lib/etat/active_record.rb
113
+ - lib/etat/version.rb
114
+ - spec/factories.rb
115
+ - spec/lib/active_record_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/database.rb
118
+ - spec/support/factory_girl.rb
119
+ - spec/support/fake_model.rb
120
+ homepage: http://github.com/jipiboily/etat
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.0.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Etat is a minimalistic gem to help you (me, for now) manage some states in
144
+ your Rails app. It is built for Rails 4 and assumes the use of ActiveRecord 4.
145
+ test_files:
146
+ - spec/factories.rb
147
+ - spec/lib/active_record_spec.rb
148
+ - spec/spec_helper.rb
149
+ - spec/support/database.rb
150
+ - spec/support/factory_girl.rb
151
+ - spec/support/fake_model.rb