hanami-utils 1.0.0.beta2 → 1.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/LICENSE.md +1 -1
- data/README.md +1 -5
- data/lib/hanami/interactor.rb +0 -13
- data/lib/hanami/logger.rb +11 -7
- data/lib/hanami/utils/blank.rb +1 -0
- data/lib/hanami/utils/callbacks.rb +10 -4
- data/lib/hanami/utils/class_attribute.rb +3 -1
- data/lib/hanami/utils/deprecation.rb +1 -0
- data/lib/hanami/utils/escape.rb +1 -1
- data/lib/hanami/utils/inflector.rb +4 -3
- data/lib/hanami/utils/json.rb +0 -46
- data/lib/hanami/utils/kernel.rb +5 -8
- data/lib/hanami/utils/version.rb +1 -1
- metadata +2 -3
- data/lib/hanami/utils/attributes.rb +0 -141
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 627e0486c780c11f9ce000f6d80b2a8b691b5e45
|
4
|
+
data.tar.gz: 033e15d05ac40fb8ad76174108a4b54b0aeb21ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bc330a6116872fb4a8d072d74fe99d8b961b1c78cdb4e9237b4153283148e704fa3a0d60369b23cb3b733076b7e200fe4ddaafa908a928cc12c3e6e3d1f92e5
|
7
|
+
data.tar.gz: 68670773d3fa4ef2b4d30030fdeb08f52d752a46366c94dd9f8abf0713d95ed91fc33b524ef7c3998bc274bee677a18fe32944f56b34e70dcc431a7a00d0766b
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
# Hanami::Utils
|
2
2
|
Ruby core extentions and class utilities for Hanami
|
3
3
|
|
4
|
+
## v1.0.0.beta3 - 2017-03-17
|
5
|
+
### Fixed
|
6
|
+
- [Luca Guidi] Use `$stdout` instead of `STDOUT` as default stream for `Hanami::Logger`
|
7
|
+
|
8
|
+
### Changed
|
9
|
+
- [Luca Guidi] Removed `Utils::Attributes`
|
10
|
+
- [Luca Guidi] Removed deprecated `Hanami::Interactor::Result#failing?`
|
11
|
+
- [Luca Guidi] Removed deprecated `Utils::Json.load` and `.dump`
|
12
|
+
|
4
13
|
## v1.0.0.beta2 - 2017-03-02
|
5
14
|
### Changed
|
6
15
|
- [Anton Davydov] Made `Utils::Blank` private API
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -55,10 +55,6 @@ Standardized Service Object with small interface and rich returning result. [[AP
|
|
55
55
|
|
56
56
|
Enhanced version of Ruby's `Logger`. [[API doc](http://www.rubydoc.info/gems/hanami-utils/Hanami/Logger)]
|
57
57
|
|
58
|
-
### Hanami::Utils::Attributes
|
59
|
-
|
60
|
-
Set of attributes with indifferent access. [[API doc](http://www.rubydoc.info/gems/hanami-utils/Hanami/Utils/Attributes)]
|
61
|
-
|
62
58
|
### Hanami::Utils::BasicObject
|
63
59
|
|
64
60
|
Enhanced version of Ruby's `BasicObject`. [[API doc](http://www.rubydoc.info/gems/hanami-utils/Hanami/Utils/BasicObject)]
|
@@ -134,6 +130,6 @@ __Hanami::Utils__ uses [Semantic Versioning 2.0.0](http://semver.org)
|
|
134
130
|
|
135
131
|
## Copyright
|
136
132
|
|
137
|
-
Copyright © 2014-
|
133
|
+
Copyright © 2014-2017 Luca Guidi – Released under MIT License
|
138
134
|
|
139
135
|
This project was formerly known as Lotus (`lotus-utils`).
|
data/lib/hanami/interactor.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'hanami/utils/basic_object'
|
2
2
|
require 'hanami/utils/class_attribute'
|
3
3
|
require 'hanami/utils/hash'
|
4
|
-
require 'hanami/utils/deprecation'
|
5
4
|
|
6
5
|
module Hanami
|
7
6
|
# Hanami Interactor
|
@@ -54,18 +53,6 @@ module Hanami
|
|
54
53
|
# @since 0.3.5
|
55
54
|
alias success? successful?
|
56
55
|
|
57
|
-
# Check if the current status is not successful
|
58
|
-
#
|
59
|
-
# @return [TrueClass,FalseClass] the result of the check
|
60
|
-
#
|
61
|
-
# @since 0.9.2
|
62
|
-
#
|
63
|
-
# @deprecated Use {#failure?} instead
|
64
|
-
def failing?
|
65
|
-
Utils::Deprecation.new("`Hanami::Interactor::Result#failing?' is deprecated, please use `Hanami::Interactor::Result#failure?'")
|
66
|
-
failure?
|
67
|
-
end
|
68
|
-
|
69
56
|
# Check if the current status is not successful
|
70
57
|
#
|
71
58
|
# @return [TrueClass,FalseClass] the result of the check
|
data/lib/hanami/logger.rb
CHANGED
@@ -39,12 +39,12 @@ module Hanami
|
|
39
39
|
# And if you want to use custom formatter you need create new class inherited from
|
40
40
|
# `Formatter` class and define `_format` private method like this:
|
41
41
|
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
42
|
+
# class CustomFormatter < Formatter
|
43
|
+
# private
|
44
|
+
# def _format(hash)
|
45
|
+
# # ...
|
46
|
+
# end
|
46
47
|
# end
|
47
|
-
# end
|
48
48
|
#
|
49
49
|
# @since 0.5.0
|
50
50
|
#
|
@@ -128,11 +128,13 @@ module Hanami
|
|
128
128
|
end.tap { |f| f.application_name = application_name }
|
129
129
|
end
|
130
130
|
|
131
|
+
# @api private
|
131
132
|
def self.inherited(subclass)
|
132
133
|
super
|
133
134
|
subclasses << subclass
|
134
135
|
end
|
135
136
|
|
137
|
+
# @api private
|
136
138
|
def self.eligible?(name)
|
137
139
|
name == :default
|
138
140
|
end
|
@@ -192,6 +194,7 @@ module Hanami
|
|
192
194
|
result
|
193
195
|
end
|
194
196
|
|
197
|
+
# @api private
|
195
198
|
def _format_error(result, hash)
|
196
199
|
result << " #{hash[:error]}:" if hash.key?(:error)
|
197
200
|
result << " #{hash[:message]}#{NEW_LINE}"
|
@@ -211,6 +214,7 @@ module Hanami
|
|
211
214
|
# @since 0.5.0
|
212
215
|
# @api private
|
213
216
|
class JSONFormatter < Formatter
|
217
|
+
# @api private
|
214
218
|
def self.eligible?(name)
|
215
219
|
name == :json
|
216
220
|
end
|
@@ -253,10 +257,10 @@ module Hanami
|
|
253
257
|
# tagging purposes
|
254
258
|
#
|
255
259
|
# @param stream [String, IO, StringIO, Pathname] an optional log stream. This is a filename
|
256
|
-
# (String) or IO object (typically
|
260
|
+
# (String) or IO object (typically `$stdout`, `$stderr`, or an open file).
|
257
261
|
#
|
258
262
|
# @since 0.5.0
|
259
|
-
def initialize(application_name = nil, stream:
|
263
|
+
def initialize(application_name = nil, stream: $stdout, level: DEBUG, formatter: nil)
|
260
264
|
super(stream)
|
261
265
|
|
262
266
|
@level = _level(level)
|
data/lib/hanami/utils/blank.rb
CHANGED
@@ -170,6 +170,7 @@ module Hanami
|
|
170
170
|
|
171
171
|
private
|
172
172
|
|
173
|
+
# @api private
|
173
174
|
def callables(callbacks, block)
|
174
175
|
callbacks.push(block) if block
|
175
176
|
callbacks.map { |c| Factory.fabricate(c) }
|
@@ -179,7 +180,7 @@ module Hanami
|
|
179
180
|
# Callback factory
|
180
181
|
#
|
181
182
|
# @since 0.1.0
|
182
|
-
# @private
|
183
|
+
# @api private
|
183
184
|
class Factory
|
184
185
|
# Instantiates a `Callback` according to if it responds to #call.
|
185
186
|
#
|
@@ -213,8 +214,9 @@ module Hanami
|
|
213
214
|
# It wraps an object that responds to #call
|
214
215
|
#
|
215
216
|
# @since 0.1.0
|
216
|
-
# @private
|
217
|
+
# @api private
|
217
218
|
class Callback
|
219
|
+
# @api private
|
218
220
|
attr_reader :callback
|
219
221
|
|
220
222
|
# Initialize by wrapping the given callback
|
@@ -224,7 +226,7 @@ module Hanami
|
|
224
226
|
# @return [Callback] self
|
225
227
|
#
|
226
228
|
# @since 0.1.0
|
227
|
-
#
|
229
|
+
# @api private
|
228
230
|
def initialize(callback)
|
229
231
|
@callback = callback
|
230
232
|
end
|
@@ -237,6 +239,7 @@ module Hanami
|
|
237
239
|
# @return [void, Object] It may return a value, it depends on the callback.
|
238
240
|
#
|
239
241
|
# @since 0.1.0
|
242
|
+
# @api private
|
240
243
|
#
|
241
244
|
# @see Hanami::Utils::Callbacks::Chain#run
|
242
245
|
def call(context, *args)
|
@@ -248,7 +251,7 @@ module Hanami
|
|
248
251
|
# It wraps a symbol or a string representing a method name that is implemented by the context within it will be called.
|
249
252
|
#
|
250
253
|
# @since 0.1.0
|
251
|
-
# @private
|
254
|
+
# @api private
|
252
255
|
class MethodCallback < Callback
|
253
256
|
# Executes the callback within the given context and eventually passing the given arguments.
|
254
257
|
# Those arguments will be passed according to the arity of the target method.
|
@@ -259,6 +262,7 @@ module Hanami
|
|
259
262
|
# @return [void, Object] It may return a value, it depends on the callback.
|
260
263
|
#
|
261
264
|
# @since 0.1.0
|
265
|
+
# @api private
|
262
266
|
#
|
263
267
|
# @see Hanami::Utils::Callbacks::Chain#run
|
264
268
|
def call(context, *args)
|
@@ -271,10 +275,12 @@ module Hanami
|
|
271
275
|
end
|
272
276
|
end
|
273
277
|
|
278
|
+
# @api private
|
274
279
|
def hash
|
275
280
|
callback.hash
|
276
281
|
end
|
277
282
|
|
283
|
+
# @api private
|
278
284
|
def eql?(other)
|
279
285
|
hash == other.hash
|
280
286
|
end
|
@@ -8,6 +8,7 @@ module Hanami
|
|
8
8
|
#
|
9
9
|
# @see Hanami::Utils::ClassAttribute::ClassMethods
|
10
10
|
module ClassAttribute
|
11
|
+
# @api private
|
11
12
|
def self.included(base)
|
12
13
|
base.extend ClassMethods
|
13
14
|
end
|
@@ -75,6 +76,7 @@ module Hanami
|
|
75
76
|
protected
|
76
77
|
|
77
78
|
# @see Class#inherited
|
79
|
+
# @api private
|
78
80
|
def inherited(subclass)
|
79
81
|
class_attributes.each do |attr|
|
80
82
|
value = send(attr)
|
@@ -89,7 +91,7 @@ module Hanami
|
|
89
91
|
private
|
90
92
|
|
91
93
|
# Class accessor for class attributes.
|
92
|
-
# @private
|
94
|
+
# @api private
|
93
95
|
def class_attributes
|
94
96
|
@class_attributes ||= Set.new
|
95
97
|
end
|
data/lib/hanami/utils/escape.rb
CHANGED
@@ -415,7 +415,7 @@ module Hanami
|
|
415
415
|
# <div><script>alert(1);</script></div>
|
416
416
|
#
|
417
417
|
# @example Bad practice
|
418
|
-
# # WRONG Use Escape.html_attribute
|
418
|
+
# # WRONG Use Escape.html_attribute instead
|
419
419
|
# <a title="<%= Hanami::Utils::Escape.html('...') %>">link</a>
|
420
420
|
def self.html(input)
|
421
421
|
input = encode(input)
|
@@ -6,8 +6,6 @@ module Hanami
|
|
6
6
|
# String inflector
|
7
7
|
#
|
8
8
|
# @since 0.4.1
|
9
|
-
#
|
10
|
-
# rubocop:disable Style/PerlBackrefs
|
11
9
|
module Inflector # rubocop:disable Metrics/ModuleLength
|
12
10
|
# Rules for irregular plurals
|
13
11
|
#
|
@@ -347,6 +345,7 @@ module Hanami
|
|
347
345
|
# rubocop:disable Metrics/AbcSize
|
348
346
|
# rubocop:disable Metrics/CyclomaticComplexity
|
349
347
|
# rubocop:disable Metrics/MethodLength
|
348
|
+
# rubocop:disable Style/PerlBackrefs
|
350
349
|
def self.pluralize(string)
|
351
350
|
return string if string.nil? || string =~ Utils::Blank::STRING_MATCHER
|
352
351
|
|
@@ -385,6 +384,7 @@ module Hanami
|
|
385
384
|
string + S
|
386
385
|
end
|
387
386
|
end
|
387
|
+
# rubocop:enable Style/PerlBackrefs
|
388
388
|
# rubocop:enable Metrics/AbcSize
|
389
389
|
# rubocop:enable Metrics/CyclomaticComplexity
|
390
390
|
# rubocop:enable Metrics/MethodLength
|
@@ -402,6 +402,7 @@ module Hanami
|
|
402
402
|
# rubocop:disable Metrics/CyclomaticComplexity
|
403
403
|
# rubocop:disable Metrics/MethodLength
|
404
404
|
# rubocop:disable Metrics/PerceivedComplexity
|
405
|
+
# rubocop:disable Style/PerlBackrefs
|
405
406
|
def self.singularize(string)
|
406
407
|
return string if string.nil? || string =~ Utils::Blank::STRING_MATCHER
|
407
408
|
|
@@ -448,11 +449,11 @@ module Hanami
|
|
448
449
|
string.chop
|
449
450
|
end
|
450
451
|
end
|
452
|
+
# rubocop:enable Style/PerlBackrefs
|
451
453
|
# rubocop:enable Metrics/AbcSize
|
452
454
|
# rubocop:enable Metrics/CyclomaticComplexity
|
453
455
|
# rubocop:enable Metrics/MethodLength
|
454
456
|
# rubocop:enable Metrics/PerceivedComplexity
|
455
457
|
end
|
456
|
-
# rubocop:enable Style/PerlBackrefs
|
457
458
|
end
|
458
459
|
end
|
data/lib/hanami/utils/json.rb
CHANGED
@@ -4,8 +4,6 @@ rescue LoadError
|
|
4
4
|
require 'json'
|
5
5
|
end
|
6
6
|
|
7
|
-
require 'hanami/utils/deprecation'
|
8
|
-
|
9
7
|
module Hanami
|
10
8
|
module Utils
|
11
9
|
# JSON wrapper
|
@@ -26,20 +24,6 @@ module Hanami
|
|
26
24
|
MultiJson.load(payload)
|
27
25
|
end
|
28
26
|
|
29
|
-
# FIXME: remove this alias, when Hanami::Utils::Json.load will be removed
|
30
|
-
#
|
31
|
-
# @since 0.9.1
|
32
|
-
# @api private
|
33
|
-
alias load parse
|
34
|
-
|
35
|
-
# FIXME: remove this method, when Hanami::Utils::Json.dump will be removed
|
36
|
-
#
|
37
|
-
# @since 0.9.1
|
38
|
-
# @api private
|
39
|
-
def dump(object)
|
40
|
-
generate(object)
|
41
|
-
end
|
42
|
-
|
43
27
|
# @since 0.9.1
|
44
28
|
# @api private
|
45
29
|
def generate(object)
|
@@ -57,22 +41,6 @@ module Hanami
|
|
57
41
|
end
|
58
42
|
# rubocop:enable Style/ClassVars
|
59
43
|
|
60
|
-
# Load the given JSON payload into Ruby objects.
|
61
|
-
#
|
62
|
-
# @param payload [String] a JSON payload
|
63
|
-
#
|
64
|
-
# @return [Object] the result of the loading process
|
65
|
-
#
|
66
|
-
# @raise [Hanami::Utils::Json::ParserError] if the paylod is invalid
|
67
|
-
#
|
68
|
-
# @since 0.8.0
|
69
|
-
#
|
70
|
-
# @deprecated Use {.parse} instead
|
71
|
-
def self.load(payload)
|
72
|
-
Hanami::Utils::Deprecation.new("`Hanami::Utils::Json.load' is deprecated, please use `Hanami::Utils::Json.parse'")
|
73
|
-
@@engine.load(payload)
|
74
|
-
end
|
75
|
-
|
76
44
|
# Parse the given JSON paylod
|
77
45
|
#
|
78
46
|
# @param payload [String] a JSON payload
|
@@ -86,20 +54,6 @@ module Hanami
|
|
86
54
|
@@engine.parse(payload)
|
87
55
|
end
|
88
56
|
|
89
|
-
# Dump the given object into a JSON payload
|
90
|
-
#
|
91
|
-
# @param object [Object] any object
|
92
|
-
#
|
93
|
-
# @return [String] the result of the dumping process
|
94
|
-
#
|
95
|
-
# @since 0.8.0
|
96
|
-
#
|
97
|
-
# @deprecated Use {.generate} instead
|
98
|
-
def self.dump(object)
|
99
|
-
Hanami::Utils::Deprecation.new("`Hanami::Utils::Json.dump' is deprecated, please use `Hanami::Utils::Json.generate'")
|
100
|
-
@@engine.dump(object)
|
101
|
-
end
|
102
|
-
|
103
57
|
# Generate a JSON document from the given object
|
104
58
|
#
|
105
59
|
# @param object [Object] any object
|
data/lib/hanami/utils/kernel.rb
CHANGED
@@ -6,11 +6,11 @@ require 'bigdecimal'
|
|
6
6
|
require 'hanami/utils'
|
7
7
|
require 'hanami/utils/string'
|
8
8
|
|
9
|
-
# Define top level constant Boolean, so it can be easily used by other libraries
|
10
|
-
# in coercions DSLs
|
11
|
-
#
|
12
|
-
# @since 0.3.0
|
13
9
|
unless defined?(Boolean)
|
10
|
+
# Define top level constant Boolean, so it can be easily used by other libraries
|
11
|
+
# in coercions DSLs
|
12
|
+
#
|
13
|
+
# @since 0.3.0
|
14
14
|
class Boolean
|
15
15
|
end
|
16
16
|
end
|
@@ -19,10 +19,7 @@ module Hanami
|
|
19
19
|
module Utils
|
20
20
|
# Kernel utilities
|
21
21
|
# @since 0.1.1
|
22
|
-
#
|
23
|
-
# rubocop:disable Style/MethodName
|
24
|
-
# rubocop:disable Metrics/ModuleLength
|
25
|
-
module Kernel
|
22
|
+
module Kernel # rubocop:disable Metrics/ModuleLength
|
26
23
|
# Matcher for numeric values
|
27
24
|
#
|
28
25
|
# @since 0.3.3
|
data/lib/hanami/utils/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.beta3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-03-
|
13
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -57,7 +57,6 @@ files:
|
|
57
57
|
- lib/hanami/interactor.rb
|
58
58
|
- lib/hanami/logger.rb
|
59
59
|
- lib/hanami/utils.rb
|
60
|
-
- lib/hanami/utils/attributes.rb
|
61
60
|
- lib/hanami/utils/basic_object.rb
|
62
61
|
- lib/hanami/utils/blank.rb
|
63
62
|
- lib/hanami/utils/callbacks.rb
|
@@ -1,141 +0,0 @@
|
|
1
|
-
require 'hanami/utils/hash'
|
2
|
-
|
3
|
-
module Hanami
|
4
|
-
module Utils
|
5
|
-
# A set of attributes.
|
6
|
-
#
|
7
|
-
# It internally stores the data as a Hash.
|
8
|
-
#
|
9
|
-
# All the operations convert keys to strings.
|
10
|
-
# This strategy avoids memory attacks due to Symbol abuses when parsing
|
11
|
-
# untrusted input.
|
12
|
-
#
|
13
|
-
# At the same time, this allows to get/set data with the original key or
|
14
|
-
# with the string representation. See the examples below.
|
15
|
-
#
|
16
|
-
# @since 0.3.2
|
17
|
-
class Attributes
|
18
|
-
# Initialize a set of attributes
|
19
|
-
# All the keys of the given Hash are recursively converted to strings.
|
20
|
-
#
|
21
|
-
# @param hash [#to_h] a Hash or any object that implements #to_h
|
22
|
-
#
|
23
|
-
# @return [Hanami::Utils::Attributes] self
|
24
|
-
#
|
25
|
-
# @since 0.3.2
|
26
|
-
#
|
27
|
-
# @example
|
28
|
-
# require 'hanami/utils/attributes'
|
29
|
-
#
|
30
|
-
# attributes = Hanami::Utils::Attributes.new(a: 1, b: { 2 => [3, 4] })
|
31
|
-
# attributes.to_h # => { "a" => 1, "b" => { "2" => [3, 4] } }
|
32
|
-
def initialize(hash = {})
|
33
|
-
@attributes = Utils::Hash.new(hash, &nil).stringify!
|
34
|
-
end
|
35
|
-
|
36
|
-
# Get the value associated with the given attribute
|
37
|
-
#
|
38
|
-
# @param attribute [#to_s] a String or any object that implements #to_s
|
39
|
-
#
|
40
|
-
# @return [Object,NilClass] the associated value, if present
|
41
|
-
#
|
42
|
-
# @since 0.3.2
|
43
|
-
#
|
44
|
-
# @example
|
45
|
-
# require 'hanami/utils/attributes'
|
46
|
-
#
|
47
|
-
# attributes = Hanami::Utils::Attributes.new(a: 1, 'b' => 2, 23 => 'foo')
|
48
|
-
#
|
49
|
-
# attributes.get(:a) # => 1
|
50
|
-
# attributes.get('a') # => 1
|
51
|
-
# attributes[:a] # => 1
|
52
|
-
# attributes['a'] # => 1
|
53
|
-
#
|
54
|
-
# attributes.get(:b) # => 2
|
55
|
-
# attributes.get('b') # => 2
|
56
|
-
# attributes[:b] # => 2
|
57
|
-
# attributes['b'] # => 2
|
58
|
-
#
|
59
|
-
# attributes.get(23) # => "foo"
|
60
|
-
# attributes.get('23') # => "foo"
|
61
|
-
# attributes[23] # => "foo"
|
62
|
-
# attributes['23'] # => "foo"
|
63
|
-
#
|
64
|
-
# attributes.get(:unknown) # => nil
|
65
|
-
# attributes.get('unknown') # => nil
|
66
|
-
# attributes[:unknown] # => nil
|
67
|
-
# attributes['unknown'] # => nil
|
68
|
-
def get(attribute)
|
69
|
-
value = @attributes
|
70
|
-
|
71
|
-
keys = attribute.to_s.split('.')
|
72
|
-
keys.each do |key|
|
73
|
-
break unless value
|
74
|
-
|
75
|
-
value = value[key]
|
76
|
-
end
|
77
|
-
|
78
|
-
value.is_a?(Hash) ? self.class.new(value) : value
|
79
|
-
end
|
80
|
-
|
81
|
-
# @since 0.3.4
|
82
|
-
alias [] get
|
83
|
-
|
84
|
-
# Set the given value for the given attribute
|
85
|
-
#
|
86
|
-
# @param attribute [#to_s] a String or any object that implements #to_s
|
87
|
-
# @param value [Object] any value
|
88
|
-
#
|
89
|
-
# @return [NilClass]
|
90
|
-
#
|
91
|
-
# @since 0.3.2
|
92
|
-
#
|
93
|
-
# @example
|
94
|
-
# require 'hanami/utils/attributes'
|
95
|
-
#
|
96
|
-
# attributes = Hanami::Utils::Attributes.new
|
97
|
-
#
|
98
|
-
# attributes.set(:a, 1)
|
99
|
-
# attributes.get(:a) # => 1
|
100
|
-
# attributes.get('a') # => 1
|
101
|
-
#
|
102
|
-
# attributes.set('b', 2)
|
103
|
-
# attributes.get(:b) # => 2
|
104
|
-
# attributes.get('b') # => 2
|
105
|
-
#
|
106
|
-
# attributes.set(23, 'foo')
|
107
|
-
# attributes.get(23) # => "foo"
|
108
|
-
# attributes.get('23') # => "foo"
|
109
|
-
def set(attribute, value)
|
110
|
-
@attributes[attribute.to_s] = value
|
111
|
-
nil
|
112
|
-
end
|
113
|
-
|
114
|
-
# Returns a deep duplicated copy of the attributes as a Hash
|
115
|
-
#
|
116
|
-
# @return [::Hash]
|
117
|
-
#
|
118
|
-
# @since 0.3.2
|
119
|
-
def to_h
|
120
|
-
::Hash[].tap do |result|
|
121
|
-
@attributes.each do |k, v|
|
122
|
-
result[k] = _read_value(v)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
private
|
128
|
-
|
129
|
-
# @since 0.4.1
|
130
|
-
# @api private
|
131
|
-
def _read_value(value)
|
132
|
-
case val = value
|
133
|
-
when ::Hash, ::Hanami::Utils::Hash, ->(v) { v.respond_to?(:hanami_nested_attributes?) }
|
134
|
-
val.to_h
|
135
|
-
else
|
136
|
-
val
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|