hanami-utils 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,7 +26,8 @@ module Hanami
26
26
  # Test::TEST_VALUE = 'redefined'
27
27
  # end
28
28
  def self.silence_warnings
29
- old_verbose, $VERBOSE = $VERBOSE, nil
29
+ old_verbose = $VERBOSE
30
+ $VERBOSE = nil
30
31
  yield
31
32
  ensure
32
33
  $VERBOSE = old_verbose
@@ -34,4 +35,3 @@ module Hanami
34
35
  end
35
36
  end
36
37
  end
37
-
@@ -0,0 +1,51 @@
1
+ begin
2
+ require 'multi_json'
3
+ rescue LoadError
4
+ require 'json'
5
+ end
6
+
7
+ module Hanami
8
+ module Utils
9
+ # JSON wrapper
10
+ #
11
+ # If you use MultiJson gem this wrapper will use it.
12
+ # Otherwise - JSON std lib.
13
+ #
14
+ # @since 0.8.0
15
+ module Json
16
+ # rubocop:disable Style/ClassVars
17
+ if defined?(MultiJson)
18
+ @@engine = MultiJson
19
+ ParserError = MultiJson::ParseError
20
+ else
21
+ @@engine = ::JSON
22
+ ParserError = ::JSON::ParserError
23
+ end
24
+ # rubocop:enable Style/ClassVars
25
+
26
+ # Load the given JSON payload into Ruby objects.
27
+ #
28
+ # @param payload [String] a JSON payload
29
+ #
30
+ # @return [Object] the result of the loading process
31
+ #
32
+ # @raise [Hanami::Utils::Json::ParserError] if the paylod is invalid
33
+ #
34
+ # @since 0.8.0
35
+ def self.load(payload)
36
+ @@engine.load(payload)
37
+ end
38
+
39
+ # Dump the given object into a JSON payload
40
+ #
41
+ # @param object [Object] any object
42
+ #
43
+ # @return [String] the result of the dumping process
44
+ #
45
+ # @since 0.8.0
46
+ def self.dump(object)
47
+ @@engine.dump(object)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -17,6 +17,9 @@ module Hanami
17
17
  module Utils
18
18
  # Kernel utilities
19
19
  # @since 0.1.1
20
+ #
21
+ # rubocop:disable Style/MethodName
22
+ # rubocop:disable Metrics/ModuleLength
20
23
  module Kernel
21
24
  # Matcher for numeric values
22
25
  #
@@ -24,13 +27,13 @@ module Hanami
24
27
  # @api private
25
28
  #
26
29
  # @see Hanami::Utils::Kernel.Integer
27
- NUMERIC_MATCHER = /\A([\d\/\.\+iE]+|NaN|Infinity)\z/.freeze
30
+ NUMERIC_MATCHER = %r{\A([\d\/\.\+iE]+|NaN|Infinity)\z}
28
31
 
29
- # @since x.x.x
32
+ # @since 0.8.0
30
33
  # @api private
31
34
  BOOLEAN_FALSE_STRING = '0'.freeze
32
35
 
33
- # @since x.x.x
36
+ # @since 0.8.0
34
37
  # @api private
35
38
  BOOLEAN_TRUE_INTEGER = 1
36
39
 
@@ -323,7 +326,7 @@ module Hanami
323
326
  # # big complex represented as a string
324
327
  # input = Complex(2, 3)
325
328
  # Hanami::Utils::Kernel.Integer(input) # => TypeError
326
- def self.Integer(arg)
329
+ def self.Integer(arg) # rubocop:disable Metrics/MethodLength
327
330
  super(arg)
328
331
  rescue ArgumentError, TypeError, NoMethodError
329
332
  begin
@@ -343,7 +346,6 @@ module Hanami
343
346
  # Coerces the argument to be a BigDecimal.
344
347
  #
345
348
  # @param arg [Object] the argument
346
- # @param precision [Keyword] precision for Rational objects (Only JRuby).
347
349
  #
348
350
  # @return [BigDecimal] the result of the coercion
349
351
  #
@@ -539,7 +541,7 @@ module Hanami
539
541
  # # big complex represented as a string
540
542
  # input = Complex(2, 3)
541
543
  # Hanami::Utils::Kernel.Float(input) # => TypeError
542
- def self.Float(arg)
544
+ def self.Float(arg) # rubocop:disable Metrics/MethodLength
543
545
  super(arg)
544
546
  rescue ArgumentError, TypeError
545
547
  begin
@@ -888,7 +890,7 @@ module Hanami
888
890
  # # Missing #respond_to?
889
891
  # input = BasicObject.new
890
892
  # Hanami::Utils::Kernel.Boolean(input) # => TypeError
891
- def self.Boolean(arg)
893
+ def self.Boolean(arg) # rubocop:disable Metrics/MethodLength
892
894
  case arg
893
895
  when Numeric
894
896
  arg.to_i == BOOLEAN_TRUE_INTEGER
@@ -1019,7 +1021,7 @@ module Hanami
1019
1021
  #
1020
1022
  # @return [TrueClass,FalseClass]
1021
1023
  #
1022
- # @since x.x.x
1024
+ # @since 0.8.0
1023
1025
  # @api private
1024
1026
  def self.numeric?(arg)
1025
1027
  !(arg.to_s =~ NUMERIC_MATCHER).nil?
@@ -1034,11 +1036,11 @@ module Hanami
1034
1036
  # @since 0.4.3
1035
1037
  # @api private
1036
1038
  def self.inspect_type_error(arg)
1037
- (arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) << " "
1039
+ (arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) << ' '
1038
1040
  rescue NoMethodError => _
1039
1041
  # missing the #respond_to? method, fall back to returning the class' name
1040
1042
  begin
1041
- arg.class.name << " instance "
1043
+ arg.class.name << ' instance '
1042
1044
  rescue NoMethodError
1043
1045
  # missing the #class method, can't fall back to anything better than nothing
1044
1046
  # Callers will have to guess from their code
@@ -1052,4 +1054,3 @@ module Hanami
1052
1054
  end
1053
1055
  end
1054
1056
  end
1055
-
@@ -110,12 +110,12 @@ module Hanami
110
110
  # paths = Hanami::Utils::LoadPaths.new
111
111
  # paths << '.' << '../..'
112
112
  def push(*paths)
113
- @paths.push(*paths)
113
+ @paths.push(*paths) # rubocop:disable Performance/PushSplat
114
114
  @paths = Kernel.Array(@paths)
115
115
  self
116
116
  end
117
117
 
118
- alias_method :<<, :push
118
+ alias << push
119
119
 
120
120
  # It freezes the object by preventing further modifications.
121
121
  #
@@ -149,11 +149,13 @@ module Hanami
149
149
  end
150
150
 
151
151
  protected
152
+
152
153
  # @since 0.6.0
153
154
  # @api private
154
155
  attr_reader :paths
155
156
 
156
157
  private
158
+
157
159
  # Allow subclasses to define their own policy to discover the realpath
158
160
  # of the given path.
159
161
  #
@@ -83,7 +83,7 @@ module Hanami
83
83
  result = [prefix, strings]
84
84
  result.flatten!
85
85
  result.compact!
86
- result.reject! {|string| string == separator }
86
+ result.reject! { |string| string == separator }
87
87
 
88
88
  self.class.new(
89
89
  result.join(separator), separator
@@ -131,13 +131,14 @@ module Hanami
131
131
  #
132
132
  # @see #relative
133
133
  def relative!
134
- @string.gsub!(%r{(?<!:)#{ separator * 2 }}, separator)
135
- @string.sub!(%r{\A#{ separator }}, '')
134
+ @string.gsub!(/(?<!:)#{separator * 2}/, separator)
135
+ @string.sub!(/\A#{separator}/, '')
136
136
 
137
137
  self
138
138
  end
139
139
 
140
140
  private
141
+
141
142
  # @since 0.1.0
142
143
  # @api private
143
144
  attr_reader :separator
@@ -5,7 +5,7 @@ module Hanami
5
5
  # String on steroids
6
6
  #
7
7
  # @since 0.1.0
8
- class String
8
+ class String # rubocop:disable Metrics/ClassLength
9
9
  # Empty string for #classify
10
10
  #
11
11
  # @since 0.6.0
@@ -46,7 +46,7 @@ module Hanami
46
46
  #
47
47
  # @since 0.3.0
48
48
  # @api private
49
- UNDERSCORE_DIVISION_TARGET = '\1_\2'.freeze
49
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'.freeze
50
50
 
51
51
  # Separator for #titleize
52
52
  #
@@ -141,7 +141,7 @@ module Hanami
141
141
  # string = Hanami::Utils::String.new 'hanami_utils'
142
142
  # string.classify # => 'HanamiUtils'
143
143
  def classify
144
- words = split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize)
144
+ words = underscore.split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize)
145
145
  delimiters = scan(CLASSIFY_WORD_SEPARATOR)
146
146
 
147
147
  delimiters.map! do |delimiter|
@@ -251,9 +251,10 @@ module Hanami
251
251
  # # =>
252
252
  # 'Hanami::Utils'
253
253
  # 'Hanami::App'
254
- def tokenize
255
- if match = TOKENIZE_REGEXP.match(@string)
256
- pre, post = match.pre_match, match.post_match
254
+ def tokenize # rubocop:disable Metrics/MethodLength
255
+ if match = TOKENIZE_REGEXP.match(@string) # rubocop:disable Lint/AssignmentInCondition
256
+ pre = match.pre_match
257
+ post = match.post_match
257
258
  tokens = match[1].split(TOKENIZE_SEPARATOR)
258
259
  tokens.each do |token|
259
260
  yield(self.class.new("#{pre}#{token}#{post}"))
@@ -307,7 +308,7 @@ module Hanami
307
308
  @string
308
309
  end
309
310
 
310
- alias_method :to_str, :to_s
311
+ alias to_str to_s
311
312
 
312
313
  # Equality
313
314
  #
@@ -318,7 +319,7 @@ module Hanami
318
319
  to_s == other
319
320
  end
320
321
 
321
- alias_method :eql?, :==
322
+ alias eql? ==
322
323
 
323
324
  # Split the string with the given pattern
324
325
  #
@@ -382,7 +383,7 @@ module Hanami
382
383
  # puts result
383
384
  # # => #<Hanami::Utils::String:0x007fdb41232ed0 @string="authors/books#index">
384
385
  def rsub(pattern, replacement)
385
- if i = rindex(pattern)
386
+ if i = rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
386
387
  s = @string.dup
387
388
  s[i] = replacement
388
389
  self.class.new s
@@ -403,7 +404,7 @@ module Hanami
403
404
  s = self.class.new(s) if s.is_a?(::String)
404
405
  s
405
406
  else
406
- raise NoMethodError.new(%(undefined method `#{ m }' for "#{ @string }":#{ self.class }))
407
+ raise NoMethodError.new(%(undefined method `#{m}' for "#{@string}":#{self.class}))
407
408
  end
408
409
  end
409
410
 
@@ -411,7 +412,7 @@ module Hanami
411
412
  #
412
413
  # @api private
413
414
  # @since 0.3.0
414
- def respond_to_missing?(m, include_private=false)
415
+ def respond_to_missing?(m, include_private = false)
415
416
  @string.respond_to?(m, include_private)
416
417
  end
417
418
  end
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.7.2'.freeze
6
+ VERSION = '0.8.0'.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.7.2
4
+ version: 0.8.0
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-05-20 00:00:00.000000000 Z
13
+ date: 2016-07-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -32,28 +32,14 @@ dependencies:
32
32
  requirements:
33
33
  - - "~>"
34
34
  - !ruby/object:Gem::Version
35
- version: '10'
35
+ version: '11'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - "~>"
41
41
  - !ruby/object:Gem::Version
42
- version: '10'
43
- - !ruby/object:Gem::Dependency
44
- name: minitest
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '5.4'
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - "~>"
55
- - !ruby/object:Gem::Version
56
- version: '5.4'
42
+ version: '11'
57
43
  description: Hanami utilities
58
44
  email:
59
45
  - me@lucaguidi.com
@@ -73,6 +59,7 @@ files:
73
59
  - lib/hanami/utils.rb
74
60
  - lib/hanami/utils/attributes.rb
75
61
  - lib/hanami/utils/basic_object.rb
62
+ - lib/hanami/utils/blank.rb
76
63
  - lib/hanami/utils/callbacks.rb
77
64
  - lib/hanami/utils/class.rb
78
65
  - lib/hanami/utils/class_attribute.rb
@@ -82,6 +69,7 @@ files:
82
69
  - lib/hanami/utils/hash.rb
83
70
  - lib/hanami/utils/inflector.rb
84
71
  - lib/hanami/utils/io.rb
72
+ - lib/hanami/utils/json.rb
85
73
  - lib/hanami/utils/kernel.rb
86
74
  - lib/hanami/utils/load_paths.rb
87
75
  - lib/hanami/utils/path_prefix.rb
@@ -99,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
87
  requirements:
100
88
  - - ">="
101
89
  - !ruby/object:Gem::Version
102
- version: 2.0.0
90
+ version: 2.2.0
103
91
  required_rubygems_version: !ruby/object:Gem::Requirement
104
92
  requirements:
105
93
  - - ">="
@@ -112,4 +100,3 @@ signing_key:
112
100
  specification_version: 4
113
101
  summary: Ruby core extentions and Hanami utilities
114
102
  test_files: []
115
- has_rdoc: