platanus 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/platanus/enum.rb +78 -0
- data/lib/platanus/version.rb +1 -1
- metadata +2 -1
@@ -0,0 +1,78 @@
|
|
1
|
+
# enum.rb : ActiveRecord Enumerated Attributes.
|
2
|
+
#
|
3
|
+
# Copyright August 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
|
4
|
+
|
5
|
+
module Platanus
|
6
|
+
|
7
|
+
## Adds +attr_enum+ property generator to a module.
|
8
|
+
#
|
9
|
+
# When attr_enum is called on one of the model properties name:
|
10
|
+
# * A getter and setter for the <name>_str property are added, this allows the property to be accessed as a string,
|
11
|
+
# the string representations are obtained from the enumeration module's constants ::downcased:: names.
|
12
|
+
# * An inclusion validation is added for the property, only values included in the enumeration module's constants are allowed
|
13
|
+
#
|
14
|
+
# Given the following configuration:
|
15
|
+
#
|
16
|
+
# module Test
|
17
|
+
# ONE = 1
|
18
|
+
# TWO = 2
|
19
|
+
# THREE = 3
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# class Model
|
23
|
+
# include Platanus::Enum
|
24
|
+
#
|
25
|
+
# attr_enum :target, Test
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# One could do:
|
29
|
+
#
|
30
|
+
# t = Model.new
|
31
|
+
# t.target = Test.ONE
|
32
|
+
# t.target_str = 'one' # Same as above
|
33
|
+
# t.target = 5 # Generates a validation error
|
34
|
+
# t.target_str = # Raises an InvalidEnumName exception
|
35
|
+
#
|
36
|
+
module Enum
|
37
|
+
|
38
|
+
# Exception risen when an invalid value is passed to one of the _str= setters.
|
39
|
+
class InvalidEnumName < Exception; end
|
40
|
+
|
41
|
+
def self.included(base)
|
42
|
+
base.extend ClassMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
|
47
|
+
def attr_enum(_target, _module, _options={})
|
48
|
+
|
49
|
+
map = {}
|
50
|
+
pname = _options.has_key?(:property_name) ? _options[:property_name] : (_target.to_s + '_str')
|
51
|
+
|
52
|
+
# Extract module constants
|
53
|
+
_module.constants.each { |cname| map[_module.const_get(cname)] = cname.to_s.downcase }
|
54
|
+
|
55
|
+
# Add string getter
|
56
|
+
self.send(:define_method, pname) do
|
57
|
+
map.fetch(self.send(_target), '')
|
58
|
+
end
|
59
|
+
|
60
|
+
# Add string setter
|
61
|
+
self.send(:define_method, pname + '=') do |value|
|
62
|
+
map.each_pair do |k,v|
|
63
|
+
if v == value
|
64
|
+
self.send(_target.to_s + '=', k)
|
65
|
+
return
|
66
|
+
end
|
67
|
+
end
|
68
|
+
raise InvalidEnumName
|
69
|
+
end
|
70
|
+
|
71
|
+
# Add value validator (unless validation is disabled)
|
72
|
+
self.validates _target, inclusion: { :in => map.keys } if _options.fetch(:validate, true)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
data/lib/platanus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: platanus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.16
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- lib/platanus/api_base.rb
|
30
30
|
- lib/platanus/canned.rb
|
31
31
|
- lib/platanus/cmap.rb
|
32
|
+
- lib/platanus/enum.rb
|
32
33
|
- lib/platanus/gcontroller.rb
|
33
34
|
- lib/platanus/http_helpers.rb
|
34
35
|
- lib/platanus/layered.rb
|