galetahub-enum_field 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f48cb1f03e5efc4c877bc640e80d1014b3ed95f3
4
- data.tar.gz: 961be9b9cca8ca6c1ded7040fdb2523fb1e24825
3
+ metadata.gz: 96da53c393009eaee6d98a66b4107c243577a44c
4
+ data.tar.gz: 7c17bffc0d29bfdb5b8399341031bb296f287835
5
5
  SHA512:
6
- metadata.gz: e613425fc98812bdf5e90845f90d41320e6601743805326aca4eeacdd26bcd2f9fe4c2f1614449befd42308930f5c37e58f3e6eaa6a83df752f84b368034710c
7
- data.tar.gz: 8bb7032ee05e016e534b309a02525adf3c6a7b54fada56f0058615d3944cc13d633db6b46e3cd40d99fd7406e09cd96863f9824776aa5786ba40defab9a4d0cf
6
+ metadata.gz: 4ae37aca910cbef3b82a0a67ea4003368c5b832654fcd404b9e45d4e986bbe5f371c2b10f2381c9448d92c064e2111ca487b09cb2937c39d94da306b7ba8a764
7
+ data.tar.gz: fca89c19f39e43eadca5395b860d3d3fc9d6b8a669dd0ad37820d5511a25e1a6bce8f86f442aa95e716ac6c69edb641e5667b70129ef8bb5786dbafae71e1e70
data/README.rdoc CHANGED
@@ -7,6 +7,10 @@
7
7
  Enables Active Record attributes to point to enum like objects, by saving in your database
8
8
  only an integer ID.
9
9
 
10
+ == INSTALL:
11
+
12
+ gem 'galetahub-enum_field', require: 'enum_field'
13
+
10
14
 
11
15
  == FEATURES:
12
16
 
@@ -27,7 +31,7 @@ several options.
27
31
  * You can use a string to identify, for instance, the role.
28
32
  * You can use an id to identify the role.
29
33
 
30
- If you are not confortable with any of this options, maybe +enum_field+ is an answer for you.
34
+ If you are not comfortable with any of this options, maybe +enum_field+ is an answer for you.
31
35
 
32
36
  == BASIC USAGE:
33
37
 
@@ -54,6 +58,9 @@ If you are not confortable with any of this options, maybe +enum_field+ is an an
54
58
  user.role = Role.manager
55
59
  user.role_id == Role.manager.id #will be true
56
60
 
61
+ Role.manager.name # :manager
62
+ user.role.name # :manager
63
+
57
64
  User.first.role.id == User.first.role_id #will be true
58
65
 
59
66
  Your enum classes can have all the methods you need:
@@ -108,7 +115,7 @@ The library also mimics has_many :through behavior, for cases such as:
108
115
 
109
116
  class UserRole < ActiveRecord::Base
110
117
  extend EnumField::EnumeratedAttribute
111
-
118
+
112
119
  belongs_to :user
113
120
  enumerated_attribute :role
114
121
  end
@@ -129,11 +136,6 @@ The library also mimics has_many :through behavior, for cases such as:
129
136
 
130
137
  * activerecord
131
138
 
132
-
133
- == INSTALL:
134
-
135
- gem 'galetahub-enum_field', :require => 'enum_field'
136
-
137
139
  == LICENSE:
138
140
 
139
141
  (The MIT License)
@@ -15,6 +15,7 @@ module EnumField
15
15
  def member(name, options = {})
16
16
  obj, candidate_id = process_options(options)
17
17
  assign_id(obj, candidate_id)
18
+ assign_name(obj, name)
18
19
  define_in_meta(name) { obj }
19
20
  save(name, obj)
20
21
  obj.freeze
@@ -34,9 +35,9 @@ module EnumField
34
35
 
35
36
  def find_by_id(id)
36
37
  case id
37
- when Integer, String, Float, Fixnum then
38
+ when Integer, String, Float, Fixnum then
38
39
  @id2obj[id.to_i]
39
- when Array then
40
+ when Array then
40
41
  id.inject([]) do |items, value|
41
42
  if value && value.respond_to?(:to_i)
42
43
  items << @id2obj[value.to_i]
@@ -62,6 +63,10 @@ module EnumField
62
63
  obj.instance_variable_set(:@id, id)
63
64
  end
64
65
 
66
+ def assign_name(obj, name)
67
+ obj.instance_variable_set(:@name, name)
68
+ end
69
+
65
70
  def new_id(candidate)
66
71
  validate_candidate_id(candidate)
67
72
  candidate || find_next_id
@@ -4,11 +4,11 @@ module EnumField
4
4
  def self.included(base)
5
5
  base.send :extend, ClassMethods
6
6
  end
7
-
7
+
8
8
  module ClassMethods
9
9
  def self.extended(base)
10
10
  base.class_eval do
11
- attr_reader :id
11
+ attr_reader :id, :name
12
12
  end
13
13
  end
14
14
 
@@ -1,3 +1,3 @@
1
1
  module EnumField
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galetahub-enum_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Galeta
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-06 00:00:00.000000000 Z
12
+ date: 2015-12-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Enables Active Record attributes to point to enum like objects, by saving
15
15
  in your database only an integer ID