iron-enum 1.0.4 → 1.0.5
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/History.txt +5 -0
- data/README.rdoc +15 -7
- data/Version.txt +1 -1
- data/lib/iron-enum.rb +2 -0
- data/lib/iron/enum/attr_support.rb +24 -3
- data/spec/enum/attr_spec.rb +3 -0
- data/spec/spec_helper.rb +1 -1
- metadata +20 -27
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e8720fd7684984e779c2c1ec32303efa0bc4524
|
4
|
+
data.tar.gz: 25418ec2a560576081bab7141e409c750001793e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4333e90565840188208a30f0f34bb16abddb6c7ff02dc3bd649b40c3de99128f14bbac2046373ec1a070456be76f4759ae6dbb0dd9fa9fa706cf2fdc0558f82
|
7
|
+
data.tar.gz: ca0e7f8ddfd034dcc65e22b6a2c0422f90fb0361f195a602277e574a0d5237760b0a1c1a8e43148a2f22215d3900c76b00fa9ffe0e2b8cf5d0f4a0d088597a6f
|
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -11,7 +11,7 @@ Add support for rich enum values (enumerated constants) to Ruby/Rails.
|
|
11
11
|
Managing constants - status flags, system states, etc - is frustrating when working
|
12
12
|
with Ruby and Rails. The canonical solution is to use class constants. But doing so
|
13
13
|
leaves a lot to be desired. This gem provides a much more Ruby-like way of dealing
|
14
|
-
with magic constants
|
14
|
+
with magic constants that provides symbol/key-based lookup, automatic conversion between
|
15
15
|
keys and values, user-friendly naming, attribute support, model scoping and validation, and more.
|
16
16
|
|
17
17
|
To use:
|
@@ -30,16 +30,16 @@ To use:
|
|
30
30
|
enum :returned, 5
|
31
31
|
end
|
32
32
|
|
33
|
-
# Once you have your enum set up, there are a host of things you can do
|
34
|
-
# To start with, enums set up normal Ruby class constants
|
33
|
+
# Once you have your enum set up, there are a host of things you can do.
|
34
|
+
# To start with, enums set up normal Ruby class constants:
|
35
35
|
>> puts OrderStatus::PURCHASED
|
36
36
|
=> 3
|
37
37
|
|
38
|
-
# But you can also refer to them with symbolic keys
|
38
|
+
# But you can also refer to them with symbolic keys:
|
39
39
|
>> puts OrderStatus.value(:purchased)
|
40
40
|
=> 3
|
41
41
|
|
42
|
-
# You can convert between values, keys and text
|
42
|
+
# You can convert between values, keys and text:
|
43
43
|
>> OrderStatus.value(:shipped)
|
44
44
|
=> 4
|
45
45
|
>> OrderStatus.name(:shipped)
|
@@ -60,7 +60,11 @@ To use:
|
|
60
60
|
|
61
61
|
# This is all handy, but where enums really shine is as declared attributes
|
62
62
|
class Order < ActiveRecord::Base
|
63
|
+
# Pass the attribute and the enum module that it uses
|
63
64
|
enum_attr :status => OrderStatus
|
65
|
+
|
66
|
+
# Note that if your attribute's name maps to your enum module name, you can skip the hash:
|
67
|
+
# enum_attr :order_status
|
64
68
|
end
|
65
69
|
|
66
70
|
# Now you can set a value using a key
|
@@ -78,7 +82,7 @@ To use:
|
|
78
82
|
# This raises RuntimeError('Unknown key or value [27] in enum OrderStatus')
|
79
83
|
>> @order.status = 27
|
80
84
|
|
81
|
-
# You even get free scoping
|
85
|
+
# You even get free scoping!
|
82
86
|
>> Order.with_status(:shipped)
|
83
87
|
=> [...all orders with status == 4...]
|
84
88
|
|
@@ -100,4 +104,8 @@ To install, simply run:
|
|
100
104
|
|
101
105
|
RVM users can skip the sudo:
|
102
106
|
|
103
|
-
gem install iron-enum
|
107
|
+
gem install iron-enum
|
108
|
+
|
109
|
+
Once installed, just require it:
|
110
|
+
|
111
|
+
require 'iron-enum'
|
data/Version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.5
|
data/lib/iron-enum.rb
ADDED
@@ -13,13 +13,19 @@ module Enum
|
|
13
13
|
# declare it like so:
|
14
14
|
#
|
15
15
|
# class User < ActiveRecord::Base
|
16
|
-
#
|
16
|
+
# # A symbol will be assumed to map to a valid enum class name
|
17
|
+
# enum_attr :user_type
|
18
|
+
# # If your attribute's name won't map automatically, you can pass a hash instead
|
19
|
+
# enum_attr :another_user_type => UserType
|
20
|
+
# # If you have multiple enum attributes, you can add them all in one call, just make
|
21
|
+
# # sure mapped attrs are at the end or you'll get an error
|
22
|
+
# enum_attr :user_type, :another_user_type => UserType
|
17
23
|
# end
|
18
24
|
#
|
19
25
|
# When using non-model classes, it's the same syntax:
|
20
26
|
#
|
21
27
|
# class User
|
22
|
-
# enum_attr :user_type
|
28
|
+
# enum_attr :user_type
|
23
29
|
# end
|
24
30
|
#
|
25
31
|
# This will tell your class/model that the user_type attribute contains values from the UserType enum, and
|
@@ -46,7 +52,22 @@ module Enum
|
|
46
52
|
module AttrSupport
|
47
53
|
|
48
54
|
# Call with enum_attr :field => Enum
|
49
|
-
def enum_attr(
|
55
|
+
def enum_attr(*array_or_map)
|
56
|
+
# Convert to a full map
|
57
|
+
field_to_enum_map = {}
|
58
|
+
array_or_map.each do |info|
|
59
|
+
if info.is_a?(Symbol)
|
60
|
+
name = info.to_s.capitalize.gsub(/\_([a-z])/) { $1.capitalize }
|
61
|
+
klass = Object.const_get(name) rescue nil
|
62
|
+
raise "Unknown enum class '#{name}' for enum_attr :#{info}" unless klass
|
63
|
+
field_to_enum_map[info] = klass
|
64
|
+
elsif info.is_a?(Hash)
|
65
|
+
field_to_enum_map.merge!(info)
|
66
|
+
else
|
67
|
+
raise "Invalid enum_attr key: #{info.inspect}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
50
71
|
# Save off the attr map
|
51
72
|
@enum_attrs ||= {}
|
52
73
|
@enum_attrs.merge!(field_to_enum_map)
|
data/spec/enum/attr_spec.rb
CHANGED
@@ -8,6 +8,7 @@ describe Enum do
|
|
8
8
|
|
9
9
|
class AttrClass
|
10
10
|
enum_attr :pos => AttrTest
|
11
|
+
enum_attr :attr_test, :bob => AttrTest
|
11
12
|
end
|
12
13
|
|
13
14
|
before do
|
@@ -16,6 +17,8 @@ describe Enum do
|
|
16
17
|
|
17
18
|
it 'should support declaring enum attributes' do
|
18
19
|
Module.should respond_to(:enum_attr)
|
20
|
+
@obj.should respond_to(:attr_test_second?)
|
21
|
+
@obj.should respond_to(:bob_as_name)
|
19
22
|
end
|
20
23
|
|
21
24
|
it 'should allow getting an attribute in key form' do
|
data/spec/spec_helper.rb
CHANGED
@@ -16,6 +16,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'iron',
|
|
16
16
|
RSpec.configure do |config|
|
17
17
|
config.add_formatter 'documentation'
|
18
18
|
config.color = true
|
19
|
-
config.
|
19
|
+
config.backtrace_exclusion_patterns = [/rspec/]
|
20
20
|
end
|
21
21
|
|
metadata
CHANGED
@@ -1,62 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iron-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rob Morris
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-08-16 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '2.6'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '2.6'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: active_record
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '3.0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '3.0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: sqlite3
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- - ~>
|
45
|
+
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '1.3'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- - ~>
|
52
|
+
- - "~>"
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '1.3'
|
62
55
|
description: Adds enumerated constant value support to Ruby and Rails projects
|
@@ -66,10 +59,16 @@ executables: []
|
|
66
59
|
extensions: []
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
62
|
+
- ".rspec"
|
63
|
+
- History.txt
|
64
|
+
- LICENSE
|
65
|
+
- README.rdoc
|
66
|
+
- Version.txt
|
67
|
+
- lib/iron-enum.rb
|
68
|
+
- lib/iron/enum.rb
|
69
69
|
- lib/iron/enum/attr_support.rb
|
70
70
|
- lib/iron/enum/core.rb
|
71
71
|
- lib/iron/enum/model_support.rb
|
72
|
-
- lib/iron/enum.rb
|
73
72
|
- spec/enum/attr_spec.rb
|
74
73
|
- spec/enum/enum_spec.rb
|
75
74
|
- spec/enum/key_spec.rb
|
@@ -78,34 +77,28 @@ files:
|
|
78
77
|
- spec/enum/option_spec.rb
|
79
78
|
- spec/enum/value_spec.rb
|
80
79
|
- spec/spec_helper.rb
|
81
|
-
- LICENSE
|
82
|
-
- History.txt
|
83
|
-
- Version.txt
|
84
|
-
- README.rdoc
|
85
|
-
- .rspec
|
86
80
|
homepage: http://irongaze.com
|
87
81
|
licenses:
|
88
82
|
- MIT
|
83
|
+
metadata: {}
|
89
84
|
post_install_message:
|
90
85
|
rdoc_options: []
|
91
86
|
require_paths:
|
92
87
|
- lib
|
93
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
-
none: false
|
95
89
|
requirements:
|
96
|
-
- -
|
90
|
+
- - ">="
|
97
91
|
- !ruby/object:Gem::Version
|
98
92
|
version: 1.9.2
|
99
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
-
none: false
|
101
94
|
requirements:
|
102
|
-
- -
|
95
|
+
- - ">="
|
103
96
|
- !ruby/object:Gem::Version
|
104
97
|
version: '0'
|
105
98
|
requirements: []
|
106
99
|
rubyforge_project:
|
107
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.4.3
|
108
101
|
signing_key:
|
109
|
-
specification_version:
|
102
|
+
specification_version: 4
|
110
103
|
summary: Enum support for Ruby and Rails
|
111
104
|
test_files: []
|