lotus-utils 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +97 -123
- data/README.md +1 -1
- data/lib/lotus/utils/class.rb +49 -7
- data/lib/lotus/utils/deprecation.rb +70 -0
- data/lib/lotus/utils/hash.rb +83 -0
- data/lib/lotus/utils/kernel.rb +21 -5
- data/lib/lotus/utils/path_prefix.rb +78 -53
- data/lib/lotus/utils/version.rb +1 -1
- data/lib/lotus/utils.rb +29 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bdc8a61bce6ac30e4889fc50aa7272f14b8b960
|
4
|
+
data.tar.gz: 22817bdd0799c56964c5a27c74df73ee268ed073
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d13c9c20d2c91da1c235b9feb742c8bca7baed56de1cc9f7e2f96213ea452b7df77160190e3d1c69a4170fd0829af3377f212e9530756c0212e7025ff1ee6eeb
|
7
|
+
data.tar.gz: 8913ccc6cc7d62dd996071112054380a9454f8f1b4a8a37451b602c3568c2107b0c735646822398b4a585619a9d73b571e5270b236c885f84b936e4ac6547dd7
|
data/CHANGELOG.md
CHANGED
@@ -1,123 +1,97 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
82c0f54 _2014-04-12_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Boolean
|
99
|
-
|
100
|
-
48ba6fb _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Hash
|
101
|
-
|
102
|
-
c10d561 _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Set
|
103
|
-
|
104
|
-
5582e72 _2014-04-11_ **Karl Freeman** add Github contributing guidelines
|
105
|
-
|
106
|
-
012f266 _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.String
|
107
|
-
|
108
|
-
e1a35e2 _2014-04-08_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Integer
|
109
|
-
|
110
|
-
152c856 _2014-04-08_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Array
|
111
|
-
|
112
|
-
## v0.1.0
|
113
|
-
### Jan 23, 2014
|
114
|
-
|
115
|
-
1212e9d _2014-01-16_ **Luca Guidi** Introduced Lotus::Utils::String#demodulize
|
116
|
-
|
117
|
-
f9badc2 _2014-01-16_ **Luca Guidi** Fix for ClassAttribute: ensure class attributes to be inherited even the subclass is defined into a different namespace
|
118
|
-
|
119
|
-
c19d26b _2014-01-07_ **Luca Guidi** Introducing Lotus::Utils::IO
|
120
|
-
|
121
|
-
0453716 _2013-08-08_ **Luca Guidi** Added Class loading mechanism
|
122
|
-
|
123
|
-
272304b _2013-08-07_ **Luca Guidi** Initial mess
|
1
|
+
# Lotus::Utils
|
2
|
+
Ruby core extentions and class utilities for Lotus
|
3
|
+
|
4
|
+
## v0.3.1 - 2014-11-23
|
5
|
+
### Added
|
6
|
+
- [Luca Guidi] Allow `Utils::Class.load!` to accept any object that implements `#to_s`
|
7
|
+
- [Trung Lê] Allow `Utils::Class.load!` to accept a class
|
8
|
+
- [Luca Guidi] Introduced `Utils::Class.load_from_pattern!`
|
9
|
+
- [Luca Guidi] Introduced `Utils.jruby?` and `Utils.rubinius?`
|
10
|
+
- [Luca Guidi] Introduced `Utils::Deprecation`
|
11
|
+
- [Luca Guidi] Official support for Rubinius 2.3+
|
12
|
+
- [Luca Guidi] Official support for JRuby 1.7+ (with 2.0 mode)
|
13
|
+
- [Janko Marohnić] Implemented `Utils::PathPrefix` relativness and absolutness
|
14
|
+
- [Luca Guidi] Made `Utils::PathPrefix` `#join` and `#relative_join` to return a new instance of that class
|
15
|
+
- [Luca Guidi] Implemented `Utils::Hash#deep_dup`
|
16
|
+
- [Luca Guidi] Made `Utils::PathPrefix#join` to accept multiple argument
|
17
|
+
|
18
|
+
### Fixed
|
19
|
+
- [Luca Guidi] Made `Utils::PathPrefix#join` remove trailing occurrences for `@separator` from the output
|
20
|
+
- [Luca Guidi] Made `Utils::PathPrefix#relative_join` to correctly replace all the instances of `@separator` from the output
|
21
|
+
|
22
|
+
### Deprecated
|
23
|
+
- [Luca Guidi] Deprecated `Utils::Class.load!` with a pattern like `Articles(Controller|::Controller)`, use `Utils::Class.load_from_pattern!` instead
|
24
|
+
|
25
|
+
## v0.3.0 - 2014-10-23
|
26
|
+
### Added
|
27
|
+
- [Celso Fernandes] Add BigDecimal coercion to Lotus::Utils::Kernel
|
28
|
+
- [Luca Guidi] Define `Boolean` constant, if missing
|
29
|
+
- [Luca Guidi] Use composition over inheritance for `Lotus::Utils::PathPrefix`
|
30
|
+
- [Luca Guidi] Use composition over inheritance for `Lotus::Utils::Hash`
|
31
|
+
- [Luca Guidi] Use composition over inheritance for `Lotus::Utils::String`
|
32
|
+
|
33
|
+
### Fixed
|
34
|
+
- [Luca Guidi] Improved error message for `Utils::Class.load!`
|
35
|
+
- [Tom Kadwill] Improved error `NameError` message by passing in the whole constant name to `Utils::Class.load!`
|
36
|
+
- [Luca Guidi] `Utils::Hash#to_h` return instances of `::Hash` in case of nested symbolized data structure
|
37
|
+
- [Luca Guidi] Raise `TypeError` if `nil` is passed to `PathPrefix#relative_join`
|
38
|
+
- [Peter Suschlik] Define `Lotus::Utils::Hash#respond_to_missing?`
|
39
|
+
- [Peter Suschlik] Define `Lotus::Utils::String#responds_to_missing?`
|
40
|
+
- [Luca Guidi] Ensure `Utils::Hash#inspect` output to be the same of `::Hash#inspect`
|
41
|
+
|
42
|
+
## v0.2.0 - 2014-06-23
|
43
|
+
### Added
|
44
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Symbol`
|
45
|
+
- [Luca Guidi] Made `Kernel.Pathname` to raise an error when `nil` is passed as argument
|
46
|
+
- [Luca Guidi] Implemented `Lotus::Utils::LoadPaths#freeze` in order to prevent modification after the object has been frozen
|
47
|
+
- [Luca Guidi] Implemented Lotus::Utils::LoadPaths#push, also aliased as #<<
|
48
|
+
- [Luca Guidi] Use composition over inheritance for `Lotus::Utils::LoadPaths`
|
49
|
+
- [Luca Guidi] Introduced `Lotus::Utils::LoadPaths`
|
50
|
+
- [Luca Guidi] Introduced `Lotus::Utils::String#namespace`, in order to return the top level Ruby namespace for the given string
|
51
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Pathname`
|
52
|
+
|
53
|
+
### Fixed
|
54
|
+
- [Luca Guidi] Implemented `Lotus::Utils::LoadPaths#initialize_copy` in order to safely `#dup` and `#clone`
|
55
|
+
|
56
|
+
### Changed
|
57
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Callbacks::Chain#freeze` in order to prevent modification after the object has been frozen
|
58
|
+
- [Luca Guidi] All the `Utils::Kernel` methods will raise `TypeError` in case of failed coercion.
|
59
|
+
- [Luca Guidi] Made `Kernel.Time` to raise an error when `nil` is passed as argument
|
60
|
+
- [Luca Guidi] Made `Kernel.DateTime` to raise an error when `nil` is passed as argument
|
61
|
+
- [Luca Guidi] Made `Kernel.Date` to raise an error when `nil` is passed as argument
|
62
|
+
- [Luca Guidi] Made `Kernel.Boolean` to return false when `nil` is passed as argument
|
63
|
+
- [Luca Guidi] Made `Kernel.String` to return an empty string when `nil` is passed as argument
|
64
|
+
- [Luca Guidi] Made `Kernel.Float` to return `0.0` when `nil` is passed as argument
|
65
|
+
- [Luca Guidi] Made `Kernel.Integer` to return `0` when `nil` is passed as argument
|
66
|
+
- [Luca Guidi] Made `Kernel.Hash` to return an empty `Hash` when `nil` is passed as argument
|
67
|
+
- [Luca Guidi] Made `Kernel.Set` to return an empty `Set` when `nil` is passed as argument
|
68
|
+
- [Luca Guidi] Made `Kernel.Array` to return an empty `Array` when `nil` is passed as argument
|
69
|
+
- [Luca Guidi] Use composition over inheritance for `Lotus::Utils::Callbacks::Chain`
|
70
|
+
|
71
|
+
## v0.1.1 - 2014-04-23
|
72
|
+
### Added
|
73
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Time`
|
74
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.DateTime`
|
75
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Date`
|
76
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Float`
|
77
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Boolean`
|
78
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Hash`
|
79
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Set`
|
80
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.String`
|
81
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Integer`
|
82
|
+
- [Luca Guidi] Implemented `Lotus::Utils::Kernel.Array`
|
83
|
+
|
84
|
+
### Fixed
|
85
|
+
- [Christopher Keele] Add missing stdlib `Set` require to `Utils::ClassAttribute`
|
86
|
+
|
87
|
+
## v0.1.0 - 2014-01-23
|
88
|
+
### Added
|
89
|
+
- [Luca Guidi] Introduced `Lotus::Utils::String#demodulize`
|
90
|
+
- [Luca Guidi] Introduced `Lotus::Utils::IO.silence_warnings`
|
91
|
+
- [Luca Guidi] Introduced class loading mechanism from a string: `Utils::Class.load!`
|
92
|
+
- [Luca Guidi] Introduced callbacks support for classes
|
93
|
+
- [Luca Guidi] Introduced inheritable class level attributes
|
94
|
+
- [Luca Guidi] Introduced `Utils::Hash`
|
95
|
+
- [Luca Guidi] Introduced `Utils::String`
|
96
|
+
- [Luca Guidi] Introduced `Utils::PathPrefix`
|
97
|
+
- [Luca Guidi] Official support for MRI 2.0+
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ 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+, JRuby 1.7+ (with 2.0 mode) and Rubinius 2.3.0+.
|
26
26
|
|
27
27
|
## Installation
|
28
28
|
|
data/lib/lotus/utils/class.rb
CHANGED
@@ -1,22 +1,64 @@
|
|
1
1
|
require 'lotus/utils/string'
|
2
|
+
require 'lotus/utils/deprecation'
|
2
3
|
|
3
4
|
module Lotus
|
4
5
|
module Utils
|
5
6
|
# Class utilities
|
6
7
|
# @since 0.1.0
|
7
8
|
class Class
|
8
|
-
# Loads a class for the given
|
9
|
-
#
|
10
|
-
# @param name [String] the specific class name or pattern for the class that we want to load
|
9
|
+
# Loads a class for the given name.
|
11
10
|
#
|
11
|
+
# @param name [String, Class] the specific class name
|
12
12
|
# @param namespace [Class, Module] the Ruby namespace where we want to perform the lookup.
|
13
|
-
#
|
14
13
|
# @return [Class, Module] the found Ruby constant.
|
15
14
|
#
|
16
15
|
# @raise [NameError] if no constant can be found.
|
17
16
|
#
|
18
17
|
# @since 0.1.0
|
19
18
|
#
|
19
|
+
# @example
|
20
|
+
# require 'lotus/utils/class'
|
21
|
+
#
|
22
|
+
# module App
|
23
|
+
# module Service
|
24
|
+
# class Endpoint
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# class ServiceEndpoint
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # basic usage
|
33
|
+
# Lotus::Utils::Class.load!('App::Service') # => App::Service
|
34
|
+
# Lotus::Utils::Class.load!(App::Service) # => App::Service
|
35
|
+
#
|
36
|
+
# # with explicit namespace
|
37
|
+
# Lotus::Utils::Class.load!('Service', App) # => App::Service
|
38
|
+
#
|
39
|
+
# # with missing constant
|
40
|
+
# Lotus::Utils::Class.load!('Unknown') # => raises NameError
|
41
|
+
def self.load!(name, namespace = Object)
|
42
|
+
name = name.to_s
|
43
|
+
|
44
|
+
if name.match(/\|/)
|
45
|
+
Utils::Deprecation.new("Using Lotus::Utils::Class.load! with a pattern is deprecated, please use Lotus::Utils::Class.load_from_pattern!: #{ name }, #{ namespace }")
|
46
|
+
return load_from_pattern!(name, namespace)
|
47
|
+
end
|
48
|
+
|
49
|
+
namespace.const_get(name)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Loads a class from the given pattern name and namespace
|
53
|
+
#
|
54
|
+
# @param pattern [String] the class name pattern
|
55
|
+
# @param namespace [Class, Module] the Ruby namespace where we want to perform the lookup.
|
56
|
+
# @return [Class, Module] the found Ruby constant.
|
57
|
+
#
|
58
|
+
# @raise [NameError] if no constant can be found.
|
59
|
+
#
|
60
|
+
# @since x.x.x
|
61
|
+
#
|
20
62
|
# @see Lotus::Utils::String#tokenize
|
21
63
|
#
|
22
64
|
# @example
|
@@ -44,15 +86,15 @@ module Lotus
|
|
44
86
|
#
|
45
87
|
# # with missing constant
|
46
88
|
# Lotus::Utils::Class.load!('Unknown') # => raises NameError
|
47
|
-
def self.
|
48
|
-
String.new(
|
89
|
+
def self.load_from_pattern!(pattern, namespace = Object)
|
90
|
+
String.new(pattern).tokenize do |token|
|
49
91
|
begin
|
50
92
|
return namespace.const_get(token)
|
51
93
|
rescue NameError
|
52
94
|
end
|
53
95
|
end
|
54
96
|
|
55
|
-
full_name = [ (namespace == Object ? nil : namespace),
|
97
|
+
full_name = [ (namespace == Object ? nil : namespace), pattern ].compact.join('::')
|
56
98
|
raise NameError.new("uninitialized constant #{ full_name }")
|
57
99
|
end
|
58
100
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'lotus/utils'
|
2
|
+
|
3
|
+
module Lotus
|
4
|
+
module Utils
|
5
|
+
# Prints a deprecation warning when initialized
|
6
|
+
#
|
7
|
+
# @since x.x.x
|
8
|
+
class Deprecation
|
9
|
+
# Initialize a deprecation message and prints it to standard error.
|
10
|
+
#
|
11
|
+
# @param message [#to_s] a deprecation message
|
12
|
+
#
|
13
|
+
# @since x.x.x
|
14
|
+
#
|
15
|
+
# @example Direct usage
|
16
|
+
# require 'lotus/utils/deprecation'
|
17
|
+
#
|
18
|
+
# class Engine
|
19
|
+
# def old_method
|
20
|
+
# Lotus::Utils::Deprecation.new('old_method is deprecated, please use new_method')
|
21
|
+
# new_method
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def new_method
|
25
|
+
# puts 'started'
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Engine.new.old_method
|
30
|
+
# # => old_method is deprecated, please use new_method - called from: test.rb:14:in `<main>'.
|
31
|
+
# # => started
|
32
|
+
#
|
33
|
+
# @example Indirect usage
|
34
|
+
# require 'lotus/utils/deprecation'
|
35
|
+
#
|
36
|
+
# class Engine
|
37
|
+
# def old_method
|
38
|
+
# Lotus::Utils::Deprecation.new('old_method is deprecated, please use new_method')
|
39
|
+
# new_method
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# def new_method
|
43
|
+
# puts 'started'
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# class Car
|
48
|
+
# def initialize
|
49
|
+
# @engine = Engine.new
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# def start
|
53
|
+
# @engine.old_method
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# Car.new.start
|
58
|
+
# # => old_method is deprecated, please use new_method - called from: test.rb:20:in `start'.
|
59
|
+
# # => started
|
60
|
+
def initialize(message)
|
61
|
+
::Kernel.warn("#{ message } - called from: #{ caller[caller_index] }.")
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def caller_index
|
66
|
+
Utils.jruby? || Utils.rubinius? ? 1 : 2
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/lib/lotus/utils/hash.rb
CHANGED
@@ -57,6 +57,73 @@ module Lotus
|
|
57
57
|
self
|
58
58
|
end
|
59
59
|
|
60
|
+
# Return a deep copy of the current Lotus::Utils::Hash
|
61
|
+
#
|
62
|
+
# @return [Hash] a deep duplicated self
|
63
|
+
#
|
64
|
+
# @since x.x.x
|
65
|
+
#
|
66
|
+
# @example
|
67
|
+
# require 'lotus/utils/hash'
|
68
|
+
#
|
69
|
+
# hash = Lotus::Utils::Hash.new(
|
70
|
+
# 'nil' => nil,
|
71
|
+
# 'false' => false,
|
72
|
+
# 'true' => true,
|
73
|
+
# 'symbol' => :foo,
|
74
|
+
# 'fixnum' => 23,
|
75
|
+
# 'bignum' => 13289301283 ** 2,
|
76
|
+
# 'float' => 1.0,
|
77
|
+
# 'complex' => Complex(0.3),
|
78
|
+
# 'bigdecimal' => BigDecimal.new('12.0001'),
|
79
|
+
# 'rational' => Rational(0.3),
|
80
|
+
# 'string' => 'foo bar',
|
81
|
+
# 'hash' => { a: 1, b: 'two', c: :three },
|
82
|
+
# 'u_hash' => Lotus::Utils::Hash.new({ a: 1, b: 'two', c: :three })
|
83
|
+
# )
|
84
|
+
#
|
85
|
+
# duped = hash.deep_dup
|
86
|
+
#
|
87
|
+
# hash.class # => Lotus::Utils::Hash
|
88
|
+
# duped.class # => Lotus::Utils::Hash
|
89
|
+
#
|
90
|
+
# hash.object_id # => 70147385937100
|
91
|
+
# duped.object_id # => 70147385950620
|
92
|
+
#
|
93
|
+
# # unduplicated values
|
94
|
+
# duped['nil'] # => nil
|
95
|
+
# duped['false'] # => false
|
96
|
+
# duped['true'] # => true
|
97
|
+
# duped['symbol'] # => :foo
|
98
|
+
# duped['fixnum'] # => 23
|
99
|
+
# duped['bignum'] # => 176605528590345446089
|
100
|
+
# duped['float'] # => 1.0
|
101
|
+
# duped['complex'] # => (0.3+0i)
|
102
|
+
# duped['bigdecimal'] # => #<BigDecimal:7f9ffe6e2fd0,'0.120001E2',18(18)>
|
103
|
+
# duped['rational'] # => 5404319552844595/18014398509481984)
|
104
|
+
#
|
105
|
+
# # it duplicates values
|
106
|
+
# duped['string'].reverse!
|
107
|
+
# duped['string'] # => "rab oof"
|
108
|
+
# hash['string'] # => "foo bar"
|
109
|
+
#
|
110
|
+
# # it deeply duplicates Hash, by preserving the class
|
111
|
+
# duped['hash'].class # => Hash
|
112
|
+
# duped['hash'].delete(:a)
|
113
|
+
# hash['hash'][:a] # => 1
|
114
|
+
#
|
115
|
+
# duped['hash'][:b].upcase!
|
116
|
+
# duped['hash'][:b] # => "TWO"
|
117
|
+
# hash['hash'][:b] # => "two"
|
118
|
+
#
|
119
|
+
# # it deeply duplicates Lotus::Utils::Hash, by preserving the class
|
120
|
+
# duped['u_hash'].class # => Lotus::Utils::Hash
|
121
|
+
def deep_dup
|
122
|
+
Hash.new.tap do |result|
|
123
|
+
@hash.each {|k, v| result[k] = duplicate(v) }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
60
127
|
# Returns a new array populated with the keys from this hash
|
61
128
|
#
|
62
129
|
# @return [Array] the keys
|
@@ -182,6 +249,22 @@ module Lotus
|
|
182
249
|
def respond_to_missing?(m, include_private=false)
|
183
250
|
@hash.respond_to?(m, include_private)
|
184
251
|
end
|
252
|
+
|
253
|
+
private
|
254
|
+
# @api private
|
255
|
+
# @since x.x.x
|
256
|
+
def duplicate(value)
|
257
|
+
case value
|
258
|
+
when NilClass, FalseClass, TrueClass, Symbol, Numeric
|
259
|
+
value
|
260
|
+
when Hash
|
261
|
+
value.deep_dup
|
262
|
+
when ::Hash
|
263
|
+
Hash.new(value).deep_dup.to_h
|
264
|
+
else
|
265
|
+
value.dup
|
266
|
+
end
|
267
|
+
end
|
185
268
|
end
|
186
269
|
end
|
187
270
|
end
|
data/lib/lotus/utils/kernel.rb
CHANGED
@@ -3,6 +3,7 @@ require 'date'
|
|
3
3
|
require 'time'
|
4
4
|
require 'pathname'
|
5
5
|
require 'bigdecimal'
|
6
|
+
require 'lotus/utils'
|
6
7
|
|
7
8
|
# Define top level constant Boolean, so it can be easily used by other libraries
|
8
9
|
# in coercions DSLs
|
@@ -623,11 +624,26 @@ module Lotus
|
|
623
624
|
# # Missing #to_s or #to_str
|
624
625
|
# input = BaseObject.new
|
625
626
|
# Lotus::Utils::Kernel.String(input) # => TypeError
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
627
|
+
if Utils.rubinius?
|
628
|
+
def self.String(arg)
|
629
|
+
case arg
|
630
|
+
when ->(a) { a.respond_to?(:to_str) }
|
631
|
+
arg.to_str
|
632
|
+
when ->(a) { a.respond_to?(:to_s) }
|
633
|
+
arg.to_s
|
634
|
+
else
|
635
|
+
super(arg)
|
636
|
+
end
|
637
|
+
rescue NoMethodError
|
638
|
+
raise TypeError.new "can't convert into String"
|
639
|
+
end
|
640
|
+
else
|
641
|
+
def self.String(arg)
|
642
|
+
arg = arg.to_str if arg.respond_to?(:to_str)
|
643
|
+
super(arg)
|
644
|
+
rescue NoMethodError
|
645
|
+
raise TypeError.new "can't convert into String"
|
646
|
+
end
|
631
647
|
end
|
632
648
|
|
633
649
|
# Coerces the argument to be a Date.
|
@@ -1,9 +1,18 @@
|
|
1
|
+
require 'lotus/utils/string'
|
2
|
+
require 'lotus/utils/kernel'
|
3
|
+
|
1
4
|
module Lotus
|
2
5
|
module Utils
|
3
6
|
# Prefixed string
|
4
7
|
#
|
5
8
|
# @since 0.1.0
|
6
|
-
class PathPrefix
|
9
|
+
class PathPrefix < Lotus::Utils::String
|
10
|
+
# Path separator
|
11
|
+
#
|
12
|
+
# @since x.x.x
|
13
|
+
# @api private
|
14
|
+
DEFAULT_SEPARATOR = '/'.freeze
|
15
|
+
|
7
16
|
# Initialize the path prefix
|
8
17
|
#
|
9
18
|
# @param string [::String] the prefix value
|
@@ -12,9 +21,11 @@ module Lotus
|
|
12
21
|
# @return [PathPrefix] self
|
13
22
|
#
|
14
23
|
# @since 0.1.0
|
15
|
-
|
24
|
+
#
|
25
|
+
# @see Lotus::Utils::PathPrefix::DEFAULT_SEPARATOR
|
26
|
+
def initialize(string = nil, separator = DEFAULT_SEPARATOR)
|
27
|
+
super(string)
|
16
28
|
@separator = separator
|
17
|
-
@string = string.to_s
|
18
29
|
end
|
19
30
|
|
20
31
|
# Joins self with the given token.
|
@@ -22,22 +33,29 @@ module Lotus
|
|
22
33
|
#
|
23
34
|
# @param string [::String] the token we want to join
|
24
35
|
#
|
25
|
-
# @return [::
|
36
|
+
# @return [Lotus::Utils::PathPrefix] the joined string
|
26
37
|
#
|
27
38
|
# @since 0.1.0
|
28
39
|
#
|
29
|
-
# @example
|
40
|
+
# @example Single string
|
30
41
|
# require 'lotus/utils/path_prefix'
|
31
42
|
#
|
32
|
-
# path_prefix = Lotus::Utils::PathPrefix.new
|
33
|
-
# path_prefix.join
|
34
|
-
# path_prefix.join
|
43
|
+
# path_prefix = Lotus::Utils::PathPrefix.new('/posts')
|
44
|
+
# path_prefix.join('new').to_s # => "/posts/new"
|
45
|
+
# path_prefix.join('/new').to_s # => "/posts/new"
|
35
46
|
#
|
36
|
-
# path_prefix = Lotus::Utils::PathPrefix.new
|
37
|
-
# path_prefix.join
|
38
|
-
# path_prefix.join
|
39
|
-
|
40
|
-
|
47
|
+
# path_prefix = Lotus::Utils::PathPrefix.new('posts')
|
48
|
+
# path_prefix.join('new').to_s # => "/posts/new"
|
49
|
+
# path_prefix.join('/new').to_s # => "/posts/new"
|
50
|
+
#
|
51
|
+
# @example Multiple strings
|
52
|
+
# require 'lotus/utils/path_prefix'
|
53
|
+
#
|
54
|
+
# path_prefix = Lotus::Utils::PathPrefix.new('myapp')
|
55
|
+
# path_prefix.join('/assets', 'application.js').to_s
|
56
|
+
# # => "/myapp/assets/application.js"
|
57
|
+
def join(*strings)
|
58
|
+
relative_join(strings).absolute!
|
41
59
|
end
|
42
60
|
|
43
61
|
# Joins self with the given token, without prefixing it with `separator`.
|
@@ -46,7 +64,7 @@ module Lotus
|
|
46
64
|
# @param string [::String] the token we want to join
|
47
65
|
# @param separator [::String] the separator used between tokens
|
48
66
|
#
|
49
|
-
# @return [::
|
67
|
+
# @return [Lotus::Utils::PathPrefix] the joined string
|
50
68
|
#
|
51
69
|
# @raise [TypeError] if one of the argument can't be treated as a
|
52
70
|
# string
|
@@ -57,62 +75,69 @@ module Lotus
|
|
57
75
|
# require 'lotus/utils/path_prefix'
|
58
76
|
#
|
59
77
|
# path_prefix = Lotus::Utils::PathPrefix.new 'posts'
|
60
|
-
# path_prefix.relative_join
|
61
|
-
# path_prefix.relative_join
|
62
|
-
def relative_join(
|
78
|
+
# path_prefix.relative_join('new').to_s # => 'posts/new'
|
79
|
+
# path_prefix.relative_join('new', '_').to_s # => 'posts_new'
|
80
|
+
def relative_join(strings, separator = @separator)
|
63
81
|
raise TypeError if separator.nil?
|
64
|
-
|
82
|
+
prefix = @string.gsub(@separator, separator)
|
83
|
+
|
84
|
+
self.class.new(
|
85
|
+
Utils::Kernel.Array([prefix, strings]).join(separator),
|
86
|
+
separator
|
87
|
+
).relative!
|
65
88
|
end
|
66
89
|
|
67
|
-
|
90
|
+
protected
|
91
|
+
|
92
|
+
# Modifies the path prefix to have a prepended separator.
|
93
|
+
#
|
94
|
+
# @return [self]
|
68
95
|
#
|
69
|
-
# @
|
96
|
+
# @since x.x.x
|
97
|
+
# @api private
|
70
98
|
#
|
71
|
-
# @
|
72
|
-
def
|
73
|
-
@string.
|
99
|
+
# @see #absolute
|
100
|
+
def absolute!
|
101
|
+
@string.prepend(separator) unless absolute?
|
102
|
+
|
103
|
+
self
|
74
104
|
end
|
75
105
|
|
76
|
-
# Returns
|
106
|
+
# Returns whether the path prefix starts with its separator.
|
107
|
+
#
|
108
|
+
# @return [TrueClass,FalseClass]
|
77
109
|
#
|
78
|
-
# @
|
110
|
+
# @since x.x.x
|
111
|
+
# @api private
|
79
112
|
#
|
80
|
-
# @
|
81
|
-
|
82
|
-
|
113
|
+
# @example
|
114
|
+
# require 'lotus/utils/path_prefix'
|
115
|
+
#
|
116
|
+
# Lotus::Utils::PathPrefix.new('/posts').absolute? #=> true
|
117
|
+
# Lotus::Utils::PathPrefix.new('posts').absolute? #=> false
|
118
|
+
def absolute?
|
119
|
+
@string.start_with?(separator)
|
83
120
|
end
|
84
121
|
|
85
|
-
|
86
|
-
|
87
|
-
# Equality
|
122
|
+
# Modifies the path prefix to remove the leading separator.
|
88
123
|
#
|
89
|
-
# @return [
|
124
|
+
# @return [self]
|
90
125
|
#
|
91
|
-
# @since
|
92
|
-
|
93
|
-
|
94
|
-
|
126
|
+
# @since x.x.x
|
127
|
+
# @api private
|
128
|
+
#
|
129
|
+
# @see #relative
|
130
|
+
def relative!
|
131
|
+
@string.gsub!(%r{(?<!:)#{ separator * 2 }}, separator)
|
132
|
+
@string.sub!(%r{\A#{ separator }}, '')
|
95
133
|
|
96
|
-
|
134
|
+
self
|
135
|
+
end
|
97
136
|
|
98
137
|
private
|
138
|
+
# @since 0.1.0
|
139
|
+
# @api private
|
99
140
|
attr_reader :separator
|
100
|
-
|
101
|
-
def absolutize(string)
|
102
|
-
string.tap do |s|
|
103
|
-
s.prepend(separator) unless absolute?(s)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def absolute?(string)
|
108
|
-
string.start_with?(separator)
|
109
|
-
end
|
110
|
-
|
111
|
-
def relativize(string, separator = @separator)
|
112
|
-
string.
|
113
|
-
gsub(%r{(?<!:)#{ separator * 2 }}, separator).
|
114
|
-
gsub(%r{\A#{ separator }}, '')
|
115
|
-
end
|
116
141
|
end
|
117
142
|
end
|
118
143
|
end
|
data/lib/lotus/utils/version.rb
CHANGED
data/lib/lotus/utils.rb
CHANGED
@@ -2,6 +2,35 @@ require 'lotus/utils/version'
|
|
2
2
|
|
3
3
|
module Lotus
|
4
4
|
# Ruby core extentions and Lotus utilities
|
5
|
+
#
|
6
|
+
# @since 0.1.0
|
5
7
|
module Utils
|
8
|
+
# @since x.x.x
|
9
|
+
# @api private
|
10
|
+
LOTUS_JRUBY = 'java'.freeze
|
11
|
+
|
12
|
+
# @since x.x.x
|
13
|
+
# @api private
|
14
|
+
LOTUS_RUBINIUS = 'rbx'.freeze
|
15
|
+
|
16
|
+
# Checks if the current VM is JRuby
|
17
|
+
#
|
18
|
+
# @return [TrueClass,FalseClass] return if the VM is JRuby or not
|
19
|
+
#
|
20
|
+
# @since x.x.x
|
21
|
+
# @api private
|
22
|
+
def self.jruby?
|
23
|
+
RUBY_PLATFORM == LOTUS_JRUBY
|
24
|
+
end
|
25
|
+
|
26
|
+
# Checks if the current VM is Rubinius
|
27
|
+
#
|
28
|
+
# @return [TrueClass,FalseClass] return if the VM is Rubinius or not
|
29
|
+
#
|
30
|
+
# @since x.x.x
|
31
|
+
# @api private
|
32
|
+
def self.rubinius?
|
33
|
+
RUBY_ENGINE == LOTUS_RUBINIUS
|
34
|
+
end
|
6
35
|
end
|
7
36
|
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.3.
|
4
|
+
version: 0.3.1
|
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-
|
11
|
+
date: 2014-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- lib/lotus/utils/callbacks.rb
|
67
67
|
- lib/lotus/utils/class.rb
|
68
68
|
- lib/lotus/utils/class_attribute.rb
|
69
|
+
- lib/lotus/utils/deprecation.rb
|
69
70
|
- lib/lotus/utils/hash.rb
|
70
71
|
- lib/lotus/utils/io.rb
|
71
72
|
- lib/lotus/utils/kernel.rb
|