activerecord-enum-without-methods 1.0.0
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.
- checksums.yaml +7 -0
- data/README.md +20 -0
- data/activerecord-enum-without-methods.gemspec +14 -0
- data/lib/activerecord-enum-without-methods.rb +59 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a05c8c7ea5fb8d28c5815f0ffa1e8979f1feb7b8
|
4
|
+
data.tar.gz: 192873ae285fb6585263dd4199ad33ecfbe0ed69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87c7eacc15a62686ad74bffcb1548604b2eade2f3b1e2e2d113ce3ecedded04f128a5fbb3dcdb403b0a482232516f91d1a4218e8ff3595e1ae5dc2735460cade
|
7
|
+
data.tar.gz: 6ce56534a17f88d8c7bcb17053b6f04c7324a502f008c313a6165f5fca9f9d6208db326d033cb132206e4301cc5c21a97ce3b36746ca7515c2787ac89d26d0d1
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
activerecord-enum-without-methods
|
2
|
+
=================================
|
3
|
+
|
4
|
+
This gem does the same as ActiveRecord::Base#enum in Rails >= 4.1 but does not define enum_value? and enum_value!
|
5
|
+
methods so you can use the same value in multiple enums.
|
6
|
+
|
7
|
+
It's a solution if you get the following error:
|
8
|
+
|
9
|
+
`ArgumentError: You tried to define an enum named "..." on the model "...", but this will generate a instance method "...?", which is already defined by another enum.`
|
10
|
+
|
11
|
+
# Usage #
|
12
|
+
|
13
|
+
class MyModel < ActiveRecord::Base
|
14
|
+
enum_without_methods :enum1, [ :a, :b ]
|
15
|
+
enum_without_methods :enum2, [ :a, :b ]
|
16
|
+
end
|
17
|
+
|
18
|
+
# License #
|
19
|
+
|
20
|
+
MIT
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{activerecord-enum-without-methods}
|
3
|
+
s.version = "1.0.0"
|
4
|
+
s.authors = ["Marcin Lewandowski"]
|
5
|
+
s.description = %q{This gem does the same as ActiveRecord::Base#enum but does not define enum_value? and enum_value! methods so you can use the same values in multiple enum}
|
6
|
+
s.email = %q{marcin@saepia.net}
|
7
|
+
s.files = ["lib/activerecord-enum-without-methods.rb", "README.md", "activerecord-enum-without-methods.gemspec"]
|
8
|
+
s.homepage = %q{https://github.com/mspanc/activerecord-enum-without-methods}
|
9
|
+
s.require_paths = ["lib"]
|
10
|
+
s.summary = %q{Define enums (Rails 4.1-style) but without value? and value! methods}
|
11
|
+
s.licenses = ['MIT']
|
12
|
+
|
13
|
+
s.add_runtime_dependency 'activerecord', '~> 4.1'
|
14
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module EnumWithoutMethods
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
# Based on original enum(), just removed some code
|
9
|
+
# File activerecord/lib/active_record/enum.rb, line 82
|
10
|
+
def enum_without_methods(definitions)
|
11
|
+
klass = self
|
12
|
+
definitions.each do |name, values|
|
13
|
+
# statuses = { }
|
14
|
+
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
15
|
+
name = name.to_sym
|
16
|
+
|
17
|
+
# def self.statuses statuses end
|
18
|
+
detect_enum_conflict!(name, name.to_s.pluralize, true)
|
19
|
+
klass.singleton_class.send(:define_method, name.to_s.pluralize) { enum_values }
|
20
|
+
|
21
|
+
_enum_methods_module.module_eval do
|
22
|
+
# def status=(value) self[:status] = statuses[value] end
|
23
|
+
klass.send(:detect_enum_conflict!, name, "#{name}=")
|
24
|
+
define_method("#{name}=") { |value|
|
25
|
+
if enum_values.has_key?(value) || value.blank?
|
26
|
+
self[name] = enum_values[value]
|
27
|
+
elsif enum_values.has_value?(value)
|
28
|
+
# Assigning a value directly is not a end-user feature, hence it's not documented.
|
29
|
+
# This is used internally to make building objects from the generated scopes work
|
30
|
+
# as expected, i.e. +Conversation.archived.build.archived?+ should be true.
|
31
|
+
self[name] = value
|
32
|
+
else
|
33
|
+
raise ArgumentError, "'#{value}' is not a valid #{name}"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
# def status() statuses.key self[:status] end
|
38
|
+
klass.send(:detect_enum_conflict!, name, name)
|
39
|
+
define_method(name) { enum_values.key self[name] }
|
40
|
+
|
41
|
+
# def status_before_type_cast() statuses.key self[:status] end
|
42
|
+
klass.send(:detect_enum_conflict!, name, "#{name}_before_type_cast")
|
43
|
+
define_method("#{name}_before_type_cast") { enum_values.key self[name] }
|
44
|
+
|
45
|
+
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
46
|
+
pairs.each do |value, i|
|
47
|
+
enum_values[value] = i
|
48
|
+
end
|
49
|
+
end
|
50
|
+
defined_enums[name.to_s] = enum_values
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class ActiveRecord::Base
|
58
|
+
include ActiveRecord::EnumWithoutMethods
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-enum-without-methods
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Lewandowski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-18 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.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.1'
|
27
|
+
description: This gem does the same as ActiveRecord::Base#enum but does not define
|
28
|
+
enum_value? and enum_value! methods so you can use the same values in multiple enum
|
29
|
+
email: marcin@saepia.net
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- README.md
|
35
|
+
- activerecord-enum-without-methods.gemspec
|
36
|
+
- lib/activerecord-enum-without-methods.rb
|
37
|
+
homepage: https://github.com/mspanc/activerecord-enum-without-methods
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 2.2.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Define enums (Rails 4.1-style) but without value? and value! methods
|
61
|
+
test_files: []
|