human_error 1.8.0 → 1.8.1

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: f8ddf319ce51c10a9949de1d62c8fbd357f8dfc4
4
- data.tar.gz: 93067b5371d13fac93253c0ccb18acffebd8d215
3
+ metadata.gz: c55554df9b5ca6be992b36fbca062fa8e6c4ea0e
4
+ data.tar.gz: 246c273b755e273e132e9fb8d65e27e909ab087e
5
5
  SHA512:
6
- metadata.gz: 9413d40d415fc3ce3d1c46bf1c7149164eef45cee125c3c3a8cf7338a76fac157317cbfc61395c56dc5766d5b9718e3654d10babd3d1542828b85a1ebf96317c
7
- data.tar.gz: eb3306baace6da3fdc883b4d933641aac826669737164fc51b29040c5d112d3e97ad4f5de36cf1f04500d5658f2263a159b43dbe250435c560beca9c0ba85c43
6
+ metadata.gz: f40e1a014d53632a840475003c64f96117f9e2cad20f412e8bd0844b48063c5efcc658ac949f0ac8281daa50aa29dd8a694f585fcb31e393370c590e83011469
7
+ data.tar.gz: a643c39007b66cc9629be8eebb2d27bd9388773b459802fde156c4b284f248dc44cecbe91b8c90c0857775b4d001eb2b3c9148d5377be71c56df885a1fc3fa78
@@ -1,5 +1,24 @@
1
1
  class HumanError
2
2
  module Persistable
3
+ module ClassMethods
4
+ def find(*ids)
5
+ super
6
+ rescue ActiveRecord::RecordNotFound => e
7
+ ids = case e.message
8
+ when /\ACouldn't find .* without an ID\z/
9
+ []
10
+ when /\ACouldn't find .* with \'.*\'=(\d+)\z/
11
+ [$1]
12
+ when /\ACouldn't find all .* with \'.*\': ((?:\d+(?:, )?)+)\z/
13
+ $1.split(', ')
14
+ end
15
+
16
+ raise HumanError::Errors::ResourceNotFoundError.new(
17
+ resource_name: human_error_resource_name(self),
18
+ resource_id: ids)
19
+ end
20
+ end
21
+
3
22
  def save!(*args)
4
23
  super
5
24
  rescue ActiveRecord::InvalidForeignKey => e
@@ -9,43 +28,31 @@ module Persistable
9
28
  [1..-1]
10
29
 
11
30
  raise HumanError::Errors::AssociationError.new(
12
- resource_name: human_error_resource_name,
31
+ resource_name: human_error_resource_name(self.class),
13
32
  association_name: association_name,
14
33
  association_id: association_id,
15
34
  attributes: attributes)
16
35
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
17
36
  raise HumanError::Errors::ResourcePersistenceError.new(
18
- resource_name: human_error_resource_name,
37
+ resource_name: human_error_resource_name(self.class),
19
38
  attributes: attributes,
20
39
  errors: errors.full_messages)
21
40
  end
22
41
 
23
- def find(*ids)
24
- super
25
- rescue ActiveRecord::RecordNotFound => e
26
- ids = case e.message
27
- when /\ACouldn't find .* without an ID\z/
28
- []
29
- when /\ACouldn't find .* with \'.*\'=(\d+)\z/
30
- [$1]
31
- when /\ACouldn't find all .* with \'.*\': ((?:\d+(?:, )?)+)\z/
32
- $1.split(', ')
33
- end
34
-
35
- raise HumanError::Errors::ResourceNotFoundError.new(
36
- resource_name: human_error_resource_name,
37
- resource_id: ids)
38
- end
39
-
40
- def human_error_resource_name
42
+ def human_error_resource_name(klass)
41
43
  last_part_of_class_name = /::(\w+)\z/
42
44
 
43
- self.
44
- class.
45
+ klass.
45
46
  name[last_part_of_class_name, 1].
46
47
  underscore.
47
48
  humanize.
48
49
  downcase
49
50
  end
51
+
52
+ module_function :human_error_resource_name
53
+
54
+ def self.included(base)
55
+ base.extend ClassMethods
56
+ end
50
57
  end
51
58
  end
@@ -1,3 +1,3 @@
1
1
  class HumanError
2
- VERSION = '1.8.0'
2
+ VERSION = '1.8.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jfelchner