has_enum 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Rakefile +0 -4
- data/VERSION +1 -1
- data/has_enum.gemspec +5 -17
- data/lib/has_enum/active_record.rb +28 -47
- metadata +23 -83
data/Gemfile
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
3
|
gem "rails", "~> 3.0.0"
|
4
|
-
gem "sqlite3-ruby", "~> 1.3.2"
|
5
4
|
|
6
5
|
# Add dependencies to develop your gem here.
|
7
6
|
# Include everything needed to run rake, tests, features, etc.
|
@@ -10,5 +9,6 @@ group :development do
|
|
10
9
|
gem "rails", "~> 3.0.0"
|
11
10
|
gem "bundler", "~> 1.0.0"
|
12
11
|
gem "jeweler", "~> 1.5.1"
|
12
|
+
gem "sqlite3-ruby", "~> 1.3.2"
|
13
13
|
gem "rcov", ">= 0"
|
14
14
|
end
|
data/Rakefile
CHANGED
@@ -18,10 +18,6 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.summary = %Q{Gem for Rails to easily handle enumeration attributes in models}
|
19
19
|
gem.email = "kes.eclipse@gmail.com"
|
20
20
|
gem.authors = ["Andreas Korth", "Konstantin Shabanov"]
|
21
|
-
gem.add_runtime_dependency 'rails', '> 3.0.0'
|
22
|
-
gem.add_development_dependency 'rspec', '> 2.2.0'
|
23
|
-
gem.add_development_dependency 'rails', '> 3.0.0'
|
24
|
-
gem.add_development_dependency 'sqlite3-ruby', '> 1.3.2'
|
25
21
|
end
|
26
22
|
Jeweler::RubygemsDotOrgTasks.new
|
27
23
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/has_enum.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_enum}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andreas Korth", "Konstantin Shabanov"]
|
12
|
-
s.date = %q{2010-12-
|
12
|
+
s.date = %q{2010-12-10}
|
13
13
|
s.email = %q{kes.eclipse@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
@@ -51,41 +51,29 @@ Gem::Specification.new do |s|
|
|
51
51
|
|
52
52
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
53
|
s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
|
54
|
-
s.add_runtime_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
55
54
|
s.add_development_dependency(%q<rspec>, ["~> 2.2.0"])
|
56
55
|
s.add_development_dependency(%q<rails>, ["~> 3.0.0"])
|
57
56
|
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
58
57
|
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
58
|
+
s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
59
59
|
s.add_development_dependency(%q<rcov>, [">= 0"])
|
60
|
-
s.add_runtime_dependency(%q<rails>, ["> 3.0.0"])
|
61
|
-
s.add_development_dependency(%q<rspec>, ["> 2.2.0"])
|
62
|
-
s.add_development_dependency(%q<rails>, ["> 3.0.0"])
|
63
|
-
s.add_development_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
|
64
60
|
else
|
65
61
|
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
66
|
-
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
67
62
|
s.add_dependency(%q<rspec>, ["~> 2.2.0"])
|
68
63
|
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
69
64
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
70
65
|
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
66
|
+
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
71
67
|
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
-
s.add_dependency(%q<rails>, ["> 3.0.0"])
|
73
|
-
s.add_dependency(%q<rspec>, ["> 2.2.0"])
|
74
|
-
s.add_dependency(%q<rails>, ["> 3.0.0"])
|
75
|
-
s.add_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
|
76
68
|
end
|
77
69
|
else
|
78
70
|
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
79
|
-
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
80
71
|
s.add_dependency(%q<rspec>, ["~> 2.2.0"])
|
81
72
|
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
82
73
|
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
83
74
|
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
75
|
+
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
84
76
|
s.add_dependency(%q<rcov>, [">= 0"])
|
85
|
-
s.add_dependency(%q<rails>, ["> 3.0.0"])
|
86
|
-
s.add_dependency(%q<rspec>, ["> 2.2.0"])
|
87
|
-
s.add_dependency(%q<rails>, ["> 3.0.0"])
|
88
|
-
s.add_dependency(%q<sqlite3-ruby>, ["> 1.3.2"])
|
89
77
|
end
|
90
78
|
end
|
91
79
|
|
@@ -11,57 +11,38 @@ module HasEnum
|
|
11
11
|
attribute ? @enum[attribute.to_sym] : @enum
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
def has_enum(attribute, values, options = {})
|
15
|
+
enum[attribute] = values.freeze
|
16
|
+
|
17
|
+
if options[:query_methods] != false
|
18
|
+
values.each do |val|
|
19
|
+
define_method(:"#{val}?") { self.send(attribute) == val }
|
20
20
|
end
|
21
|
-
|
22
|
-
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method(:"#{attribute}=") do |value|
|
24
|
+
if value.nil? or values.find{ |val| val == value }
|
25
|
+
set_value = lambda { |value| value.is_a?(Symbol) ? Marshal.dump(value) : value }
|
26
|
+
self[:"#{attribute}"] = value.nil? ? nil : set_value.call(value)
|
23
27
|
else
|
24
|
-
|
25
|
-
end
|
26
|
-
enum[attribute] = values.freeze
|
27
|
-
|
28
|
-
if options[:query_methods] != false
|
29
|
-
enum[attribute].each do |val|
|
30
|
-
val = val[1..-1] if symbols
|
31
|
-
define_method(:"#{val}?") do
|
32
|
-
self.send(attribute).to_s == val
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
define_method(:"#{attribute}=") do |value|
|
38
|
-
value = value.to_s.insert(0, ':') if symbols and value
|
39
|
-
|
40
|
-
if value.nil? or values.find{ |val| val == value }
|
41
|
-
write_attribute(attribute, value.blank? ? nil : value.to_s)
|
42
|
-
else
|
43
|
-
errors.add(:"#{attribute}", "#{value} is not in enum")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
define_method(:"#{attribute}") do
|
48
|
-
if symbols and read_attribute(attribute.to_sym)
|
49
|
-
read_attribute(attribute.to_sym).tap{|str| str.slice!(0)}.to_sym
|
50
|
-
else
|
51
|
-
read_attribute(attribute.to_sym)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
define_method "human_#{attribute}" do
|
56
|
-
begin
|
57
|
-
return nil unless self.send(attribute)
|
58
|
-
key = "activerecord.attributes.#{self.class.name.underscore}.#{attribute}_enum.#{self.send(attribute)}"
|
59
|
-
translation = I18n.translate(key, :raise => true)
|
60
|
-
rescue I18n::MissingTranslationData
|
61
|
-
self.send(attribute).humanize
|
62
|
-
end
|
28
|
+
errors.add(:"#{attribute}", "#{value} is not in enum")
|
63
29
|
end
|
30
|
+
end
|
64
31
|
|
32
|
+
define_method(:"#{attribute}") do
|
33
|
+
load_value = lambda { |value| value[0].getbyte(0) == 4 ? Marshal.load(value) : value }
|
34
|
+
value = self[:"#{attribute}"]
|
35
|
+
value ? load_value.call(value) : nil
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method "human_#{attribute}" do
|
39
|
+
begin
|
40
|
+
return nil unless self.send(attribute)
|
41
|
+
key = "activerecord.attributes.#{self.class.name.underscore}.#{attribute}_enum.#{self.send(attribute)}"
|
42
|
+
translation = I18n.translate(key, :raise => true)
|
43
|
+
rescue I18n::MissingTranslationData
|
44
|
+
self.send(attribute).humanize
|
45
|
+
end
|
65
46
|
end
|
66
47
|
end
|
67
48
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 4
|
9
|
+
version: 0.3.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andreas Korth
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-10 00:00:00 +06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -33,24 +33,9 @@ dependencies:
|
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
35
|
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: sqlite3-ruby
|
38
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
segments:
|
44
|
-
- 1
|
45
|
-
- 3
|
46
|
-
- 2
|
47
|
-
version: 1.3.2
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: *id002
|
51
36
|
- !ruby/object:Gem::Dependency
|
52
37
|
name: rspec
|
53
|
-
requirement: &
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
54
39
|
none: false
|
55
40
|
requirements:
|
56
41
|
- - ~>
|
@@ -62,10 +47,10 @@ dependencies:
|
|
62
47
|
version: 2.2.0
|
63
48
|
type: :development
|
64
49
|
prerelease: false
|
65
|
-
version_requirements: *
|
50
|
+
version_requirements: *id002
|
66
51
|
- !ruby/object:Gem::Dependency
|
67
52
|
name: rails
|
68
|
-
requirement: &
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
69
54
|
none: false
|
70
55
|
requirements:
|
71
56
|
- - ~>
|
@@ -77,10 +62,10 @@ dependencies:
|
|
77
62
|
version: 3.0.0
|
78
63
|
type: :development
|
79
64
|
prerelease: false
|
80
|
-
version_requirements: *
|
65
|
+
version_requirements: *id003
|
81
66
|
- !ruby/object:Gem::Dependency
|
82
67
|
name: bundler
|
83
|
-
requirement: &
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
84
69
|
none: false
|
85
70
|
requirements:
|
86
71
|
- - ~>
|
@@ -92,10 +77,10 @@ dependencies:
|
|
92
77
|
version: 1.0.0
|
93
78
|
type: :development
|
94
79
|
prerelease: false
|
95
|
-
version_requirements: *
|
80
|
+
version_requirements: *id004
|
96
81
|
- !ruby/object:Gem::Dependency
|
97
82
|
name: jeweler
|
98
|
-
requirement: &
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
99
84
|
none: false
|
100
85
|
requirements:
|
101
86
|
- - ~>
|
@@ -107,80 +92,35 @@ dependencies:
|
|
107
92
|
version: 1.5.1
|
108
93
|
type: :development
|
109
94
|
prerelease: false
|
110
|
-
version_requirements: *
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: rcov
|
113
|
-
requirement: &id007 !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
|
-
requirements:
|
116
|
-
- - ">="
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
segments:
|
119
|
-
- 0
|
120
|
-
version: "0"
|
121
|
-
type: :development
|
122
|
-
prerelease: false
|
123
|
-
version_requirements: *id007
|
95
|
+
version_requirements: *id005
|
124
96
|
- !ruby/object:Gem::Dependency
|
125
|
-
name:
|
126
|
-
requirement: &
|
97
|
+
name: sqlite3-ruby
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
127
99
|
none: false
|
128
100
|
requirements:
|
129
|
-
- -
|
101
|
+
- - ~>
|
130
102
|
- !ruby/object:Gem::Version
|
131
103
|
segments:
|
104
|
+
- 1
|
132
105
|
- 3
|
133
|
-
- 0
|
134
|
-
- 0
|
135
|
-
version: 3.0.0
|
136
|
-
type: :runtime
|
137
|
-
prerelease: false
|
138
|
-
version_requirements: *id008
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: rspec
|
141
|
-
requirement: &id009 !ruby/object:Gem::Requirement
|
142
|
-
none: false
|
143
|
-
requirements:
|
144
|
-
- - ">"
|
145
|
-
- !ruby/object:Gem::Version
|
146
|
-
segments:
|
147
106
|
- 2
|
148
|
-
|
149
|
-
- 0
|
150
|
-
version: 2.2.0
|
107
|
+
version: 1.3.2
|
151
108
|
type: :development
|
152
109
|
prerelease: false
|
153
|
-
version_requirements: *
|
110
|
+
version_requirements: *id006
|
154
111
|
- !ruby/object:Gem::Dependency
|
155
|
-
name:
|
156
|
-
requirement: &
|
112
|
+
name: rcov
|
113
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
157
114
|
none: false
|
158
115
|
requirements:
|
159
|
-
- - "
|
116
|
+
- - ">="
|
160
117
|
- !ruby/object:Gem::Version
|
161
118
|
segments:
|
162
|
-
- 3
|
163
|
-
- 0
|
164
119
|
- 0
|
165
|
-
version:
|
166
|
-
type: :development
|
167
|
-
prerelease: false
|
168
|
-
version_requirements: *id010
|
169
|
-
- !ruby/object:Gem::Dependency
|
170
|
-
name: sqlite3-ruby
|
171
|
-
requirement: &id011 !ruby/object:Gem::Requirement
|
172
|
-
none: false
|
173
|
-
requirements:
|
174
|
-
- - ">"
|
175
|
-
- !ruby/object:Gem::Version
|
176
|
-
segments:
|
177
|
-
- 1
|
178
|
-
- 3
|
179
|
-
- 2
|
180
|
-
version: 1.3.2
|
120
|
+
version: "0"
|
181
121
|
type: :development
|
182
122
|
prerelease: false
|
183
|
-
version_requirements: *
|
123
|
+
version_requirements: *id007
|
184
124
|
description:
|
185
125
|
email: kes.eclipse@gmail.com
|
186
126
|
executables: []
|
@@ -221,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
221
161
|
requirements:
|
222
162
|
- - ">="
|
223
163
|
- !ruby/object:Gem::Version
|
224
|
-
hash:
|
164
|
+
hash: -4461255227335299909
|
225
165
|
segments:
|
226
166
|
- 0
|
227
167
|
version: "0"
|