simplest_status 0.1.0

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: 845888ba96293dd86f09dfa9d2f04de2b0645972
4
+ data.tar.gz: f10292a0a5691358a4e6f8df725c52edd3d81878
5
+ SHA512:
6
+ metadata.gz: aebf80db4038ac7a6b7c6b4fc29d76270ef7a5ada06c954fc7a75200552e38b4fe6ec224919d5d59b1acc8f6cb7af1e709f306653689fbf31b1423df666a48c6
7
+ data.tar.gz: bd1c060400a9a0c3a983600c53b1c77e425a8263c351ad765b986203f575bb23316bd909f9f43ea7ceb0e1dbd1685454ebe3834ce4cc4d0739fb18fcb78b6138
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
11
+ *.gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,27 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.2
7
+ gemfile:
8
+ - gemfiles/rails_20.gemfile
9
+ - gemfiles/rails_31.gemfile
10
+ - gemfiles/rails_32.gemfile
11
+ - gemfiles/rails_40.gemfile
12
+ - gemfiles/rails_41.gemfile
13
+ - gemfiles/rails_42.gemfile
14
+ matrix:
15
+ exclude:
16
+ - rvm: 2.2.2
17
+ gemfile: gemfiles/rails_20.gemfile
18
+ - rvm: 2.2.2
19
+ gemfile: gemfiles/rails_31.gemfile
20
+ - rvm: 2.2.2
21
+ gemfile: gemfiles/rails_32.gemfile
22
+ - rvm: 2.2.2
23
+ gemfile: gemfiles/rails_40.gemfile
24
+ before_install: gem install bundler -v 1.10.2
25
+ addons:
26
+ code_climate:
27
+ repo_token: 406484919b3c7b57d10643c465efb9cbe9109a59ea6b5d64fb099c6c1e81a9f4
data/Appraisals ADDED
@@ -0,0 +1,23 @@
1
+ appraise "rails-20" do
2
+ gem "rails", "~> 2.0.5"
3
+ end
4
+
5
+ appraise "rails-31" do
6
+ gem "rails", "~> 3.1.12"
7
+ end
8
+
9
+ appraise "rails-32" do
10
+ gem "rails", "~> 3.2.21"
11
+ end
12
+
13
+ appraise "rails-40" do
14
+ gem "rails", "~> 4.0.13"
15
+ end
16
+
17
+ appraise "rails-41" do
18
+ gem "rails", "~> 4.1.10"
19
+ end
20
+
21
+ appraise "rails-42" do
22
+ gem "rails", "~> 4.2.1"
23
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simplest_status.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # SimplestStatus [![Gem Version](https://badge.fury.io/rb/simplest_status.svg)](http://badge.fury.io/rb/simplest_status) [![Code Climate](https://codeclimate.com/github/vigetlabs/simplest_status/badges/gpa.svg)](https://codeclimate.com/github/vigetlabs/simplest_status) [![Test Coverage](https://codeclimate.com/github/vigetlabs/simplest_status/badges/coverage.svg)](https://codeclimate.com/github/vigetlabs/simplest_status/coverage) [![Build Status](https://travis-ci.org/vigetlabs/simplest_status.svg)]((https://travis-ci.org/vigetlabs/simplest_status))
2
+
3
+ SimplestStatus is a gem built to provide simple, convenient status functionality for Rails models. It's designed to work with practically every version of Rails (tested as far back as 2.0.5) and will work with Ruby 1.9.3 and up.
4
+
5
+ SimplestStatus is similar to the recently introduced [`enum`](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html) (debuted in Rails 4.1), but is different in that it doesn't rely on a particular version of Rails, it's geared specifically toward `status` columns, and it provides additional functionality like constant-based status lookup, label helpers, and validations.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'simplest_status'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install simplest_status
21
+
22
+ ## Usage
23
+ Add an `integer`-type `status` field to a model with `:null => false` and `:default => 0`:
24
+ ```ruby
25
+ class AddStatusToPostsMigration < ActiveRecord::Migration
26
+ def change
27
+ add_column :posts, :status, :integer, :null => false, :default => 0
28
+ end
29
+ end
30
+ ```
31
+ Then in your model, extend `SimplestStatus` and list out your statuses:
32
+ ```ruby
33
+ class Post < ActiveRecord::Base
34
+ extend SimplestStatus
35
+
36
+ statuses :draft, :preview, :published, :archived
37
+ end
38
+ ```
39
+
40
+ This will generate a number of constants, methods, and model validations.
41
+
42
+ #### Status List
43
+ ```ruby
44
+ Post.statuses # => { :draft => 0, :preview => 1, :published => 2, :archived => 3 }
45
+ ```
46
+
47
+ The returned hash is a [`StatusCollection`](link) that, when iterated over, yields [`Status`](link) objects:
48
+ ```
49
+ Post.statuses.first.tap do |status|
50
+ status.name # => :draft
51
+ status.value # => 0
52
+ status.string # => 'draft'
53
+ status.to_hash # => { :draft => 0 }
54
+ status.constant_name # => 'DRAFT'
55
+ status.label # => 'Draft'
56
+ status.for_select # => ['Draft', 0]
57
+ end
58
+ ```
59
+
60
+ It also provides a helper method for usage in a form select:
61
+ ```ruby
62
+ Post.statuses.for_select # => [['Draft', 0], ['Preview', 1], ['Published', 2], ['Archived', 3]]
63
+ ```
64
+
65
+ #### Constants
66
+ Instead of referring to status values by the underlying integer, SimplestStatus generates constants for this purpose:
67
+ ```ruby
68
+ Post::DRAFT # => 0
69
+ Post::PREVIEW # => 1
70
+ Post::PUBLISHED # => 2
71
+ Post::ARCHIVED # => 3
72
+ ```
73
+
74
+ #### Scopes
75
+ ```ruby
76
+ Post.draft
77
+ Post.preview
78
+ Post.published
79
+ Post.archived
80
+ ```
81
+
82
+ #### Predicate Methods
83
+ ```ruby
84
+ Post.new(:status => Post::DRAFT) do |post|
85
+ post.draft? # => true
86
+ post.preview? # => false
87
+ post.published? # => false
88
+ post.archived? # => false
89
+ end
90
+ ```
91
+
92
+ #### Status Mutation Methods
93
+ ```ruby
94
+ Post.new(:status => Post::ARCHIVED) do
95
+ post.draft # status from Post::ARCHIVED to Post::DRAFT
96
+ post.preview # status from Post::DRAFT to Post::PREVIEW
97
+ post.published # status from Post::PREVIEW to Post::PUBLISHED
98
+ post.archived # status from Post::PUBLISHED to Post::ARCHIVED
99
+ end
100
+ ```
101
+
102
+ #### Status Label Method
103
+ ```ruby
104
+ Post.new(:status => Post::DRAFT).status_label # => 'Draft'
105
+ Post.new(:status => Post::PREVIEW).status_label # => 'Preview'
106
+ Post.new(:status => Post::PUBLISHED).status_label # => 'Published'
107
+ Post.new(:status => Post::ARCHIVED).status_label # => 'Archived'
108
+ ```
109
+
110
+ #### Status Validations
111
+ SimplestStatus will automatically add the following validations:
112
+ ```ruby
113
+ validates :status, :presence => true, :inclusion => { :in => proc { statuses.values } }
114
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simplest_status"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 2.0.5"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.1.12"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 3.2.21"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.0.13"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.1.10"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.2.1"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,61 @@
1
+ module SimplestStatus
2
+ module ModelMethods
3
+ def self.included(base)
4
+ Configurator.new(base).configure
5
+ end
6
+
7
+ private
8
+
9
+ class Configurator < Struct.new(:model)
10
+ def configure
11
+ model.statuses.each do |status|
12
+ set_constant_for status
13
+ define_class_methods_for status
14
+ define_instance_methods_for status
15
+ end
16
+
17
+ define_status_label_method
18
+ set_validations
19
+ end
20
+
21
+ private
22
+
23
+ def set_constant_for(status_info)
24
+ model.send :const_set, status_info.constant_name, status_info.value
25
+ end
26
+
27
+ def define_class_methods_for(status_info)
28
+ model.send :define_singleton_method, status_info.symbol do
29
+ where(:status => status_info.value)
30
+ end
31
+ end
32
+
33
+ def define_instance_methods_for(status_info)
34
+ define_predicate(status_info)
35
+ define_status_setter(status_info)
36
+ end
37
+
38
+ def define_predicate(status_info)
39
+ model.send :define_method, "#{status_info.symbol}?" do
40
+ status == status_info.value
41
+ end
42
+ end
43
+
44
+ def define_status_setter(status_info)
45
+ model.send :define_method, status_info.symbol do
46
+ update_attributes(:status => status_info.value)
47
+ end
48
+ end
49
+
50
+ def define_status_label_method
51
+ model.send :define_method, :status_label do
52
+ self.class.statuses.label_for(status)
53
+ end
54
+ end
55
+
56
+ def set_validations
57
+ model.send :validates, :status, :presence => true, :inclusion => { :in => proc { model.statuses.values } }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,42 @@
1
+ module SimplestStatus
2
+ class Status
3
+ attr_reader :name, :value
4
+
5
+ def initialize(input)
6
+ @name, @value = Array(input)
7
+ end
8
+
9
+ def symbol
10
+ name.to_sym
11
+ end
12
+
13
+ def string
14
+ name.to_s
15
+ end
16
+ alias :to_s :string
17
+
18
+ def to_hash
19
+ { name => value }
20
+ end
21
+
22
+ def matches?(input)
23
+ [string, value.to_s].include? input.to_s
24
+ end
25
+
26
+ def constant_name
27
+ string.upcase
28
+ end
29
+
30
+ def label
31
+ string.split(/[\s_-]+/).map(&:capitalize).join(' ')
32
+ end
33
+
34
+ def for_select
35
+ [label, value]
36
+ end
37
+
38
+ def ==(status)
39
+ self.to_hash == status.to_hash
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ module SimplestStatus
2
+ autoload :Status, 'simplest_status/status'
3
+
4
+ class StatusCollection < Hash
5
+ def each
6
+ super do |status|
7
+ yield Status.new(status)
8
+ end
9
+ end
10
+
11
+ def [](status_name)
12
+ status_for(status_name).value
13
+ end
14
+ alias :value_for :[]
15
+
16
+ def status_for(input)
17
+ find do |status|
18
+ status.matches? input
19
+ end
20
+ end
21
+
22
+ def add(status, value = self.size)
23
+ self.merge!(status => value)
24
+ end
25
+
26
+ def label_for(value)
27
+ status_for(value).label
28
+ end
29
+
30
+ def for_select
31
+ map(&:for_select)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module SimplestStatus
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "simplest_status/version"
2
+
3
+ module SimplestStatus
4
+ autoload :StatusCollection, 'simplest_status/status_collection'
5
+ autoload :ModelMethods, 'simplest_status/model_methods'
6
+
7
+ def statuses(*status_list)
8
+ @statuses ||= status_list.reduce(StatusCollection.new) do |collection, status|
9
+ collection.add(status)
10
+ end
11
+
12
+ send(:include, ModelMethods) unless ancestors.include? ModelMethods
13
+
14
+ @statuses
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'simplest_status/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "simplest_status"
9
+ spec.version = SimplestStatus::VERSION
10
+ spec.authors = ["Ryan Stenberg"]
11
+ spec.email = ["ryan.stenberg@viget.com"]
12
+
13
+ spec.summary = "Simple status functionality for Rails models."
14
+ spec.description = "SimplestStatus provides a dead-simple DSL for defining statuses on Rails models, which generates scopes, constants, and a number of instance-level convenience methods."
15
+ spec.homepage = "https://github.com/vigetlabs/simplest_status"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = %w(lib)
22
+
23
+ spec.add_dependency "rails"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.2.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "codeclimate-test-reporter"
30
+ spec.add_development_dependency "appraisal"
31
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplest_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Stenberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
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
+ - !ruby/object:Gem::Dependency
98
+ name: appraisal
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: SimplestStatus provides a dead-simple DSL for defining statuses on Rails
112
+ models, which generates scopes, constants, and a number of instance-level convenience
113
+ methods.
114
+ email:
115
+ - ryan.stenberg@viget.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Appraisals
124
+ - Gemfile
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - gemfiles/rails_20.gemfile
130
+ - gemfiles/rails_31.gemfile
131
+ - gemfiles/rails_32.gemfile
132
+ - gemfiles/rails_40.gemfile
133
+ - gemfiles/rails_41.gemfile
134
+ - gemfiles/rails_42.gemfile
135
+ - lib/simplest_status.rb
136
+ - lib/simplest_status/model_methods.rb
137
+ - lib/simplest_status/status.rb
138
+ - lib/simplest_status/status_collection.rb
139
+ - lib/simplest_status/version.rb
140
+ - simplest_status.gemspec
141
+ homepage: https://github.com/vigetlabs/simplest_status
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.4.5
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Simple status functionality for Rails models.
165
+ test_files: []