galetahub-enum_field 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +3 -1
- data/Rakefile +26 -25
- data/lib/enum_field.rb +1 -1
- data/lib/enum_field/attribute_value_resolver.rb +3 -1
- data/lib/enum_field/builder.rb +4 -3
- data/lib/enum_field/define_enum.rb +1 -1
- data/lib/enum_field/enumerated_attribute.rb +1 -1
- data/lib/enum_field/version.rb +3 -1
- data/spec/enum_field/attribute_value_resolver_spec.rb +2 -0
- data/spec/enum_field/define_enum_spec.rb +8 -0
- data/spec/enum_field/enumerated_attribute_spec.rb +4 -2
- data/spec/enum_field_spec.rb +2 -0
- data/spec/spec_helper.rb +3 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ecf564d565c3fb097c126fee3d88effe88d0240c5359fe2ea0ca466f163f9127
|
4
|
+
data.tar.gz: 53d0500a0585fe7ebd765b0771f1ca0235a797c7e420d8b4c95edbd177bf1f0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5fee53ce52bc2ff4d69a0c6e99976837164b74ca713f7630c1562158750fdcace36ea1e8e5bf4b0137c4649063842584ba76ee2c4776c8fd92c08981d2e4143
|
7
|
+
data.tar.gz: 1d70dff1a5d345f44152b85f1eec2a583cb9f9cbb238262525a1bc3fb0afc2384206d26b4cce709215d8ea54d49947059b842b5a1ecc7f908391b3c1f2cc6ca5
|
data/README.md
CHANGED
@@ -107,7 +107,9 @@ PhoneType.first == PhoneType.home
|
|
107
107
|
PhoneType.last == PhoneType.mobile
|
108
108
|
|
109
109
|
PhoneType.find_by_id(PhoneType.home.id) == PhoneType.home
|
110
|
-
PhoneType.find_by_id(123456) == nil
|
110
|
+
PhoneType.find_by_id(123456) == nil # will not raise if id does not exists
|
111
|
+
PhoneType.safe_find(123456) == nil # just alias for find_by_id
|
112
|
+
|
111
113
|
PhoneType.find(2) == PhoneType.commercial
|
112
114
|
PhoneType.find(123456) # will raise
|
113
115
|
|
data/Rakefile
CHANGED
@@ -1,25 +1,26 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require 'rake
|
4
|
-
require 'rake/
|
5
|
-
require
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
t.libs << '
|
14
|
-
t.
|
15
|
-
t.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
rdoc.
|
22
|
-
rdoc.
|
23
|
-
rdoc.
|
24
|
-
rdoc.rdoc_files.include('
|
25
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require File.join(File.dirname(__FILE__), 'lib', 'enum_field', 'version')
|
7
|
+
|
8
|
+
desc 'Default: run unit tests.'
|
9
|
+
task default: :test
|
10
|
+
|
11
|
+
desc 'Test the enum_field plugin.'
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << 'lib'
|
14
|
+
t.libs << 'test'
|
15
|
+
t.pattern = 'test/**/*_test.rb'
|
16
|
+
t.verbose = true
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Generate documentation for the enum_field plugin.'
|
20
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
21
|
+
rdoc.rdoc_dir = 'rdoc'
|
22
|
+
rdoc.title = 'EnumField'
|
23
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
24
|
+
rdoc.rdoc_files.include('README.rdoc')
|
25
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
|
+
end
|
data/lib/enum_field.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module EnumField
|
2
4
|
class AttributeValueResolver
|
3
5
|
attr_reader :value, :klass
|
@@ -18,7 +20,7 @@ module EnumField
|
|
18
20
|
private
|
19
21
|
|
20
22
|
def value_is_member?
|
21
|
-
value
|
23
|
+
value&.respond_to?(:id)
|
22
24
|
end
|
23
25
|
|
24
26
|
def value_is_member_name?
|
data/lib/enum_field/builder.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module EnumField
|
4
4
|
class Builder
|
5
|
-
METHODS = %w[all names find_by_id find first last ids valid_id? valid_name?].freeze
|
5
|
+
METHODS = %w[all names find_by_id find first last ids valid_id? valid_name? safe_find].freeze
|
6
6
|
|
7
7
|
attr_reader :members
|
8
8
|
|
@@ -58,12 +58,13 @@ module EnumField
|
|
58
58
|
|
59
59
|
def find_by_id(id)
|
60
60
|
case id
|
61
|
-
when Array
|
61
|
+
when Array
|
62
62
|
all.select { |object| id.include?(object.id) }
|
63
63
|
else
|
64
64
|
all.detect { |object| object.id == id }
|
65
65
|
end
|
66
66
|
end
|
67
|
+
alias safe_find find_by_id
|
67
68
|
|
68
69
|
def method_missing(method_name, *args, &block)
|
69
70
|
if @target.respond_to?(method_name)
|
data/lib/enum_field/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe EnumField::DefineEnum do
|
@@ -38,6 +40,12 @@ describe EnumField::DefineEnum do
|
|
38
40
|
expect(Role.find_by_id(3)).to eq Role.employee
|
39
41
|
end
|
40
42
|
|
43
|
+
it 'must safe_find one instance' do
|
44
|
+
expect(Role.safe_find(1)).to eq Role.admin
|
45
|
+
expect(Role.safe_find(2)).to eq Role.manager
|
46
|
+
expect(Role.safe_find(3)).to eq Role.employee
|
47
|
+
end
|
48
|
+
|
41
49
|
it 'must find_by_id array of instances' do
|
42
50
|
expect(Role.find_by_id([2, 3])).to eq [Role.manager, Role.employee]
|
43
51
|
expect(Role.find_by_id([nil, 1])).to eq [Role.admin]
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe EnumField::EnumeratedAttribute do
|
@@ -35,8 +37,8 @@ describe EnumField::EnumeratedAttribute do
|
|
35
37
|
end
|
36
38
|
|
37
39
|
it 'must set role_type_id' do
|
38
|
-
expect
|
40
|
+
expect do
|
39
41
|
admin_user.role_type = new_role
|
40
|
-
|
42
|
+
end.to change { admin_user.role_type_id }.from(RoleType.admin.id).to(new_role.id)
|
41
43
|
end
|
42
44
|
end
|
data/spec/enum_field_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 0.5.4
|
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:
|
12
|
+
date: 2020-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -108,13 +108,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
110
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.7.6.2
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Enumerated attributes for any ruby class aka Active Record
|
115
115
|
test_files:
|
116
|
-
- spec/enum_field/attribute_value_resolver_spec.rb
|
117
|
-
- spec/enum_field/define_enum_spec.rb
|
118
|
-
- spec/enum_field/enumerated_attribute_spec.rb
|
119
116
|
- spec/enum_field_spec.rb
|
120
117
|
- spec/spec_helper.rb
|
118
|
+
- spec/enum_field/enumerated_attribute_spec.rb
|
119
|
+
- spec/enum_field/attribute_value_resolver_spec.rb
|
120
|
+
- spec/enum_field/define_enum_spec.rb
|