rubyenum 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +50 -1
- data/lib/rubyenum/version.rb +1 -1
- data/lib/rubyenum.rb +44 -11
- data/rubyenum.gemspec +2 -0
- data/spec/rubyenum_associations_spec.rb +24 -0
- data/spec/rubyenum_base_spec.rb +52 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/user_fixture.rb +7 -0
- data/spec/user_role_fixture.rb +7 -0
- metadata +42 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c8c35f0eefaea3a69f72e056fa555a9b1179902
|
4
|
+
data.tar.gz: 8371b1ce5445c3f3878a6860a21d0955e5392b57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32fdc4390bf0280cf4cee4df923f8dd83555a3f8f5d3c5c683cef824862cd2370953faaeb95cd8b9bcaeabab04acb80fd637b79861862bead9ecc0031df5650b
|
7
|
+
data.tar.gz: db8f3d8ad90967b1316ecbbe62ff926352477e9767ae5b8854d2a6601a57f3ef2dbe47d7313be5622760701749914fa4077efbb0c054374aa9c56469df6e55a7
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -1,6 +1,55 @@
|
|
1
1
|
# Rubyenum
|
2
2
|
|
3
|
-
|
3
|
+
RubyEnum is a simple implemenation of the enum pattern that you see in java, c++, etc. Why use a pattern from a scary laguage like java when we have ruby? Because Enums are awesome. Anytime you have a variety of types for an object that are merely classification, and not deserving of objects themselves, you can use an enum.
|
4
|
+
|
5
|
+
For instance, if you have a User class that has a role attribute (admin, editor, viewer) in the past you either did:
|
6
|
+
- a) store an integer in the database and have a long list of constants to use throughout your code (the better option)
|
7
|
+
- b) Stored strings in the database like `"admin"`, or `"editor"` (the terrible option)
|
8
|
+
|
9
|
+
Enter enums
|
10
|
+
|
11
|
+
an Enum is an object that has it's own final state (such as an integer for the data), as well as a variety of methods allowing you to act upon that state. For example, this is how you might define a UserRoles enum with RubyEnum
|
12
|
+
|
13
|
+
```
|
14
|
+
class UserRoles < RubyEnum::Base
|
15
|
+
|
16
|
+
fields :id, :permissions
|
17
|
+
|
18
|
+
enum :ADMIN, 0, "all"
|
19
|
+
enum :EDITOR, 1, "read/write"
|
20
|
+
enum :VIEWER, 2, "read"
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
```
|
25
|
+
|
26
|
+
After defining the enum, you can access an instance of it via the constant and access its properties like so:
|
27
|
+
|
28
|
+
```
|
29
|
+
role = UserRoles::ADMIN
|
30
|
+
role.name #=> "ADMIN" (built in method)
|
31
|
+
role.id #=> 0
|
32
|
+
role.permissions #=> "all"
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also go backwards from a field:
|
36
|
+
```
|
37
|
+
i = 1
|
38
|
+
name = "VIEWER"
|
39
|
+
UserRoles.from_id(i) #=> UserRoles::EDITOR
|
40
|
+
UserRoles.value_of(name) #=> UserRoles::VIEWER
|
41
|
+
```
|
42
|
+
|
43
|
+
Finally, they are grouped together, so you can access them all as an array:
|
44
|
+
```
|
45
|
+
UserRoles.all #=> [UserRoles::ADMIN, UserRoles::EDITOR, UserRoles::VIEWER]
|
46
|
+
```
|
47
|
+
|
48
|
+
Together, this all makes for a far more flexible, yet still very stable, method of defining constants throughout the app.
|
49
|
+
|
50
|
+
## With rails
|
51
|
+
|
52
|
+
|
4
53
|
|
5
54
|
## Installation
|
6
55
|
|
data/lib/rubyenum/version.rb
CHANGED
data/lib/rubyenum.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "rubyenum/version"
|
2
|
+
require "pry"
|
2
3
|
|
3
4
|
module RubyEnum
|
4
5
|
|
@@ -22,6 +23,16 @@ module RubyEnum
|
|
22
23
|
def self.fields(*args)
|
23
24
|
args.each do |argument|
|
24
25
|
@@fields << argument
|
26
|
+
define_singleton_method("from_#{argument.to_s}") do |value|
|
27
|
+
item_to_return = nil
|
28
|
+
@@all.each do |item|
|
29
|
+
if item.send(argument) == value
|
30
|
+
item_to_return = item
|
31
|
+
break
|
32
|
+
end
|
33
|
+
end
|
34
|
+
item_to_return
|
35
|
+
end
|
25
36
|
end
|
26
37
|
end
|
27
38
|
|
@@ -41,6 +52,10 @@ module RubyEnum
|
|
41
52
|
@@all << enumObj
|
42
53
|
end
|
43
54
|
|
55
|
+
def initialize(name)
|
56
|
+
@name = name
|
57
|
+
end
|
58
|
+
|
44
59
|
def add_attribute(attr_name, value)
|
45
60
|
var = instance_variable_set(:"@#{attr_name}", value)
|
46
61
|
define_singleton_method("#{attr_name}") do
|
@@ -48,26 +63,46 @@ module RubyEnum
|
|
48
63
|
end
|
49
64
|
end
|
50
65
|
|
51
|
-
def initialize(name)
|
52
|
-
@name = name
|
53
|
-
end
|
54
66
|
|
55
67
|
|
56
68
|
end
|
57
69
|
|
58
|
-
|
59
|
-
|
70
|
+
module Associations
|
71
|
+
|
72
|
+
# let ActiveRecord define these if using rails
|
73
|
+
def create_helper_methods
|
74
|
+
unless respond_to? :read_attribute
|
75
|
+
define_method(:read_attribute) do |name|
|
76
|
+
instance_variable_get(:"@#{name}")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
unless respond_to? :write_attribute
|
80
|
+
define_method(:write_attribute) do |name, value|
|
81
|
+
instance_variable_set(:"@#{name}", value)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_camelcase(string)
|
87
|
+
return_string = ""
|
88
|
+
parts = string.split("_")
|
89
|
+
parts.each do |part|
|
90
|
+
part[0] = part[0].upcase
|
91
|
+
return_string += part
|
92
|
+
end
|
93
|
+
return_string
|
94
|
+
end
|
60
95
|
|
61
96
|
def enumify(method_name, hash)
|
62
|
-
|
63
|
-
|
97
|
+
create_helper_methods
|
98
|
+
klass = Object.const_get(to_camelcase(hash[:with].to_s))
|
64
99
|
define_method(method_name) do
|
65
100
|
enum_to_return = nil
|
66
101
|
var = read_attribute(method_name)
|
67
102
|
klass.all.each do |item|
|
68
103
|
if item.send(hash[:use]) == var
|
69
104
|
enum_to_return = item
|
70
|
-
break
|
105
|
+
break
|
71
106
|
end
|
72
107
|
end
|
73
108
|
enum_to_return
|
@@ -101,11 +136,9 @@ end
|
|
101
136
|
|
102
137
|
# extend RubyEnum::Associations
|
103
138
|
|
104
|
-
# attr_accessor :animal
|
105
|
-
|
106
139
|
# enumify :animal, :with => :animal_example, :use => :id
|
107
140
|
|
108
|
-
# #
|
141
|
+
# # writes getter and setter methods
|
109
142
|
# # animal=(AnimalExample), sets the property to the :use value @animal = id
|
110
143
|
# # animal => AnimalExample, gets the property from the :use value and converts to Enum
|
111
144
|
|
data/rubyenum.gemspec
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubyenum'
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'user_role_fixture'
|
4
|
+
|
5
|
+
describe RubyEnum::Associations do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@user = User.new
|
9
|
+
@user.role = UserRole::ADMIN
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#role" do
|
14
|
+
it "stores the value of the assigned field" do
|
15
|
+
expect(@user.instance_variable_get(:"@role")).to be 0
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns the enum of the user" do
|
19
|
+
expect(@user.role).to eq(UserRole::ADMIN)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubyenum'
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'user_fixture'
|
4
|
+
|
5
|
+
describe RubyEnum::Associations do
|
6
|
+
|
7
|
+
describe "#all" do
|
8
|
+
it "returns a array of the enums" do
|
9
|
+
enums = UserRole.all
|
10
|
+
expect(enums.size).to be 3
|
11
|
+
expect(enums[0]).to eq UserRole::ADMIN
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#new" do
|
16
|
+
it "does not allow instantiation" do
|
17
|
+
expect { UserRole.new }.to raise_error(NoMethodError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#value_of" do
|
22
|
+
it "returns the proper enum for a string" do
|
23
|
+
enum = UserRole.value_of "EDITOR"
|
24
|
+
expect(enum).to eq UserRole::EDITOR
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns the proper enum for a symbol" do
|
28
|
+
enum = UserRole.value_of :READER
|
29
|
+
expect(enum).to eq UserRole::READER
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".fields" do
|
34
|
+
it "creates methods to access enum from each field" do
|
35
|
+
enum = UserRole.from_id 1
|
36
|
+
expect(enum).to eq UserRole::EDITOR
|
37
|
+
|
38
|
+
enum = UserRole.from_permission "all"
|
39
|
+
expect(enum).to eq UserRole::ADMIN
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#get_fields" do
|
44
|
+
it "returns an array of the defined fields" do
|
45
|
+
fields = UserRole.get_fields
|
46
|
+
expect(fields.size).to be 2
|
47
|
+
|
48
|
+
expect(fields.include? :id).to be true
|
49
|
+
expect(fields.include? :permission).to be true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyenum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- georgemayer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
description: provides enum functionality for ruby
|
42
70
|
email:
|
43
71
|
- mayer.georgep@gmail.com
|
@@ -46,6 +74,7 @@ extensions: []
|
|
46
74
|
extra_rdoc_files: []
|
47
75
|
files:
|
48
76
|
- ".gitignore"
|
77
|
+
- ".rspec"
|
49
78
|
- Gemfile
|
50
79
|
- LICENSE.txt
|
51
80
|
- README.md
|
@@ -53,6 +82,11 @@ files:
|
|
53
82
|
- lib/rubyenum.rb
|
54
83
|
- lib/rubyenum/version.rb
|
55
84
|
- rubyenum.gemspec
|
85
|
+
- spec/rubyenum_associations_spec.rb
|
86
|
+
- spec/rubyenum_base_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/user_fixture.rb
|
89
|
+
- spec/user_role_fixture.rb
|
56
90
|
homepage: ''
|
57
91
|
licenses:
|
58
92
|
- MIT
|
@@ -77,4 +111,9 @@ rubygems_version: 2.2.1
|
|
77
111
|
signing_key:
|
78
112
|
specification_version: 4
|
79
113
|
summary: provides enum functionality for ruby
|
80
|
-
test_files:
|
114
|
+
test_files:
|
115
|
+
- spec/rubyenum_associations_spec.rb
|
116
|
+
- spec/rubyenum_base_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/user_fixture.rb
|
119
|
+
- spec/user_role_fixture.rb
|