koine-attributes 1.0.0 → 1.1.0

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
- SHA256:
3
- metadata.gz: 72df551f32d3b9c9d8611e78b1f7cdcc49be6acd6d0265a3730f6f312871812a
4
- data.tar.gz: 5b526a287781a9ec4b6d46315276aa9fc0c87473f435929d871bd6e203a31b8f
2
+ SHA1:
3
+ metadata.gz: 4861f47589ca3917acd1004bfa66db499eda711f
4
+ data.tar.gz: 1d5f6a2798c9e655b5fcd4659f65cb1d92ff2683
5
5
  SHA512:
6
- metadata.gz: 1e722c8108a3364615f7c8f68581881d007b35b43ba2c85904edb09eeb88b8c20afd2638468023021c1c243277e944f421f3a293997eef41574f26553449af4a
7
- data.tar.gz: e5265fe8f2d642cdc7fa6657dc8a76747f1c8028b063be2bc7db2963389a6f1a7eb351ee05d2fb012323f469ca05b84ef8e8137d3486e9112c53d18e9b8a2287
6
+ metadata.gz: 05b50951cb2f648ae979f2c23a7c09f17b442c23ceca28e98fe11e34e1cd25906b4e920ce056b3c9144cad36b719b33fe66d87ab56a19fdaa4c4431efd85e44f
7
+ data.tar.gz: db69bb585e013ed1f1ea95e05c6b550011864e79f2e3b687535bab52a3e6f45fc0dbee50c81a8a5ca684bebeb3cead9dcce929b1e4c43f2a88aa5f31c5e03117
data/README.md CHANGED
@@ -145,6 +145,8 @@ product.price = "100 USD"
145
145
 
146
146
  ```ruby
147
147
  class MyCustom::Money
148
+ attr_accessor :attribute_name
149
+
148
150
  def default_value
149
151
  return 'some default_value'
150
152
  end
@@ -2,6 +2,12 @@ module Koine
2
2
  module Attributes
3
3
  module Adapter
4
4
  class Base
5
+ attr_accessor :attribute_name
6
+
7
+ def initialize
8
+ @attribute_name = 'AnonymousAttribute'
9
+ end
10
+
5
11
  def coerce(value)
6
12
  return coerce_nil if value.nil?
7
13
 
@@ -34,7 +40,7 @@ module Koine
34
40
  return @nil_value.respond_to?(:call) ? @nil_value.call : @nil_value
35
41
  end
36
42
 
37
- raise ArgumentError, 'Cannot be nil'
43
+ raise ArgumentError, 'Cannot be nil', attribute_name
38
44
  end
39
45
 
40
46
  def coerce_not_nil(_value)
@@ -51,6 +57,12 @@ module Koine
51
57
 
52
58
  value.freeze
53
59
  end
60
+
61
+ def wrap_errors
62
+ yield
63
+ rescue StandardError => error
64
+ raise Koine::Attributes::ArgumentError.new(error, attribute_name)
65
+ end
54
66
  end
55
67
  end
56
68
  end
@@ -5,10 +5,12 @@ module Koine
5
5
  private
6
6
 
7
7
  def coerce_not_nil(date)
8
- secure do
9
- next date if date.is_a?(::Date)
10
- next date.to_date if date.is_a?(::Time)
11
- ::Date.parse(date)
8
+ wrap_errors do
9
+ secure do
10
+ next date if date.is_a?(::Date)
11
+ next date.to_date if date.is_a?(::Time)
12
+ ::Date.parse(date)
13
+ end
12
14
  end
13
15
  end
14
16
  end
@@ -5,7 +5,7 @@ module Koine
5
5
  private
6
6
 
7
7
  def coerce_not_nil(value)
8
- Float(value)
8
+ wrap_errors { Float(value) }
9
9
  end
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module Koine
5
5
  private
6
6
 
7
7
  def coerce_not_nil(value)
8
- Integer(value)
8
+ wrap_errors { Integer(value) }
9
9
  end
10
10
  end
11
11
  end
@@ -7,9 +7,11 @@ module Koine
7
7
  private
8
8
 
9
9
  def coerce_not_nil(value)
10
- secure do
11
- next value if value.is_a?(::Time)
12
- ::Time.parse(value)
10
+ wrap_errors do
11
+ secure do
12
+ next value if value.is_a?(::Time)
13
+ ::Time.parse(value)
14
+ end
13
15
  end
14
16
  end
15
17
  end
@@ -0,0 +1,19 @@
1
+ module Koine
2
+ module Attributes
3
+ class ArgumentError < ::ArgumentError
4
+ attr_reader :attribute_name
5
+
6
+ def initialize(error, attribute_name = nil)
7
+ @attribute_name = attribute_name
8
+
9
+ if error.is_a?(Exception)
10
+ super(error.message)
11
+ set_backtrace(error.backtrace)
12
+ return
13
+ end
14
+
15
+ super(error)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -12,6 +12,7 @@ module Koine
12
12
 
13
13
  def add_attribute(name, adapter, &block)
14
14
  adapter = coerce_adapter(adapter)
15
+ adapter.attribute_name = name
15
16
  yield(adapter) if block
16
17
  @adapters[name.to_sym] = adapter.freeze
17
18
  end
@@ -1,5 +1,5 @@
1
1
  module Koine
2
2
  module Attributes
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require 'koine/attributes/version'
2
2
  require 'koine/attributes/adapter/base'
3
+ require 'koine/attributes/argument_error'
3
4
 
4
5
  # provides the following API
5
6
  #
@@ -139,7 +140,7 @@ module Koine
139
140
  define_method(:initialize) { |*args| attributes.initialize_values(*args) }
140
141
 
141
142
  define_method(:inspect) do
142
- hex_id = '%x' % (object_id << 1)
143
+ hex_id = format('%x', (object_id << 1))
143
144
  "#<#{self.class}:0x00#{hex_id} @attributes=#{attributes.to_h.inspect}>"
144
145
  end
145
146
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koine-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
@@ -138,6 +138,7 @@ files:
138
138
  - lib/koine/attributes/adapter/string.rb
139
139
  - lib/koine/attributes/adapter/symbol.rb
140
140
  - lib/koine/attributes/adapter/time.rb
141
+ - lib/koine/attributes/argument_error.rb
141
142
  - lib/koine/attributes/attributes.rb
142
143
  - lib/koine/attributes/attributes_factory.rb
143
144
  - lib/koine/attributes/hash_helper.rb
@@ -163,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
164
  version: '0'
164
165
  requirements: []
165
166
  rubyforge_project:
166
- rubygems_version: 2.7.6
167
+ rubygems_version: 2.6.14.1
167
168
  signing_key:
168
169
  specification_version: 4
169
170
  summary: Stronger setters and getters