lotus-utils 0.2.0 → 0.3.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
2
  SHA1:
3
- metadata.gz: 1937436164015390ea262191b1535441167f1e9c
4
- data.tar.gz: 65bb8ce1f1d128fdca98e1918f96c08cf9fa4175
3
+ metadata.gz: 1d04858b6a58f09fa11b7067a9f10b4b936e724b
4
+ data.tar.gz: 0e7c601a620a91a58516869b34de12013c86d599
5
5
  SHA512:
6
- metadata.gz: aace7eede8b7e773ea4401d70e5537e89a965d5ed0000ffe9767ad5f78bcd5629080c17755770f3a4358fa61adb63da604df641a685073be6ea2fef9eb44bd88
7
- data.tar.gz: 380a90d0b5df6cc0e52bf4bfea010f55e57fc207864268d39d3ba3f6d3f3e86541951fda027edbcef7da968be26779ab88b5e22dfb508b8a8fca86c72638568d
6
+ metadata.gz: 50dd0f5ae40cec11a6e3b06275988fb1a1535ce42007aaccdf84ba6ec12ff000f9e6ddc67bfb90dab041e5565769cf8c116ffd227f3cdff804cdfeb4ce13101a
7
+ data.tar.gz: 4136424d1f2efa2082b58adc347ffecedbc4f14200e7cf03609222364a870722e50c2f1e891bddf154bb075fdd0ac1d5072ab67718ca1920430010d52ad29e96
data/CHANGELOG.md CHANGED
@@ -1,3 +1,38 @@
1
+ ## v0.3.0
2
+ ### Oct 23, 2014
3
+
4
+ 4f7a8cf 2014-10-22 **Luca Guidi** Use 1/3 less objects when using Lotus::Utils::Kernel.Array
5
+
6
+ a82437d 2014-10-20 **Luca Guidi** Improved error message for Utils::Class.load!
7
+
8
+ 2a7134d 2014-10-17 **Tom Kadwill** Improved error NameError message by passing in the whole constant name
9
+
10
+ 52c9fe2 2014-10-16 **Luca Guidi** Utils::Hash#to_h return instances of ::Hash in case of nested symbolized data structure
11
+
12
+ 6b9ba7b 2014-09-23 **Luca Guidi** Raise TypeError if nil is passed for PathPrefix#relative_join
13
+
14
+ 28d119f 2014-08-19 **Celso Fernandes** Add BigDecimal coercion to Lotus::Utils::Kernel
15
+
16
+ a1b7213 2014-08-18 **Luca Guidi** Ensure PathPrefix#relative_join will raise a meaningful error
17
+
18
+ 139c4d5 2014-08-03 **Luca Guidi** Define Boolean, if missing :airplane:
19
+
20
+ 520c021 2014-07-29 **Peter Suschlik** Speedup Lotus::Utils::String#tokenize
21
+
22
+ 2380d62 2014-07-29 **Peter Suschlik** Define Lotus::Utils::Hash#respond_to_missing?
23
+
24
+ 1c300af 2014-07-29 **Peter Suschlik** Define Lotus::Utils::String#responds_to_missing?
25
+
26
+ 46b39e3 2014-07-25 **Luca Guidi** Let Utils::Hash#inspect output to be the same of ::Hash#inspect
27
+
28
+ 5b3e790 2014-07-25 **Luca Guidi** Use composition over inheritance for Lotus::Utils::PathPrefix
29
+
30
+ 6333287 2014-07-09 **Luca Guidi** Specify ::Hash in Lotus::Utils::Kernel.Hash
31
+
32
+ 25e101e 2014-07-09 **Luca Guidi** Use composition over inheritance for Lotus::Utils::Hash
33
+
34
+ dea7e6b 2014-07-07 **Luca Guidi** Lotus::Utils::String is now a wrapper around a string, instead of inheriting from String.
35
+
1
36
  ## v0.2.0
2
37
  ### Jun 23, 2014
3
38
 
data/README.md CHANGED
@@ -9,7 +9,7 @@ Ruby core extentions and class utilities for [Lotus](http://lotusrb.org)
9
9
  [![Coverage](http://img.shields.io/coveralls/lotus/utils/master.svg)](https://coveralls.io/r/lotus/utils)
10
10
  [![Code Climate](http://img.shields.io/codeclimate/github/lotus/utils.svg)](https://codeclimate.com/github/lotus/utils)
11
11
  [![Dependencies](http://img.shields.io/gemnasium/lotus/utils.svg)](https://gemnasium.com/lotus/utils)
12
- [![Inline Docs](http://inch-pages.github.io/github/lotus/utils.svg)](http://inch-pages.github.io/github/lotus/utils)
12
+ [![Inline Docs](http://inch-ci.org/github/lotus/utils.svg)](http://inch-ci.org/github/lotus/utils)
13
13
 
14
14
  ## Contact
15
15
 
@@ -22,13 +22,15 @@ Ruby core extentions and class utilities for [Lotus](http://lotusrb.org)
22
22
 
23
23
  ## Rubies
24
24
 
25
- __Lotus::Utils__ supports Ruby (MRI) 2+
25
+ __Lotus::Utils__ supports Ruby (MRI) 2+ and JRuby 1.7 (with 2.0 mode).
26
26
 
27
27
  ## Installation
28
28
 
29
29
  Add this line to your application's Gemfile:
30
30
 
31
- gem 'lotus-utils'
31
+ ```ruby
32
+ gem 'lotus-utils'
33
+ ```
32
34
 
33
35
  And then execute:
34
36
 
@@ -113,7 +113,7 @@ module Lotus
113
113
  #
114
114
  # @since 0.2.0
115
115
  #
116
- # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-freeze
116
+ # @see http://ruby-doc.org/core/Object.html#method-i-freeze
117
117
  #
118
118
  # @example
119
119
  # require 'lotus/utils/callbacks'
@@ -225,7 +225,6 @@ module Lotus
225
225
  end
226
226
  end
227
227
 
228
- protected
229
228
  def hash
230
229
  callback.hash
231
230
  end
@@ -52,7 +52,8 @@ module Lotus
52
52
  end
53
53
  end
54
54
 
55
- raise NameError.new(name)
55
+ full_name = [ (namespace == Object ? nil : namespace), name ].compact.join('::')
56
+ raise NameError.new("uninitialized constant #{ full_name }")
56
57
  end
57
58
  end
58
59
  end
@@ -8,9 +8,7 @@ module Lotus
8
8
  # @see Lotus::Utils::ClassAttribute::ClassMethods
9
9
  module ClassAttribute
10
10
  def self.included(base)
11
- base.class_eval do
12
- extend ClassMethods
13
- end
11
+ base.extend ClassMethods
14
12
  end
15
13
 
16
14
  module ClassMethods
@@ -64,7 +62,7 @@ module Lotus
64
62
  # SmallAirplane.engines # => 2
65
63
  # SmallAirplane.wheels # => 8
66
64
  def class_attribute(*attributes)
67
- (class << self; self; end).class_eval do
65
+ singleton_class.class_eval do
68
66
  attr_accessor *attributes
69
67
  end
70
68
 
@@ -2,16 +2,34 @@ module Lotus
2
2
  module Utils
3
3
  # Hash on steroids
4
4
  # @since 0.1.0
5
- class Hash < ::Hash
5
+ class Hash
6
6
  # Initialize the hash
7
7
  #
8
- # @param hash [::Hash, Hash] the value we want to use to initialize this instance
8
+ # @param hash [#to_h] the value we want to use to initialize this instance
9
+ # @param blk [Proc] define the default value
9
10
  #
10
- # @return [Hash] self
11
+ # @return [Lotus::Utils::Hash] self
11
12
  #
12
13
  # @since 0.1.0
13
- def initialize(hash = {})
14
- merge! hash
14
+ #
15
+ # @see http://www.ruby-doc.org/core/Hash.html#method-c-5B-5D
16
+ #
17
+ # @example Passing a Hash
18
+ # require 'lotus/utils/hash'
19
+ #
20
+ # hash = Lotus::Utils::Hash.new('l' => 23)
21
+ # hash['l'] # => 23
22
+ #
23
+ # @example Passing a block for default
24
+ # require 'lotus/utils/hash'
25
+ #
26
+ # hash = Lotus::Utils::Hash.new {|h,k| h[k] = [] }
27
+ # hash['foo'].push 'bar'
28
+ #
29
+ # hash.to_h # => { 'foo' => ['bar'] }
30
+ def initialize(hash = {}, &blk)
31
+ @hash = hash.to_h
32
+ @hash.default_proc = blk
15
33
  end
16
34
 
17
35
  # Convert in-place all the keys to Symbol instances, nested hashes are converted too.
@@ -38,6 +56,132 @@ module Lotus
38
56
 
39
57
  self
40
58
  end
59
+
60
+ # Returns a new array populated with the keys from this hash
61
+ #
62
+ # @return [Array] the keys
63
+ #
64
+ # @since 0.3.0
65
+ #
66
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-keys
67
+ def keys
68
+ @hash.keys
69
+ end
70
+
71
+ # Deletes the key-value pair and returns the value from hsh whose key is
72
+ # equal to key.
73
+ #
74
+ # @param key [Object] the key to remove
75
+ #
76
+ # @return [Object,nil] the value hold by the given key, if present
77
+ #
78
+ # @since 0.3.0
79
+ #
80
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-keys
81
+ def delete(key)
82
+ @hash.delete(key)
83
+ end
84
+
85
+ # Retrieves the value object corresponding to the key object.
86
+ #
87
+ # @param key [Object] the key
88
+ #
89
+ # @return [Object,nil] the correspoding value, if present
90
+ #
91
+ # @since 0.3.0
92
+ #
93
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-5B-5D
94
+ def [](key)
95
+ @hash[key]
96
+ end
97
+
98
+ # Associates the value given by value with the key given by key.
99
+ #
100
+ # @param key [Object] the key to assign
101
+ # @param value [Object] the value to assign
102
+ #
103
+ # @since 0.3.0
104
+ #
105
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-5B-5D-3D
106
+ def []=(key, value)
107
+ @hash[key] = value
108
+ end
109
+
110
+ # Returns a Ruby Hash as duplicated version of self
111
+ #
112
+ # @return [::Hash] the hash
113
+ #
114
+ # @since 0.3.0
115
+ #
116
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-to_h
117
+ def to_h
118
+ @hash.each_with_object({}) do |(k, v), result|
119
+ v = v.to_h if v.is_a?(self.class)
120
+ result[k] = v
121
+ end
122
+ end
123
+
124
+ alias_method :to_hash, :to_h
125
+
126
+ # Converts into a nested array of [ key, value ] arrays.
127
+ #
128
+ # @return [::Array] the array
129
+ #
130
+ # @since 0.3.0
131
+ #
132
+ # @see http://www.ruby-doc.org/core/Hash.html#method-i-to_a
133
+ def to_a
134
+ @hash.to_a
135
+ end
136
+
137
+ # Equality
138
+ #
139
+ # @return [TrueClass,FalseClass]
140
+ #
141
+ # @since 0.3.0
142
+ def ==(other)
143
+ @hash == other.to_h
144
+ end
145
+
146
+ alias_method :eql?, :==
147
+
148
+ # Returns the hash of the internal @hash
149
+ #
150
+ # @return [Fixnum]
151
+ #
152
+ # @since 0.3.0
153
+ def hash
154
+ @hash.hash
155
+ end
156
+
157
+ # Returns a string describing the internal @hash
158
+ #
159
+ # @return [String]
160
+ #
161
+ # @since 0.3.0
162
+ def inspect
163
+ @hash.inspect
164
+ end
165
+
166
+ # Override Ruby's method_missing in order to provide ::Hash interface
167
+ #
168
+ # @api private
169
+ # @since 0.3.0
170
+ def method_missing(m, *args, &blk)
171
+ h = @hash.__send__(m, *args, &blk)
172
+ h = self.class.new(h) if h.is_a?(::Hash)
173
+ h
174
+ rescue NoMethodError
175
+ raise NoMethodError.new(%(undefined method `#{ m }' for #{ @hash }:#{ self.class }))
176
+ end
177
+
178
+ # Override Ruby's respond_to_missing? in order to support ::Hash interface
179
+ #
180
+ # @api private
181
+ # @since 0.3.0
182
+ def respond_to_missing?(m, include_private=false)
183
+ @hash.respond_to?(m, include_private)
184
+ end
41
185
  end
42
186
  end
43
187
  end
@@ -7,9 +7,9 @@ module Lotus
7
7
  # Decreases the level of verbosity, during the execution of the given block.
8
8
  #
9
9
  # Revised version of ActiveSupport's `Kernel.with_warnings` implementation
10
- # @see https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L25
10
+ # @see https://github.com/rails/rails/blob/v4.1.2/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L25
11
11
  #
12
- # @param blk [Proc] the block of code that generates warnings.
12
+ # @yield the block of code that generates warnings.
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -25,9 +25,9 @@ module Lotus
25
25
  # Lotus::Utils::IO.silence_warnings do
26
26
  # Test::TEST_VALUE = 'redefined'
27
27
  # end
28
- def self.silence_warnings(&blk)
28
+ def self.silence_warnings
29
29
  old_verbose, $VERBOSE = $VERBOSE, nil
30
- blk.call
30
+ yield
31
31
  ensure
32
32
  $VERBOSE = old_verbose
33
33
  end
@@ -2,13 +2,21 @@ require 'set'
2
2
  require 'date'
3
3
  require 'time'
4
4
  require 'pathname'
5
+ require 'bigdecimal'
6
+
7
+ # Define top level constant Boolean, so it can be easily used by other libraries
8
+ # in coercions DSLs
9
+ #
10
+ # @since 0.3.0
11
+ class Boolean
12
+ end unless defined?(Boolean)
5
13
 
6
14
  module Lotus
7
15
  module Utils
8
16
  # Kernel utilities
9
17
  # @since 0.1.1
10
18
  module Kernel
11
- # Coerces the argument to be an array.
19
+ # Coerces the argument to be an Array.
12
20
  #
13
21
  # It's similar to Ruby's Kernel.Array, but it applies further
14
22
  # transformations:
@@ -23,11 +31,11 @@ module Lotus
23
31
  #
24
32
  # @since 0.1.1
25
33
  #
26
- # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Array
34
+ # @see http://www.ruby-doc.org/core/Kernel.html#method-i-Array
27
35
  #
28
- # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-flatten
29
- # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-compact
30
- # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-uniq
36
+ # @see http://www.ruby-doc.org/core/Array.html#method-i-flatten
37
+ # @see http://www.ruby-doc.org/core/Array.html#method-i-compact
38
+ # @see http://www.ruby-doc.org/core/Array.html#method-i-uniq
31
39
  #
32
40
  # @example Basic Usage
33
41
  # require 'lotus/utils/kernel'
@@ -62,10 +70,14 @@ module Lotus
62
70
  # response = Response.new(200, {}, 'hello')
63
71
  # Lotus::Utils::Kernel.Array(response) # => [200, {}, "hello"]
64
72
  def self.Array(arg)
65
- super(arg).flatten.compact.uniq
73
+ super(arg).dup.tap do |a|
74
+ a.flatten!
75
+ a.compact!
76
+ a.uniq!
77
+ end
66
78
  end
67
79
 
68
- # Coerces the argument to be a set.
80
+ # Coerces the argument to be a Set.
69
81
  #
70
82
  # @param arg [Object] the input
71
83
  #
@@ -122,7 +134,7 @@ module Lotus
122
134
  raise TypeError.new("can't convert into Set")
123
135
  end
124
136
 
125
- # Coerces the argument to be an hash.
137
+ # Coerces the argument to be a Hash.
126
138
  #
127
139
  # @param arg [Object] the input
128
140
  #
@@ -132,7 +144,7 @@ module Lotus
132
144
  #
133
145
  # @since 0.1.1
134
146
  #
135
- # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Hash
147
+ # @see http://www.ruby-doc.org/core/Kernel.html#method-i-Hash
136
148
  #
137
149
  # @example Basic Usage
138
150
  # require 'lotus/utils/kernel'
@@ -190,7 +202,7 @@ module Lotus
190
202
  def self.Hash(arg)
191
203
  case arg
192
204
  when ::Hash then arg
193
- when ::Array, ::Set then Hash[*self.Array(arg)]
205
+ when ::Array, ::Set then ::Hash[*self.Array(arg)]
194
206
  when ->(a) { a.respond_to?(:to_h) } then arg.to_h
195
207
  else
196
208
  super(arg)
@@ -200,7 +212,7 @@ module Lotus
200
212
  end
201
213
  end
202
214
 
203
- # Coerces the argument to be an integer.
215
+ # Coerces the argument to be an Integer.
204
216
  #
205
217
  # It's similar to Ruby's Kernel.Integer, but it doesn't stop at the first
206
218
  # error and raise an exception only when the argument can't be coerced.
@@ -213,7 +225,7 @@ module Lotus
213
225
  #
214
226
  # @since 0.1.1
215
227
  #
216
- # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Integer
228
+ # @see http://www.ruby-doc.org/core/Kernel.html#method-i-Integer
217
229
  #
218
230
  # @example Basic Usage
219
231
  # require 'bigdecimal'
@@ -305,7 +317,7 @@ module Lotus
305
317
  # Lotus::Utils::Kernel.Integer(input) # => TypeError
306
318
  def self.Integer(arg)
307
319
  super(arg)
308
- rescue ArgumentError, TypeError
320
+ rescue ArgumentError, TypeError, NoMethodError
309
321
  begin
310
322
  if arg.respond_to?(:to_i)
311
323
  arg.to_i
@@ -319,7 +331,88 @@ module Lotus
319
331
  raise TypeError.new "can't convert into Integer"
320
332
  end
321
333
 
322
- # Coerces the argument to be a float.
334
+ # Coerces the argument to be a BigDecimal.
335
+ #
336
+ # @param arg [Object] the argument
337
+ #
338
+ # @return [BigDecimal] the result of the coercion
339
+ #
340
+ # @raise [TypeError] if the argument can't be coerced
341
+ #
342
+ # @since 0.3.0
343
+ #
344
+ # @see http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/BigDecimal.html
345
+ #
346
+ # @example Basic Usage
347
+ # require 'lotus/utils/kernel'
348
+ #
349
+ # Lotus::Utils::Kernel.BigDecimal(1) # => 1
350
+ # Lotus::Utils::Kernel.BigDecimal(1.2) # => 1
351
+ # Lotus::Utils::Kernel.BigDecimal(011) # => 9
352
+ # Lotus::Utils::Kernel.BigDecimal(0xf5) # => 245
353
+ # Lotus::Utils::Kernel.BigDecimal("1") # => 1
354
+ # Lotus::Utils::Kernel.BigDecimal(Rational(0.3)) # => 0.3
355
+ # Lotus::Utils::Kernel.BigDecimal(Complex(0.3)) # => 0.3
356
+ # Lotus::Utils::Kernel.BigDecimal(BigDecimal.new(12.00001)) # => 12.00001
357
+ # Lotus::Utils::Kernel.BigDecimal(176605528590345446089)
358
+ # # => 176605528590345446089
359
+ #
360
+ # @example BigDecimal Interface
361
+ # require 'lotus/utils/kernel'
362
+ #
363
+ # UltimateAnswer = Struct.new(:question) do
364
+ # def to_d
365
+ # BigDecimal.new(42)
366
+ # end
367
+ # end
368
+ #
369
+ # answer = UltimateAnswer.new('The Ultimate Question of Life')
370
+ # Lotus::Utils::Kernel.BigDecimal(answer)
371
+ # # => #<BigDecimal:7fabfd148588,'0.42E2',9(27)>
372
+ #
373
+ # @example Unchecked exceptions
374
+ # require 'lotus/utils/kernel'
375
+ #
376
+ # # When nil
377
+ # input = nil
378
+ # Lotus::Utils::Kernel.BigDecimal(nil) # => TypeError
379
+ #
380
+ # # When true
381
+ # input = true
382
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
383
+ #
384
+ # # When false
385
+ # input = false
386
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
387
+ #
388
+ # # When Date
389
+ # input = Date.today
390
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
391
+ #
392
+ # # When DateTime
393
+ # input = DateTime.now
394
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
395
+ #
396
+ # # When Time
397
+ # input = Time.now
398
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
399
+ #
400
+ # # Missing #respond_to?
401
+ # input = BasicObject.new
402
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
403
+ def self.BigDecimal(arg)
404
+ case arg
405
+ when ->(a) { a.respond_to?(:to_d) } then arg.to_d
406
+ when Float, Complex, Rational
407
+ BigDecimal(arg.to_s)
408
+ else
409
+ BigDecimal.new(arg)
410
+ end
411
+ rescue NoMethodError
412
+ raise TypeError.new "can't convert into BigDecimal"
413
+ end
414
+
415
+ # Coerces the argument to be a Float.
323
416
  #
324
417
  # It's similar to Ruby's Kernel.Float, but it doesn't stop at the first
325
418
  # error and raise an exception only when the argument can't be coerced.
@@ -332,7 +425,7 @@ module Lotus
332
425
  #
333
426
  # @since 0.1.1
334
427
  #
335
- # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Float
428
+ # @see http://www.ruby-doc.org/core/Kernel.html#method-i-Float
336
429
  #
337
430
  # @example Basic Usage
338
431
  # require 'bigdecimal'
@@ -443,7 +536,7 @@ module Lotus
443
536
  raise TypeError.new "can't convert into Float"
444
537
  end
445
538
 
446
- # Coerces the argument to be a string.
539
+ # Coerces the argument to be a String.
447
540
  #
448
541
  # Identical behavior of Ruby's Kernel.Array, still here because we want
449
542
  # to keep the interface consistent
@@ -456,7 +549,7 @@ module Lotus
456
549
  #
457
550
  # @since 0.1.1
458
551
  #
459
- # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-String
552
+ # @see http://www.ruby-doc.org/core/Kernel.html#method-i-String
460
553
  #
461
554
  # @example Basic Usage
462
555
  # require 'date'
@@ -529,9 +622,12 @@ module Lotus
529
622
  #
530
623
  # # Missing #to_s or #to_str
531
624
  # input = BaseObject.new
532
- # Lotus::Utils::Kernel.Integer(input) # => TypeError
625
+ # Lotus::Utils::Kernel.String(input) # => TypeError
533
626
  def self.String(arg)
627
+ arg = arg.to_str if arg.respond_to?(:to_str)
534
628
  super(arg)
629
+ rescue NoMethodError
630
+ raise TypeError.new "can't convert into String"
535
631
  end
536
632
 
537
633
  # Coerces the argument to be a Date.
@@ -731,7 +827,7 @@ module Lotus
731
827
  raise TypeError.new "can't convert into Time"
732
828
  end
733
829
 
734
- # Coerces the argument to be a boolean.
830
+ # Coerces the argument to be a Boolean.
735
831
  #
736
832
  # @param arg [Object] the argument
737
833
  #
@@ -15,7 +15,7 @@ module Lotus
15
15
  #
16
16
  # @since 0.2.0
17
17
  #
18
- # @see http://ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
18
+ # @see http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/Pathname.html
19
19
  # @see Lotus::Utils::Kernel.Pathname
20
20
  def initialize(*paths)
21
21
  @paths = Array(paths)
@@ -27,8 +27,8 @@ module Lotus
27
27
  # @api private
28
28
  # @since 0.2.0
29
29
  #
30
- # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-clone
31
- # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-dup
30
+ # @see http://ruby-doc.org/core/Object.html#method-i-clone
31
+ # @see http://ruby-doc.org/core/Object.html#method-i-dup
32
32
  #
33
33
  # @example
34
34
  # require 'lotus/utils/load_paths'
@@ -52,16 +52,17 @@ module Lotus
52
52
  # It skips duplications and raises an error in case one of the paths
53
53
  # doesn't exist.
54
54
  #
55
- # @param blk [Proc] the block of code to be yielded
55
+ # @yield [pathname] the block of code that acts on the collection
56
+ # @yieldparam pathname [Pathname]
56
57
  #
57
58
  # @return [void]
58
59
  #
59
60
  # @raise [Errno::ENOENT] if one of the paths doesn't exist
60
61
  #
61
62
  # @since 0.2.0
62
- def each(&blk)
63
+ def each
63
64
  Utils::Kernel.Array(@paths).each do |path|
64
- blk.call realpath(path)
65
+ yield realpath(path)
65
66
  end
66
67
  end
67
68
 
@@ -78,7 +79,7 @@ module Lotus
78
79
  #
79
80
  # @since 0.2.0
80
81
  #
81
- # @see http://ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
82
+ # @see http://ruby-doc.org/stdlib/libdoc/pathname/rdoc/Pathname.html
82
83
  # @see Lotus::Utils::Kernel.Pathname
83
84
  # @see Lotus::Utils::LoadPaths#freeze
84
85
  #
@@ -119,7 +120,7 @@ module Lotus
119
120
  #
120
121
  # @since 0.2.0
121
122
  #
122
- # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-freeze
123
+ # @see http://ruby-doc.org/core/Object.html#method-i-freeze
123
124
  #
124
125
  # @example
125
126
  # require 'lotus/utils/load_paths'
@@ -3,7 +3,7 @@ module Lotus
3
3
  # Prefixed string
4
4
  #
5
5
  # @since 0.1.0
6
- class PathPrefix < ::String
6
+ class PathPrefix
7
7
  # Initialize the path prefix
8
8
  #
9
9
  # @param string [::String] the prefix value
@@ -14,11 +14,11 @@ module Lotus
14
14
  # @since 0.1.0
15
15
  def initialize(string = nil, separator = '/')
16
16
  @separator = separator
17
- super(string.to_s)
17
+ @string = string.to_s
18
18
  end
19
19
 
20
20
  # Joins self with the given token.
21
- # It cleanups the all the `separator` repetitions.
21
+ # It cleans up all the `separator` repetitions.
22
22
  #
23
23
  # @param string [::String] the token we want to join
24
24
  #
@@ -41,13 +41,16 @@ module Lotus
41
41
  end
42
42
 
43
43
  # Joins self with the given token, without prefixing it with `separator`.
44
- # It cleanups the all the `separator` repetitions.
44
+ # It cleans up all the `separator` repetitions.
45
45
  #
46
46
  # @param string [::String] the token we want to join
47
47
  # @param separator [::String] the separator used between tokens
48
48
  #
49
49
  # @return [::String] the joined string
50
50
  #
51
+ # @raise [TypeError] if one of the argument can't be treated as a
52
+ # string
53
+ #
51
54
  # @since 0.1.0
52
55
  #
53
56
  # @example
@@ -57,16 +60,47 @@ module Lotus
57
60
  # path_prefix.relative_join 'new' # => 'posts/new'
58
61
  # path_prefix.relative_join 'new', '_' # => 'posts_new'
59
62
  def relative_join(string, separator = @separator)
60
- separator = separator || @separator
61
- relativize [self, string].join(separator), separator
63
+ raise TypeError if separator.nil?
64
+ relativize [@string, string].join(separator), separator
65
+ end
66
+
67
+ # Returns the hash of the internal string
68
+ #
69
+ # @return [Fixnum]
70
+ #
71
+ # @since 0.3.0
72
+ def hash
73
+ @string.hash
62
74
  end
63
75
 
76
+ # Returns a string representation
77
+ #
78
+ # @return [String]
79
+ #
80
+ # @since 0.3.0
81
+ def to_s
82
+ @string
83
+ end
84
+
85
+ alias_method :to_str, :to_s
86
+
87
+ # Equality
88
+ #
89
+ # @return [TrueClass,FalseClass]
90
+ #
91
+ # @since 0.3.0
92
+ def ==(other)
93
+ to_s == other
94
+ end
95
+
96
+ alias_method :eql?, :==
97
+
64
98
  private
65
99
  attr_reader :separator
66
100
 
67
101
  def absolutize(string)
68
102
  string.tap do |s|
69
- s.insert(0, separator) unless absolute?(s)
103
+ s.prepend(separator) unless absolute?(s)
70
104
  end
71
105
  end
72
106
 
@@ -3,16 +3,43 @@ module Lotus
3
3
  # String on steroids
4
4
  #
5
5
  # @since 0.1.0
6
- class String < ::String
6
+ class String
7
7
  # Separator between Ruby namespaces
8
8
  #
9
- # @example
10
- # Lotus::Utils
11
- #
12
9
  # @since 0.1.0
13
10
  # @api private
14
11
  NAMESPACE_SEPARATOR = '::'.freeze
15
12
 
13
+ # Separator for #classify
14
+ #
15
+ # @since 0.3.0
16
+ # @api private
17
+ CLASSIFY_SEPARATOR = '_'.freeze
18
+
19
+ # Regexp for #tokenize
20
+ #
21
+ # @since 0.3.0
22
+ # @api private
23
+ TOKENIZE_REGEXP = /\((.*)\)/
24
+
25
+ # Separator for #tokenize
26
+ #
27
+ # @since 0.3.0
28
+ # @api private
29
+ TOKENIZE_SEPARATOR = '|'.freeze
30
+
31
+ # Separator for #underscore
32
+ #
33
+ # @since 0.3.0
34
+ # @api private
35
+ UNDERSCORE_SEPARATOR = "/".freeze
36
+
37
+ # gsub second parameter used in #underscore
38
+ #
39
+ # @since 0.3.0
40
+ # @api private
41
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'.freeze
42
+
16
43
  # Initialize the string
17
44
  #
18
45
  # @param string [::String, Symbol] the value we want to initialize
@@ -21,7 +48,7 @@ module Lotus
21
48
  #
22
49
  # @since 0.1.0
23
50
  def initialize(string)
24
- super(string.to_s)
51
+ @string = string.to_s
25
52
  end
26
53
 
27
54
  # Return a CamelCase version of the string
@@ -36,12 +63,12 @@ module Lotus
36
63
  # string = Lotus::Utils::String.new 'lotus_utils'
37
64
  # string.classify # => 'LotusUtils'
38
65
  def classify
39
- split('_').map {|token| token.slice(0).upcase + token.slice(1..-1) }.join
66
+ self.class.new split(CLASSIFY_SEPARATOR).map(&:capitalize).join
40
67
  end
41
68
 
42
69
  # Return a downcased and underscore separated version of the string
43
70
  #
44
- # Revised version of `ActiveSupport::Inflector.underscore` implementation
71
+ # Revised version of `ActiveSupport::Inflector.underscore` implementation
45
72
  # @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
46
73
  #
47
74
  # @return [String] the transformed string
@@ -54,10 +81,11 @@ module Lotus
54
81
  # string = Lotus::Utils::String.new 'LotusUtils'
55
82
  # string.underscore # => 'lotus_utils'
56
83
  def underscore
57
- gsub(NAMESPACE_SEPARATOR, '/').
58
- gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').
59
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
60
- downcase
84
+ new_string = gsub(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
85
+ new_string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
86
+ new_string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
87
+ new_string.downcase!
88
+ self.class.new new_string
61
89
  end
62
90
 
63
91
  # Return the string without the Ruby namespace of the class
@@ -75,7 +103,7 @@ module Lotus
75
103
  # string = Lotus::Utils::String.new 'String'
76
104
  # string.demodulize # => 'String'
77
105
  def demodulize
78
- split(NAMESPACE_SEPARATOR).last
106
+ self.class.new split(NAMESPACE_SEPARATOR).last
79
107
  end
80
108
 
81
109
  # Return the top level namespace name
@@ -93,13 +121,13 @@ module Lotus
93
121
  # string = Lotus::Utils::String.new 'String'
94
122
  # string.namespace # => 'String'
95
123
  def namespace
96
- split(NAMESPACE_SEPARATOR).first
124
+ self.class.new split(NAMESPACE_SEPARATOR).first
97
125
  end
98
126
 
99
127
  # It iterates thru the tokens and calls the given block.
100
128
  # A token is a substring wrapped by `()` and separated by `|`.
101
129
  #
102
- # @param blk [Proc, #call] the block that is called for each token.
130
+ # @yield the block that is called for each token.
103
131
  #
104
132
  # @return [void]
105
133
  #
@@ -116,16 +144,92 @@ module Lotus
116
144
  # # =>
117
145
  # 'Lotus::Utils'
118
146
  # 'Lotus::App'
119
- def tokenize(&blk)
120
- template = gsub(/\((.*)\)/, "%{token}")
121
- tokens = Array(( $1 || self ).split('|'))
122
-
123
- tokens.each do |token|
124
- blk.call(template % {token: token})
147
+ def tokenize
148
+ if match = TOKENIZE_REGEXP.match(@string)
149
+ pre, post = match.pre_match, match.post_match
150
+ tokens = match[1].split(TOKENIZE_SEPARATOR)
151
+ tokens.each do |token|
152
+ yield(self.class.new("#{pre}#{token}#{post}"))
153
+ end
154
+ else
155
+ yield(self.class.new(@string))
125
156
  end
126
157
 
127
158
  nil
128
159
  end
160
+
161
+ # Returns the hash of the internal string
162
+ #
163
+ # @return [Fixnum]
164
+ #
165
+ # @since 0.3.0
166
+ def hash
167
+ @string.hash
168
+ end
169
+
170
+ # Returns a string representation
171
+ #
172
+ # @return [String]
173
+ #
174
+ # @since 0.3.0
175
+ def to_s
176
+ @string
177
+ end
178
+
179
+ alias_method :to_str, :to_s
180
+
181
+ # Equality
182
+ #
183
+ # @return [TrueClass,FalseClass]
184
+ #
185
+ # @since 0.3.0
186
+ def ==(other)
187
+ to_s == other
188
+ end
189
+
190
+ alias_method :eql?, :==
191
+
192
+ # Split the string with the given pattern
193
+ #
194
+ # @return [Array<String>]
195
+ #
196
+ # @see http://www.ruby-doc.org/core/String.html#method-i-split
197
+ #
198
+ # @since 0.3.0
199
+ def split(pattern, limit = 0)
200
+ @string.split(pattern, limit)
201
+ end
202
+
203
+ # Replace the given pattern with the given replacement
204
+ #
205
+ # @return [String,nil]
206
+ #
207
+ # @see http://www.ruby-doc.org/core/String.html#method-i-gsub
208
+ #
209
+ # @since 0.3.0
210
+ def gsub(pattern, replacement, &blk)
211
+ @string.gsub(pattern, replacement, &blk)
212
+ end
213
+
214
+ # Override Ruby's method_missing in order to provide ::String interface
215
+ #
216
+ # @api private
217
+ # @since 0.3.0
218
+ def method_missing(m, *args, &blk)
219
+ s = @string.__send__(m, *args, &blk)
220
+ s = self.class.new(s) if s.is_a?(::String)
221
+ s
222
+ rescue NoMethodError
223
+ raise NoMethodError.new(%(undefined method `#{ m }' for "#{ @string }":#{ self.class }))
224
+ end
225
+
226
+ # Override Ruby's respond_to_missing? in order to support ::String interface
227
+ #
228
+ # @api private
229
+ # @since 0.3.0
230
+ def respond_to_missing?(m, include_private=false)
231
+ @string.respond_to?(m, include_private)
232
+ end
129
233
  end
130
234
  end
131
235
  end
@@ -1,5 +1,5 @@
1
1
  module Lotus
2
2
  module Utils
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'.freeze
4
4
  end
5
5
  end
data/lotus-utils.gemspec CHANGED
@@ -17,7 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
+ spec.required_ruby_version = '>= 2.0.0'
20
21
 
21
- spec.add_development_dependency 'bundler', '~> 1.6'
22
- spec.add_development_dependency 'rake', '~> 10'
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake', '~> 10'
24
+ spec.add_development_dependency 'minitest', '~> 5.4'
23
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.4'
41
55
  description: Lotus utilities
42
56
  email:
43
57
  - me@lucaguidi.com
@@ -72,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
74
88
  - !ruby/object:Gem::Version
75
- version: '0'
89
+ version: 2.0.0
76
90
  required_rubygems_version: !ruby/object:Gem::Requirement
77
91
  requirements:
78
92
  - - ">="