enumerated_attr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/enumerated_attr/version.rb +3 -0
- data/lib/enumerated_attr.rb +41 -0
- metadata +100 -0
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
module EnumeratedAttr
|
5
|
+
module Base
|
6
|
+
def self.human(klass, attribute, value)
|
7
|
+
klass_name = klass.name.underscore
|
8
|
+
I18n.t(:"enumerations.#{klass_name}.#{attribute}.#{value}", :default => value.to_s.humanize)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.enumeration(klass, attribute, enumeration)
|
12
|
+
enumeration.map do |value|
|
13
|
+
[value, self.human(klass, attribute, value)]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module Glue
|
19
|
+
def enumerated_attr(attribute, enumeration, options = {})
|
20
|
+
define_method "human_#{attribute}_value" do |value = nil|
|
21
|
+
Base.human(self.class, attribute, value || self.send(attribute))
|
22
|
+
end
|
23
|
+
|
24
|
+
define_method "#{attribute}_enumeration" do
|
25
|
+
Base.enumeration(self.class, attribute, enumeration)
|
26
|
+
end
|
27
|
+
|
28
|
+
add_validations(attribute, enumeration) if options[:validates]
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def add_validations(attribute, enumeration)
|
34
|
+
validates(attribute, :presence => true, :inclusion => enumeration)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.included(base)
|
39
|
+
base.extend Glue
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerated_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lenon Marcel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A very simple enumeration gem
|
63
|
+
email: lenon.marcel@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- lib/enumerated_attr.rb
|
69
|
+
- lib/enumerated_attr/version.rb
|
70
|
+
homepage: http://rubygems.org/gems/enumerated_attr
|
71
|
+
licenses: []
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
hash: -2450379149475425090
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
hash: -2450379149475425090
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.8.24
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: EnumeratedAttr
|
100
|
+
test_files: []
|