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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2be661f299492be0519d65da4c1058d38799ff3e
4
- data.tar.gz: a06ff5abf876efbfb0d9318aac939566e7daad83
2
+ SHA256:
3
+ metadata.gz: ecf564d565c3fb097c126fee3d88effe88d0240c5359fe2ea0ca466f163f9127
4
+ data.tar.gz: 53d0500a0585fe7ebd765b0771f1ca0235a797c7e420d8b4c95edbd177bf1f0c
5
5
  SHA512:
6
- metadata.gz: 243473028ff13052255159cdb793122763ab4e568a7289fa6db1a825dace9edcf7345a6767f8b66f357ee780173e70f50617adcbef293211a35e354225724c45
7
- data.tar.gz: e1f248f5232d6aa901081579fba8faddbe86a0a71853b984c76c4fe464f1cc73699ee0f400de0df4866dfde17c13fecd4ecf2b6f7bbd3b078e0dedcd50307f8c
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
- # encoding: utf-8
2
- require 'rake'
3
- require 'rake/testtask'
4
- require 'rake/rdoctask'
5
- require File.join(File.dirname(__FILE__), 'lib', 'enum_field', 'version')
6
-
7
- desc 'Default: run unit tests.'
8
- task :default => :test
9
-
10
- desc 'Test the enum_field plugin.'
11
- Rake::TestTask.new(:test) do |t|
12
- t.libs << 'lib'
13
- t.libs << 'test'
14
- t.pattern = 'test/**/*_test.rb'
15
- t.verbose = true
16
- end
17
-
18
- desc 'Generate documentation for the enum_field plugin.'
19
- Rake::RDocTask.new(:rdoc) do |rdoc|
20
- rdoc.rdoc_dir = 'rdoc'
21
- rdoc.title = 'EnumField'
22
- rdoc.options << '--line-numbers' << '--inline-source'
23
- rdoc.rdoc_files.include('README.rdoc')
24
- rdoc.rdoc_files.include('lib/**/*.rb')
25
- end
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
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  # Basic usage:
4
4
  #
@@ -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 && value.respond_to?(:id)
23
+ value&.respond_to?(:id)
22
24
  end
23
25
 
24
26
  def value_is_member_name?
@@ -1,8 +1,8 @@
1
- # encoding: utf-8
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 then
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)
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module EnumField
4
4
  # Add enum methods to class
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/inflector'
4
4
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EnumField
2
- VERSION = '0.5.3'.freeze
4
+ VERSION = '0.5.4'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe EnumField::AttributeValueResolver do
@@ -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
- }.to change { admin_user.role_type_id }.from(RoleType.admin.id).to(new_role.id)
42
+ end.to change { admin_user.role_type_id }.from(RoleType.admin.id).to(new_role.id)
41
43
  end
42
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  describe EnumField do
@@ -1,2 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
2
4
  require 'enum_field'
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.3
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: 2019-04-12 00:00:00.000000000 Z
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.5.2.3
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