hanami-utils 0.9.2 → 1.0.0.beta1

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
2
  SHA1:
3
- metadata.gz: 9fed837067b96edc2d9f3b4e3a571e8fb9220dde
4
- data.tar.gz: 69075fd83da706506ead576f6040384ab2c75a98
3
+ metadata.gz: 2ea1ca32894baa365b51feaeddc616f6fa55cc82
4
+ data.tar.gz: abb789173257a8b131d66a0aa6d1d2e161e1f549
5
5
  SHA512:
6
- metadata.gz: 5aae813a308bc47f00c68cbeccde7bbe60c797a5c179c1e5e8637ed20dcdb20700b3888cc3901ade12125438960af64edd30779f1af6483347ae100316d91bd8
7
- data.tar.gz: e9b15c795ed49e894014b58d7c32ee55259a80a665726658fc72d1f4fdff1183061b3136fcc2231fdf2daf9e10065e54b734ec2d3edc5cac0d6204a33655b5f2
6
+ metadata.gz: 212099661a2b3be79ca7c2218d5fda126331db1fe6b2625164dac6e2dbf0f1d9bd4f869e31d7c1fc227f96a926815408954abf160a8df852ebbb490ed70fb101
7
+ data.tar.gz: c21fbd4cfafd99203ae7b796a91d130ba72d382a18a0f57146c7d86fa375b30944f55060d701020b51c4c6af11dd50155ea40bb26397bfabe2ea42bc3c2cc2f0
data/CHANGELOG.md CHANGED
@@ -1,14 +1,31 @@
1
1
  # Hanami::Utils
2
2
  Ruby core extentions and class utilities for Hanami
3
3
 
4
+ ## v1.0.0.beta1 - 2017-02-14
5
+ ### Added
6
+ - [Luca Guidi] Official support for Ruby: MRI 2.4
7
+ - [alexd16] Introduced `Utils::Hash#deep_symbolize!` for deep symbolization
8
+ - [Luca Guidi] Introduced `Hanami::Utils.reload!` as a mechanism to force code reloading in development
9
+
10
+ ### Fixed
11
+ - [alexd16 & Alfonso Uceda & Luca Guidi] Don't deeply symbolize `Hanami::Interactor::Result` payload
12
+ – [Alfonso Uceda] `Hanami::Interactor::Result`: Don't transform objects that respond to `#to_hash` (like entities)
13
+ - [Bhanu Prakash] Use `Utils::Json.generate` instead of the deprecated `.dump` for `Hanami::Logger` JSON formatter
14
+ - [Luca Guidi] `Hanami::Logger`: when a `Hash` message is passed, don't nest it under `:message` key, but unwrap at the top level
15
+
16
+ ### Changed
17
+ - [alexd16] `Utils::Hash#symbolize!` no longer symbolizes deep structures
18
+ - [Luca Guidi & Alfonso Uceda] Improve readability for default logger formatter
19
+ - [Luca Guidi] Use ISO-8601 time format for JSON logger formatter
20
+
4
21
  ## v0.9.2 - 2016-12-19
5
- ## Added
22
+ ### Added
6
23
  - [Grachev Mikhail] Introduced `Hanami::Interactor::Result#failure?`
7
24
 
8
- ## Fixed
25
+ ### Fixed
9
26
  - [Paweł Świątkowski] `Utils::Inflector.pluralize` Pluralize -en to -ens instead of -ina
10
27
 
11
- ## Changed
28
+ ### Changed
12
29
  - [Grachev Mikhail] Deprecate `Hanami::Interactor::Result#failing?` in favor of `#failure?`
13
30
 
14
31
  ## v0.9.1 - 2016-11-18
@@ -37,7 +37,7 @@ module Hanami
37
37
  # @since 0.3.5
38
38
  # @api private
39
39
  def initialize(payload = {})
40
- @payload = _payload(payload)
40
+ @payload = payload
41
41
  @errors = []
42
42
  @success = true
43
43
  end
@@ -125,7 +125,7 @@ module Hanami
125
125
  # @since 0.3.5
126
126
  # @api private
127
127
  def prepare!(payload)
128
- @payload.merge!(_payload(payload))
128
+ @payload.merge!(payload)
129
129
  self
130
130
  end
131
131
 
@@ -144,12 +144,6 @@ module Hanami
144
144
  METHODS[method_name] || @payload.key?(method_name)
145
145
  end
146
146
 
147
- # @since 0.3.5
148
- # @api private
149
- def _payload(payload)
150
- Utils::Hash.new(payload).symbolize!
151
- end
152
-
153
147
  # @since 0.3.5
154
148
  # @api private
155
149
  def __inspect
@@ -492,7 +486,7 @@ module Hanami
492
486
  include Utils::ClassAttribute
493
487
 
494
488
  class_attribute :exposures
495
- self.exposures = Utils::Hash.new
489
+ self.exposures = {}
496
490
  end
497
491
  end
498
492
 
@@ -527,7 +521,7 @@ module Hanami
527
521
  # result.params # => NoMethodError
528
522
  def expose(*instance_variable_names)
529
523
  instance_variable_names.each do |name|
530
- exposures[name] = "@#{name}"
524
+ exposures[name.to_sym] = "@#{name}"
531
525
  end
532
526
  end
533
527
  end
data/lib/hanami/logger.rb CHANGED
@@ -108,6 +108,10 @@ module Hanami
108
108
  # @api private
109
109
  NEW_LINE = $/
110
110
 
111
+ # @since 1.0.0.beta1
112
+ # @api private
113
+ RESERVED_KEYS = [:app, :severity, :time].freeze
114
+
111
115
  include Utils::ClassAttribute
112
116
 
113
117
  class_attribute :subclasses
@@ -137,15 +141,19 @@ module Hanami
137
141
  # @api private
138
142
  attr_writer :application_name
139
143
 
144
+ # @since 1.0.0.beta1
145
+ # @api private
146
+ attr_reader :application_name
147
+
140
148
  # @since 0.5.0
141
149
  # @api private
142
150
  #
143
151
  # @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html#method-i-call
144
152
  def call(severity, time, _progname, msg)
145
153
  _format({
146
- app: @application_name,
154
+ app: application_name,
147
155
  severity: severity,
148
- time: time.utc
156
+ time: time
149
157
  }.merge(
150
158
  _message_hash(msg)
151
159
  ))
@@ -173,7 +181,27 @@ module Hanami
173
181
  # @since 0.8.0
174
182
  # @api private
175
183
  def _format(hash)
176
- hash.map { |k, v| "#{k}=#{v}" }.join(SEPARATOR) + NEW_LINE
184
+ result = RESERVED_KEYS.map { |k| "[#{hash[k]}]" }.join(SEPARATOR)
185
+ return _format_error(result, hash) if hash.key?(:error)
186
+
187
+ values = hash.each_with_object([]) do |(k, v), memo|
188
+ memo << v unless RESERVED_KEYS.include?(k)
189
+ end
190
+
191
+ result << " #{values.join(SEPARATOR)}#{NEW_LINE}"
192
+ result
193
+ end
194
+
195
+ def _format_error(result, hash)
196
+ result << " #{hash[:error]}:" if hash.key?(:error)
197
+ result << " #{hash[:message]}#{NEW_LINE}"
198
+ if hash.key?(:backtrace)
199
+ hash[:backtrace].each do |line|
200
+ result << "from #{line}#{NEW_LINE}"
201
+ end
202
+ end
203
+
204
+ result
177
205
  end
178
206
  end
179
207
 
@@ -192,7 +220,8 @@ module Hanami
192
220
  # @since 0.8.0
193
221
  # @api private
194
222
  def _format(hash)
195
- Hanami::Utils::Json.dump(hash)
223
+ hash[:time] = hash[:time].utc.iso8601
224
+ Hanami::Utils::Json.generate(hash) + NEW_LINE
196
225
  end
197
226
  end
198
227
 
@@ -201,7 +230,7 @@ module Hanami
201
230
  #
202
231
  # @since 0.5.0
203
232
  # @api private
204
- DEFAULT_APPLICATION_NAME = 'Hanami'.freeze
233
+ DEFAULT_APPLICATION_NAME = 'hanami'.freeze
205
234
 
206
235
  # @since 0.8.0
207
236
  # @api private
data/lib/hanami/utils.rb CHANGED
@@ -49,11 +49,45 @@ module Hanami
49
49
  #
50
50
  # @since 0.9.0
51
51
  def self.require!(directory)
52
+ for_each_file_in(directory) { |file| require_relative(file) }
53
+ end
54
+
55
+ # Recursively reload Ruby files under the given directory.
56
+ #
57
+ # If the directory is relative, it implies it's the path from current directory.
58
+ # If the directory is absolute, it uses as it is.
59
+ #
60
+ # It respects file separator of the current operating system.
61
+ # A pattern like <tt>"path/to/files"</tt> will work both on *NIX and Windows machines.
62
+ #
63
+ # @param directory [String, Pathname] the directory
64
+ #
65
+ # @since 1.0.0.beta1
66
+ # @api private
67
+ def self.reload!(directory)
68
+ for_each_file_in(directory) { |file| load(file) }
69
+ end
70
+
71
+ # Recursively scans through the given directory and yields the given block
72
+ # for each Ruby source file.
73
+ #
74
+ # If the directory is relative, it implies it's the path from current directory.
75
+ # If the directory is absolute, it uses as it is.
76
+ #
77
+ # It respects file separator of the current operating system.
78
+ # A pattern like <tt>"path/to/files"</tt> will work both on *NIX and Windows machines.
79
+ #
80
+ # @param directory [String, Pathname] the directory
81
+ # @param blk [Proc] the block to yield
82
+ #
83
+ # @since 1.0.0.beta1
84
+ # @api private
85
+ def self.for_each_file_in(directory, &blk)
52
86
  directory = directory.to_s.gsub(%r{(\/|\\)}, File::SEPARATOR)
53
87
  directory = Pathname.new(Dir.pwd).join(directory).to_s
54
88
  directory = File.join(directory, '**', '*.rb') unless directory =~ /(\*\*)/
55
89
 
56
- FileList[directory].each { |file| require_relative file }
90
+ FileList[directory].each(&blk)
57
91
  end
58
92
  end
59
93
  end
@@ -35,9 +35,9 @@ module Hanami
35
35
  __id__
36
36
  end
37
37
 
38
- # Interface for <tt>pp</pp>
38
+ # Interface for pp
39
39
  #
40
- # @return [String] the pretty printable inspection of the object
40
+ # @return [String] the pretty-printable inspection of the object
41
41
  #
42
42
  # @since 0.9.0
43
43
  #
@@ -48,7 +48,7 @@ module Hanami
48
48
  @hash.default_proc = blk
49
49
  end
50
50
 
51
- # Convert in-place all the keys to Symbol instances, nested hashes are converted too.
51
+ # Convert in-place all the keys to Symbol instances.
52
52
  #
53
53
  # @return [Hash] self
54
54
  #
@@ -61,11 +61,34 @@ module Hanami
61
61
  # hash.symbolize!
62
62
  #
63
63
  # hash.keys # => [:a, :b]
64
- # hash.inspect # => {:a=>23, :b=>{:c=>["x", "y", "z"]}}
64
+ # hash.inspect # => { :a => 23, :b => { 'c' => ["x", "y", "z"] } }
65
65
  def symbolize!
66
66
  keys.each do |k|
67
67
  v = delete(k)
68
- v = self.class.new(v).symbolize! if v.respond_to?(:to_hash)
68
+ self[k.to_sym] = v
69
+ end
70
+
71
+ self
72
+ end
73
+
74
+ # Convert in-place all the keys to Symbol instances, nested hashes are converted too.
75
+ #
76
+ # @return [Hash] self
77
+ #
78
+ # @since 1.0.0.beta1
79
+ #
80
+ # @example
81
+ # require 'hanami/utils/hash'
82
+ #
83
+ # hash = Hanami::Utils::Hash.new 'a' => 23, 'b' => { 'c' => ['x','y','z'] }
84
+ # hash.deep_symbolize!
85
+ #
86
+ # hash.keys # => [:a, :b]
87
+ # hash.inspect # => {:a=>23, :b=>{:c=>["x", "y", "z"]}}
88
+ def deep_symbolize!
89
+ keys.each do |k|
90
+ v = delete(k)
91
+ v = self.class.new(v).deep_symbolize! if v.respond_to?(:to_hash)
69
92
 
70
93
  self[k.to_sym] = v
71
94
  end
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.9.2'.freeze
6
+ VERSION = '1.0.0.beta1'.freeze
7
7
  end
8
8
  end
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: 0.9.2
4
+ version: 1.0.0.beta1
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: 2016-12-19 00:00:00.000000000 Z
13
+ date: 2017-02-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -91,9 +91,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
91
  version: 2.3.0
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - ">"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 1.3.1
97
97
  requirements: []
98
98
  rubyforge_project:
99
99
  rubygems_version: 2.6.8