galetahub-enum_field 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8fe01fd9a421a42a8bc5a49fc1f1f57d78b3ef8
4
- data.tar.gz: 3b86f719ab1b2dc329af61fca85ffff9e797bf42
3
+ metadata.gz: 6aecc946fb32aa9197932e9ff1264bbf06cd19ef
4
+ data.tar.gz: 58f8f65ee968672a7b1678819b18612a7faec2ac
5
5
  SHA512:
6
- metadata.gz: c51540d123b2444fbc79dcde8457590960911e5147d19aac45931eda9ce5aa842efe64bd95e5fa3e258e5699e51d9fe3b47c33348290be6dfd9040d003f634e8
7
- data.tar.gz: 744b7af8a17cf2c2ee51f5a1d154a42ad03926a2a5e8f72943d63f5053110f658a7ce2bfe97c50f7ed2ad3040fd9e7cffc6df90b415ebd40f5d01e782beb7446
6
+ metadata.gz: 2a9241a8594e8f55df86dd494cf288075e2865cb47672f537f675bd0c3e982b38c764bfa9b916dd69a67bf530f43f62274c766a04d147f87abf762d5a8eb8616
7
+ data.tar.gz: 065c0d729c5ecd8ef57b5673eb7802a851ad96c1a6c89f4b130edf3f808fb1440e681b3694bf1b749fc150a202f3506073de43387c7e6272817379771726cd47
data/README.md CHANGED
@@ -6,11 +6,11 @@
6
6
  Enables Active Record attributes to point to enum like objects, by saving in your database
7
7
  only an integer ID.
8
8
 
9
- ## INSTALL:
9
+ ## Install
10
10
 
11
11
  gem 'galetahub-enum_field', require: 'enum_field'
12
12
 
13
- ## FEATURES:
13
+ ## Features
14
14
 
15
15
  * Allows creation of Classes with enum like behaviour.
16
16
  * Allows any number of members and methods in the enum classes.
@@ -20,7 +20,7 @@ only an integer ID.
20
20
  * <code>if user.role.can_edit?</code>
21
21
  * Saves in your +AR+ tables, only an integer id pointing to the enumeration member.
22
22
 
23
- ## SYNOPSIS:
23
+ ## Synopsis
24
24
 
25
25
  When in an Active Record class, you have an attribute like role, state or country you have
26
26
  several options.
@@ -31,7 +31,7 @@ several options.
31
31
 
32
32
  If you are not comfortable with any of this options, maybe +enum_field+ is an answer for you.
33
33
 
34
- ## BASIC USAGE:
34
+ ## Basic usage
35
35
 
36
36
  Define rules:
37
37
 
@@ -79,18 +79,21 @@ Your enum classes can have all the methods you need:
79
79
  class PhoneType
80
80
  include EnumField::DefineEnum
81
81
 
82
- def initialize(name)
83
- @name = name
82
+ attr_reader :prefix
83
+
84
+ def initialize(prefix)
85
+ @prefix = prefix
84
86
  end
85
87
 
86
88
  define_enum do
87
- member :home, object: new('home')
88
- member :commercial, object: new('commercial')
89
- member :mobile, object: new('mobile')
89
+ member :home, object: new('045')
90
+ member :commercial, object: new('044')
91
+ member :mobile, object: new('+380')
90
92
  end
91
93
  end
92
94
 
93
- user.phone.type.name
95
+ user.phone_type.prefix # +380
96
+ user.phone_type.name # :mobile
94
97
  ```
95
98
 
96
99
  You have some +AR+ like methods in enum classes
@@ -126,6 +129,31 @@ CommentType.audio.id # 102
126
129
  CommentType.text.id # 103
127
130
  ```
128
131
 
132
+ ### Check if id exists
133
+
134
+ ``` ruby
135
+ CommentType.valid_id?(-1) # false
136
+ CommentType.valid_id?(0) # false
137
+ CommentType.valid_id?(nil) # false
138
+ CommentType.valid_id?(1) # false
139
+ CommentType.valid_id?('101') # false
140
+ CommentType.valid_id?(:text) # false
141
+
142
+ CommentType.valid_id?(101) # true
143
+ ```
144
+
145
+ ### Check if name exists
146
+
147
+ ``` ruby
148
+ CommentType.valid_name?('video') # true
149
+ CommentType.valid_name?(:video) # true
150
+
151
+ CommentType.valid_name?(nil) # false
152
+ CommentType.valid_name?(1) # false
153
+ CommentType.valid_name?('wrong') # false
154
+ CommentType.valid_name?(:wrong) # false
155
+ ```
156
+
129
157
  ## Tests
130
158
 
131
159
  bundle install
@@ -2,7 +2,7 @@
2
2
 
3
3
  module EnumField
4
4
  class Builder
5
- METHODS = %w[all names find_by_id find first last ids].freeze
5
+ METHODS = %w[all names find_by_id find first last ids valid_id? valid_name?].freeze
6
6
 
7
7
  attr_reader :members
8
8
 
@@ -44,6 +44,14 @@ module EnumField
44
44
  all.map(&:id)
45
45
  end
46
46
 
47
+ def valid_id?(value)
48
+ !value.nil? && ids.include?(value)
49
+ end
50
+
51
+ def valid_name?(value)
52
+ !value.nil? && names.include?(normalize_name(value))
53
+ end
54
+
47
55
  def find(id)
48
56
  find_by_id(id) || raise(EnumField::ObjectNotFound)
49
57
  end
@@ -57,6 +65,23 @@ module EnumField
57
65
  end
58
66
  end
59
67
 
68
+ def method_missing(method_name, *args, &block)
69
+ if @target.respond_to?(method_name)
70
+ @target.send(method_name, *args, &block)
71
+ else
72
+ super
73
+ end
74
+ end
75
+
76
+ def respond_to_missing?(method_name, include_private = false)
77
+ @target.respond_to?(method_name) || super
78
+ end
79
+
80
+ def freeze!
81
+ @members.freeze
82
+ freeze
83
+ end
84
+
60
85
  private
61
86
 
62
87
  def create_new_object(name, options)
@@ -25,9 +25,9 @@ module EnumField
25
25
  end
26
26
  end
27
27
 
28
- @enum_builder.members.keys.each do |method|
28
+ @enum_builder.names.each do |method|
29
29
  define_singleton_method method do
30
- @enum_builder.members[method]
30
+ @enum_builder[method]
31
31
  end
32
32
 
33
33
  define_method "#{method}?" do
@@ -39,7 +39,7 @@ module EnumField
39
39
  @enum_builder[value]
40
40
  end
41
41
 
42
- @enum_builder.members.freeze
42
+ @enum_builder.freeze!
43
43
  end
44
44
  end
45
45
 
@@ -1,3 +1,3 @@
1
1
  module EnumField
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.5.1'.freeze
3
3
  end
@@ -124,5 +124,58 @@ describe EnumField::DefineEnum do
124
124
  expect(comment_type.audio.id).to eq start_number + 2
125
125
  expect(comment_type.text.id).to eq start_number + 3
126
126
  end
127
+
128
+ it 'must check valid id' do
129
+ expect(comment_type.valid_id?(-1)).to eq false
130
+ expect(comment_type.valid_id?(0)).to eq false
131
+ expect(comment_type.valid_id?(nil)).to eq false
132
+ expect(comment_type.valid_id?(1)).to eq false
133
+ expect(comment_type.valid_id?('101')).to eq false
134
+ expect(comment_type.valid_id?(:text)).to eq false
135
+
136
+ expect(comment_type.valid_id?(101)).to eq true
137
+ end
138
+
139
+ it 'must check valid name' do
140
+ expect(comment_type.valid_name?('video')).to eq true
141
+ expect(comment_type.valid_name?(:video)).to eq true
142
+
143
+ expect(comment_type.valid_name?(nil)).to eq false
144
+ expect(comment_type.valid_name?(0)).to eq false
145
+ expect(comment_type.valid_name?(101)).to eq false
146
+ expect(comment_type.valid_name?('wrong')).to eq false
147
+ expect(comment_type.valid_name?(:not_exist)).to eq false
148
+ end
149
+ end
150
+
151
+ context 'custom objects' do
152
+ let(:start_number) { 100 }
153
+ let(:figure_type) do
154
+ Class.new(Object) do
155
+ include EnumField::DefineEnum
156
+
157
+ attr_accessor :size
158
+
159
+ def initialize(size)
160
+ @size = size
161
+ end
162
+
163
+ define_enum do
164
+ member :straight, object: new(10)
165
+ member :pear, object: new(20)
166
+ member :spoon, object: new(30)
167
+ end
168
+ end
169
+ end
170
+
171
+ it 'must set size attribute' do
172
+ expect(figure_type.straight.size).to eq 10
173
+ expect(figure_type.straight.name).to eq :straight
174
+ expect(figure_type.straight.id).to eq 1
175
+ end
176
+
177
+ it 'must freeze objects' do
178
+ expect { figure_type.pear.size = 100 }.to raise_error(RuntimeError)
179
+ end
127
180
  end
128
181
  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.5.0
4
+ version: 0.5.1
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: 2017-04-28 00:00:00.000000000 Z
12
+ date: 2017-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  requirements: []
108
108
  rubyforge_project:
109
- rubygems_version: 2.5.1
109
+ rubygems_version: 2.6.12
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Enumerated attributes for any ruby class aka Active Record