active_record_enumerated_type 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzgwY2UwNzY2OTIxZDhiNGI1MjQwNzkwYTI5Mjk1ZTM3YWM4YzFjMQ==
5
+ data.tar.gz: !binary |-
6
+ OGE0NzFjYzBhNzAyZWQ0YzJlNjA3NTdlNzkwMzJkYmViOWY1Y2Y5Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjhkODUwMTAyZjg4NWZjMDZkMzAzODUyMzUyOGJhNTJhYzNhNjJkYmE0YmNk
10
+ ZTU0ZjM5MGU4ZTkzNzNjNTZiOGQ1NmZhZjA1ZjQ4M2JkMGIyYmE1YmMwMGUw
11
+ OWJmZDFiYWY2YjEwNjZkYmMxYjU0ZTBmMzAxOGViZTg2YmVkNzY=
12
+ data.tar.gz: !binary |-
13
+ NWJhZGNkZGNjZGNiNmQ5ZmYwNTJhYjI1Yzk5NGEwZWVkMmZjZDU5MWI4MDJk
14
+ NGUwMmI5OGUwZjdmMzcwNjU5YTljYjA5OTZmMmFlNDNkZmRhNjgxMGY4NTEz
15
+ ODg1Njk5NmNmYWU0NzhiZjNiMDFkNDNhMGNlMmQ4YzJmYzIyOGQ=
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record_enumerated_type.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ben Eddy
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,90 @@
1
+ # ActiveRecord Enumerated Type
2
+
3
+ Integrates [EnumeratedType](https://github.com/rafer/enumerated_type) with ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_record_enumerated_type'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_record_enumerated_type
18
+
19
+ ## Usage
20
+
21
+ __1. Create a formal type__
22
+
23
+ For example, per the [EnumeratedType](https://github.com/rafer/enumerated_type) documentation, a `JobStatus`:
24
+
25
+ ```Ruby
26
+ class JobStatus
27
+ include EnumeratedType
28
+
29
+ declare :started
30
+ declare :finished
31
+ end
32
+ ```
33
+
34
+ __2. Restrict an ActiveRecord attribute to the type.__
35
+
36
+ For example, assuming a `Job` class with a `status` attribute:
37
+
38
+ ```Ruby
39
+ class Job < ActiveRecord::Base
40
+ restrict_type_of :status, to: JobStatus
41
+ end
42
+ ```
43
+
44
+ This allows one to set attributes symbolically or as formal types.
45
+
46
+ ```Ruby
47
+ job = Job.new(status: JobStatus[:started])
48
+ job.status # => JobStatus[:started]
49
+
50
+ job = Job.new(status: :started)
51
+ job.status # => JobStatus[:started]
52
+
53
+ job = Job.new(status: nil)
54
+ job.status # => nil
55
+ ```
56
+
57
+ Setting an invalid type raises an exception.
58
+
59
+ ```Ruby
60
+ job = Job.new(status: :pending)
61
+ # => "'pending' is not a valid type for status. Valid types include 'started' and 'finished'."
62
+ ```
63
+
64
+ ### I18n support
65
+ Type names can be translated with I18n.
66
+
67
+ ```YML
68
+ en:
69
+ enumerated_type:
70
+ job_status:
71
+ finished: Finito
72
+ ```
73
+
74
+ ```Ruby
75
+ job = Job.new(status: :finished)
76
+ job.status.human
77
+ # => 'Finito'
78
+
79
+ job = Job.new(status: :started)
80
+ job.status.human
81
+ # => 'started'
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ 1. Fork it
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
89
+ 4. Push to the branch (`git push origin my-new-feature`)
90
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_enumerated_type/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_enumerated_type"
8
+ spec.version = ActiveRecordEnumeratedType::VERSION
9
+ spec.authors = ["Ben Eddy"]
10
+ spec.email = ["bae@foraker.com"]
11
+ spec.description = %q{Enumerated types for ActiveRecord attributes}
12
+ spec.summary = %q{Enumerated types for ActiveRecord attributes}
13
+ spec.homepage = "https://github.com/foraker/active_record_enumerated_type"
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 "activesupport"
22
+ spec.add_dependency "i18n"
23
+ spec.add_dependency "enumerated_type"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ end
@@ -0,0 +1,7 @@
1
+ require "active_record_enumerated_type/version"
2
+ require "active_record_enumerated_type/enumerated_type"
3
+ require "active_record_enumerated_type/active_record/type_restriction"
4
+ require "active_record_enumerated_type/rails"
5
+
6
+ module ActiveRecordEnumeratedType
7
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveRecord
2
+ module TypeRestriction
3
+ def self.included(base)
4
+ base.send(:extend, ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def restrict_type_of(attribute, options)
9
+ type_class = options.fetch(:to)
10
+
11
+ define_method(attribute) do
12
+ super() && type_class[super().to_sym]
13
+ end
14
+
15
+ define_method(:"#{attribute}=") do |value|
16
+ begin
17
+ super(value.presence && type_class.coerce(value).serialize)
18
+ rescue ArgumentError
19
+ valid_types = type_class.map { |type| "'#{type}'" }.to_sentence
20
+ raise TypeError, "'#{value}' is not a valid type for #{attribute}. Valid types include #{valid_types}."
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ module EnumeratedType
2
+ def serialize
3
+ name
4
+ end
5
+
6
+ def human
7
+ I18n.translate!(name, scope: [:enumerated_type, :status])
8
+ rescue I18n::MissingTranslationData
9
+ to_s.titleize
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ if defined?(ActiveRecord::Base)
2
+ ActiveRecord::Base.send(:include, ActiveRecord::TypeRestriction)
3
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordEnumeratedType
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe EnumeratedType do
4
+ describe "I18n" do
5
+ it "returns an internationalized name" do
6
+ allow(I18n).to receive(:translate!)
7
+ .with(:finished, scope: [:enumerated_type, :status])
8
+ .and_return("finito")
9
+
10
+ expect(Status[:finished].human).to eq "finito"
11
+ end
12
+
13
+ it "defaulst to the name" do
14
+ expect(Status[:finished].human).to eq "Finished"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'active_support/core_ext'
5
+ require 'enumerated_type'
6
+ require 'active_record_enumerated_type'
7
+
8
+ class Status
9
+ include EnumeratedType
10
+
11
+ declare :started
12
+ declare :finished
13
+ end
14
+
15
+ RSpec.configure do |config|
16
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ class TestRecord
4
+ include ActiveRecord::TypeRestriction
5
+
6
+ module InheritedAttributes
7
+ def status
8
+ @status
9
+ end
10
+
11
+ def status=(value)
12
+ @status = value
13
+ end
14
+ end
15
+ include InheritedAttributes
16
+
17
+ restrict_type_of :status, to: Status
18
+ end
19
+
20
+ describe TestRecord do
21
+ describe "setting attributes" do
22
+ let(:instance) { described_class.new }
23
+
24
+ it "accepts a formal type" do
25
+ instance.status = Status[:started]
26
+ expect(instance.status).to eq Status[:started]
27
+ end
28
+
29
+ it "accepts a symbolic type" do
30
+ instance.status = :started
31
+ expect(instance.status).to eq Status[:started]
32
+ end
33
+
34
+ it "accepts a string type" do
35
+ instance.status = 'started'
36
+ expect(instance.status).to eq Status[:started]
37
+ end
38
+
39
+ it "allows type resetting" do
40
+ instance.status = Status[:started]
41
+ instance.status = Status[:finished]
42
+ expect(instance.status).to eq Status[:finished]
43
+ end
44
+
45
+ it "allows nil" do
46
+ instance.status = nil
47
+ expect(instance.status).to be_nil
48
+ end
49
+
50
+ it "raises an exception when types are invalid" do
51
+ error_message = "'pending' is not a valid type for status. Valid types include 'started' and 'finished'."
52
+
53
+ expect {
54
+ instance.status = :pending
55
+ }.to raise_error(TypeError, error_message)
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_enumerated_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Eddy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: enumerated_type
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
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: rspec
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: Enumerated types for ActiveRecord attributes
98
+ email:
99
+ - bae@foraker.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - active_record_enumerated_type.gemspec
110
+ - lib/active_record_enumerated_type.rb
111
+ - lib/active_record_enumerated_type/active_record/type_restriction.rb
112
+ - lib/active_record_enumerated_type/enumerated_type.rb
113
+ - lib/active_record_enumerated_type/rails.rb
114
+ - lib/active_record_enumerated_type/version.rb
115
+ - spec/enumerated_type_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/type_restriction_spec.rb
118
+ homepage: https://github.com/foraker/active_record_enumerated_type
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.0.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Enumerated types for ActiveRecord attributes
142
+ test_files:
143
+ - spec/enumerated_type_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/type_restriction_spec.rb
146
+ has_rdoc: