rails3_ac_enum 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 ADDED
@@ -0,0 +1,18 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails3_ac_enum.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Terry Tai
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,67 @@
1
+ # Rails3AcEnum
2
+ The ActiveRecord::Base#enum is original created by DHH [in Rails
3
+ 4](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5#commitcomment-4496447) for declaring enum attributes where the values map to integers in the database, but can be queried by name.
4
+
5
+ If you want to use it in your Rails3 application. Please go and check out this gem.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rails3_ac_enum'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rails3_ac_enum
21
+
22
+ ## Usage
23
+
24
+ Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:
25
+
26
+ class Conversation < ActiveRecord::Base
27
+ enum status: [ :active, :archived ]
28
+ end
29
+
30
+ Conversation::STATUS # => { active: 0, archived: 1 }
31
+
32
+ # conversation.update! status: 0
33
+ conversation.active!
34
+ conversation.active? # => true
35
+ conversation.status # => :active
36
+
37
+ # conversation.update! status: 1
38
+ conversation.archived!
39
+ conversation.archived? # => true
40
+ conversation.status # => :archived
41
+
42
+ # conversation.update! status: 1
43
+ conversation.status = :archived
44
+
45
+ You can set the default value from the database declaration, like:
46
+
47
+ create_table :conversations do |t|
48
+ t.column :status, :integer, default: 0
49
+ end
50
+
51
+ Good practice is to let the first declared status be the default.
52
+
53
+ Finally, it's also possible to explicitly map the relation between attribute and database integer:
54
+
55
+ class Conversation < ActiveRecord::Base
56
+ enum status: { active: 0, archived: 1 }
57
+ end
58
+
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
67
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ require "rails3_ac_enum/version"
2
+ require "rails3_ac_enum/enum"
3
+
4
+ module ActiveRecord
5
+ autoload :Enum
6
+ class Base
7
+ extend Enum
8
+ end
9
+ end
@@ -0,0 +1,83 @@
1
+ module ActiveRecord
2
+ # This is a work around to make ActiveRecord::Base#enum in Rails master works for Rails3
3
+ #
4
+ # Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:
5
+ #
6
+ # class Conversation < ActiveRecord::Base
7
+ # enum status: [ :active, :archived ]
8
+ # end
9
+ #
10
+ # Conversation::STATUS # => { active: 0, archived: 1 }
11
+ #
12
+ # # conversation.update! status: 0
13
+ # conversation.active!
14
+ # conversation.active? # => true
15
+ # conversation.status # => :active
16
+ #
17
+ # # conversation.update! status: 1
18
+ # conversation.archived!
19
+ # conversation.archived? # => true
20
+ # conversation.status # => :archived
21
+ #
22
+ # # conversation.update! status: 1
23
+ # conversation.status = :archived
24
+ #
25
+ # You can set the default value from the database declaration, like:
26
+ #
27
+ # create_table :conversations do |t|
28
+ # t.column :status, :integer, default: 0
29
+ # end
30
+ #
31
+ # Good practice is to let the first declared status be the default.
32
+ #
33
+ # Finally, it's also possible to explicitly map the relation between attribute and database integer:
34
+ #
35
+ # class Conversation < ActiveRecord::Base
36
+ # enum status: { active: 0, archived: 1 }
37
+ # end
38
+ module Enum
39
+ def enum(definitions)
40
+ klass = self
41
+ definitions.each do |name, values|
42
+ enum_values = {}
43
+ name = name.to_sym
44
+
45
+ _enum_methods_module.module_eval do
46
+ # def direction=(value) self[:direction] = DIRECTION[value] end
47
+ define_method("#{name}=") { |value|
48
+ value = value.to_s
49
+ unless enum_values.has_key?(value)
50
+ raise ArgumentError, "'#{value}' is not a valid #{name}"
51
+ end
52
+ self[name] = enum_values[value]
53
+ }
54
+
55
+ # def direction() DIRECTION.key self[:direction] end
56
+ define_method(name) { enum_values.key self[name] }
57
+
58
+ pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
59
+ pairs.each do |value, i|
60
+ enum_values[value.to_s] = i
61
+
62
+ # scope :incoming, -> { where direction: 0 }
63
+ klass.scope value, -> { klass.where name => i }
64
+
65
+ # def incoming?() direction == 0 end
66
+ define_method("#{value}?") { self[name] == i }
67
+
68
+ # def incoming! update! direction: :incoming end
69
+ define_method("#{value}!") { update_attributes! name => value.to_sym }
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ def _enum_methods_module
76
+ @_enum_methods_module ||= begin
77
+ mod = Module.new
78
+ include mod
79
+ mod
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Rails3AcEnum
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails3_ac_enum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails3_ac_enum"
8
+ spec.version = Rails3AcEnum::VERSION
9
+ spec.authors = ["Terry Tai"]
10
+ spec.email = ["poshboytl@gmail.com"]
11
+ spec.description = %q{Let ActiveRecord::Base#enum work for Rails 3}
12
+ spec.summary = %q{Love the ActiveRecord::Base#enum https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5#commitcomment-4496447.So want use it in Rails 3.}
13
+ spec.homepage = "http://terrytai.com"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails3_ac_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Terry Tai
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Let ActiveRecord::Base#enum work for Rails 3
47
+ email:
48
+ - poshboytl@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/rails3_ac_enum.rb
59
+ - lib/rails3_ac_enum/enum.rb
60
+ - lib/rails3_ac_enum/version.rb
61
+ - rails3_ac_enum.gemspec
62
+ homepage: http://terrytai.com
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Love the ActiveRecord::Base#enum https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5#commitcomment-4496447.So
87
+ want use it in Rails 3.
88
+ test_files: []
89
+ has_rdoc: