enum_plus 0.1.0 → 0.2.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.
- data/.gitignore +2 -1
- data/Gemfile +3 -0
- data/README.md +1 -1
- data/Rakefile +8 -0
- data/enum_plus.gemspec +15 -0
- data/init.rb +1 -0
- data/lib/enum_plus/version.rb +3 -0
- data/lib/enum_plus.rb +53 -0
- metadata +7 -1
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/README.md
CHANGED
data/Rakefile
ADDED
data/enum_plus.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../lib/enum_plus/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'enum_plus'
|
5
|
+
s.version = EnumPlus::VERSION
|
6
|
+
s.authors = ['caedes']
|
7
|
+
s.email = ['laurentromain@gmail.com']
|
8
|
+
s.homepage = 'https://github.com/caedes/enum_plus'
|
9
|
+
s.summary = 'Add enumeration to your ruby classes'
|
10
|
+
s.description = s.summary
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
|
14
|
+
s.require_path = 'lib'
|
15
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'enum_plus'
|
data/lib/enum_plus.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'enum_plus/version'
|
2
|
+
|
3
|
+
unless String.methods.include? 'underscore'
|
4
|
+
class String
|
5
|
+
def underscore
|
6
|
+
self.gsub(/::/, '/').
|
7
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
8
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
9
|
+
tr("-", "_").
|
10
|
+
downcase
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Object
|
16
|
+
def self.enum name, enumeration
|
17
|
+
name = name.to_s
|
18
|
+
class_eval "
|
19
|
+
def self.#{name}_values
|
20
|
+
#{enumeration}
|
21
|
+
end
|
22
|
+
def self.#{name}_captions
|
23
|
+
#{enumeration}.map do |value|
|
24
|
+
humanize_#{name} value
|
25
|
+
end
|
26
|
+
end"
|
27
|
+
enumeration.each do |value|
|
28
|
+
value = value.to_s
|
29
|
+
eval "
|
30
|
+
def #{value}?
|
31
|
+
self.#{name} == '#{value}'
|
32
|
+
end
|
33
|
+
def #{value}!
|
34
|
+
self.#{name} = '#{value}'
|
35
|
+
end"
|
36
|
+
end
|
37
|
+
eval "
|
38
|
+
def humanize_#{name} value
|
39
|
+
value = value.to_s
|
40
|
+
begin
|
41
|
+
Required::Module::const_get 'I18n'
|
42
|
+
::I18n.t \"enum.#{self.to_s.underscore}.#{name}.#{'#{value}'}\"
|
43
|
+
rescue NameError
|
44
|
+
value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
def #{name}_caption
|
48
|
+
humanize_#{name} self.#{name}
|
49
|
+
end"
|
50
|
+
|
51
|
+
validates_inclusion_of name.to_sym, { allow_nil: true, in: enumeration } if self.ancestors.include? 'ActiveRecord::Base'
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enum_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,7 +19,13 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- Gemfile
|
22
23
|
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- enum_plus.gemspec
|
26
|
+
- init.rb
|
27
|
+
- lib/enum_plus.rb
|
28
|
+
- lib/enum_plus/version.rb
|
23
29
|
homepage: https://github.com/caedes/enum_plus
|
24
30
|
licenses: []
|
25
31
|
post_install_message:
|