lotus-utils 0.3.1 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bdc8a61bce6ac30e4889fc50aa7272f14b8b960
4
- data.tar.gz: 22817bdd0799c56964c5a27c74df73ee268ed073
3
+ metadata.gz: e9735bda19b42820e137d3701b680744b1e31efa
4
+ data.tar.gz: 2f8d6bd945be961bf835f994dcf0f3337805d4d6
5
5
  SHA512:
6
- metadata.gz: d13c9c20d2c91da1c235b9feb742c8bca7baed56de1cc9f7e2f96213ea452b7df77160190e3d1c69a4170fd0829af3377f212e9530756c0212e7025ff1ee6eeb
7
- data.tar.gz: 8913ccc6cc7d62dd996071112054380a9454f8f1b4a8a37451b602c3568c2107b0c735646822398b4a585619a9d73b571e5270b236c885f84b936e4ac6547dd7
6
+ metadata.gz: bd341a626427d29d9e8785fca9eb600a171ddf1e4acc64ed72f8301276a8d2166f8ac4fc4ce41ecfa285af419e3556e88c735cdb7a29e8e53c801d601089df35
7
+ data.tar.gz: 4b895ccb945daeea8eb89f03ae3f77cf8e3a58d6f0b59c717fd9a8737c4239e95f2f823f7c136f01938d5d6da80a037604cfc9a60e1e53b2ee7e20badbc2485c
data/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # Lotus::Utils
2
2
  Ruby core extentions and class utilities for Lotus
3
3
 
4
+ ## v0.3.2 - 2014-12-23
5
+ ### Added
6
+ - [Luca Guidi] Official support for Ruby 2.2
7
+ - [Luca Guidi] Introduced `Utils::Attributes`
8
+ - [Luca Guidi] Added `Utils::Hash#stringify!`
9
+
4
10
  ## v0.3.1 - 2014-11-23
5
11
  ### Added
6
12
  - [Luca Guidi] Allow `Utils::Class.load!` to accept any object that implements `#to_s`
data/lib/lotus/utils.rb CHANGED
@@ -5,11 +5,11 @@ module Lotus
5
5
  #
6
6
  # @since 0.1.0
7
7
  module Utils
8
- # @since x.x.x
8
+ # @since 0.3.1
9
9
  # @api private
10
10
  LOTUS_JRUBY = 'java'.freeze
11
11
 
12
- # @since x.x.x
12
+ # @since 0.3.1
13
13
  # @api private
14
14
  LOTUS_RUBINIUS = 'rbx'.freeze
15
15
 
@@ -17,7 +17,7 @@ module Lotus
17
17
  #
18
18
  # @return [TrueClass,FalseClass] return if the VM is JRuby or not
19
19
  #
20
- # @since x.x.x
20
+ # @since 0.3.1
21
21
  # @api private
22
22
  def self.jruby?
23
23
  RUBY_PLATFORM == LOTUS_JRUBY
@@ -27,7 +27,7 @@ module Lotus
27
27
  #
28
28
  # @return [TrueClass,FalseClass] return if the VM is Rubinius or not
29
29
  #
30
- # @since x.x.x
30
+ # @since 0.3.1
31
31
  # @api private
32
32
  def self.rubinius?
33
33
  RUBY_ENGINE == LOTUS_RUBINIUS
@@ -0,0 +1,106 @@
1
+ require 'lotus/utils/hash'
2
+
3
+ module Lotus
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 [Lotus::Utils::Attributes] self
24
+ #
25
+ # @since 0.3.2
26
+ #
27
+ # @example
28
+ # require 'lotus/utils/attributes'
29
+ #
30
+ # attributes = Lotus::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 'lotus/utils/attributes'
46
+ #
47
+ # attributes = Lotus::Utils::Attributes.new(a: 1, 'b' => 2, 23 => 'foo')
48
+ #
49
+ # attributes.get(:a) # => 1
50
+ # attributes.get('a') # => 1
51
+ #
52
+ # attributes.get(:b) # => 2
53
+ # attributes.get('b') # => 2
54
+ #
55
+ # attributes.get(23) # => "foo"
56
+ # attributes.get('23') # => "foo"
57
+ #
58
+ # attributes.get(:unknown) # => nil
59
+ # attributes.get('unknown') # => nil
60
+ def get(attribute)
61
+ @attributes[attribute.to_s]
62
+ end
63
+
64
+ # Set the given value for the given attribute
65
+ #
66
+ # @param attribute [#to_s] a String or any object that implements #to_s
67
+ # @param value [Object] any value
68
+ #
69
+ # @return [NilClass]
70
+ #
71
+ # @since 0.3.2
72
+ #
73
+ # @example
74
+ # require 'lotus/utils/attributes'
75
+ #
76
+ # attributes = Lotus::Utils::Attributes.new
77
+ #
78
+ # attributes.set(:a, 1)
79
+ # attributes.get(:a) # => 1
80
+ # attributes.get('a') # => 1
81
+ #
82
+ # attributes.set('b', 2)
83
+ # attributes.get(:b) # => 2
84
+ # attributes.get('b') # => 2
85
+ #
86
+ # attributes.set(23, 'foo')
87
+ # attributes.get(23) # => "foo"
88
+ # attributes.get('23') # => "foo"
89
+ def set(attribute, value)
90
+ @attributes[attribute.to_s] = value
91
+ nil
92
+ end
93
+
94
+ # Returns a deep duplicated copy of the attributes as a Hash
95
+ #
96
+ # @return [Lotus::Utils::Hash]
97
+ #
98
+ # @since 0.3.2
99
+ #
100
+ # @see Lotus::Utils::Hash
101
+ def to_h
102
+ @attributes.deep_dup
103
+ end
104
+ end
105
+ end
106
+ end
@@ -57,7 +57,7 @@ module Lotus
57
57
  #
58
58
  # @raise [NameError] if no constant can be found.
59
59
  #
60
- # @since x.x.x
60
+ # @since 0.3.1
61
61
  #
62
62
  # @see Lotus::Utils::String#tokenize
63
63
  #
@@ -4,13 +4,13 @@ module Lotus
4
4
  module Utils
5
5
  # Prints a deprecation warning when initialized
6
6
  #
7
- # @since x.x.x
7
+ # @since 0.3.1
8
8
  class Deprecation
9
9
  # Initialize a deprecation message and prints it to standard error.
10
10
  #
11
11
  # @param message [#to_s] a deprecation message
12
12
  #
13
- # @since x.x.x
13
+ # @since 0.3.1
14
14
  #
15
15
  # @example Direct usage
16
16
  # require 'lotus/utils/deprecation'
@@ -57,11 +57,36 @@ module Lotus
57
57
  self
58
58
  end
59
59
 
60
+ # Convert in-place all the keys to Symbol instances, nested hashes are converted too.
61
+ #
62
+ # @return [Hash] self
63
+ #
64
+ # @since 0.3.2
65
+ #
66
+ # @example
67
+ # require 'lotus/utils/hash'
68
+ #
69
+ # hash = Lotus::Utils::Hash.new a: 23, b: { c: ['x','y','z'] }
70
+ # hash.stringify!
71
+ #
72
+ # hash.keys # => [:a, :b]
73
+ # hash.inspect # => {"a"=>23, "b"=>{"c"=>["x", "y", "z"]}}
74
+ def stringify!
75
+ keys.each do |k|
76
+ v = delete(k)
77
+ v = Hash.new(v).stringify! if v.is_a?(::Hash)
78
+
79
+ self[k.to_s] = v
80
+ end
81
+
82
+ self
83
+ end
84
+
60
85
  # Return a deep copy of the current Lotus::Utils::Hash
61
86
  #
62
87
  # @return [Hash] a deep duplicated self
63
88
  #
64
- # @since x.x.x
89
+ # @since 0.3.1
65
90
  #
66
91
  # @example
67
92
  # require 'lotus/utils/hash'
@@ -252,7 +277,7 @@ module Lotus
252
277
 
253
278
  private
254
279
  # @api private
255
- # @since x.x.x
280
+ # @since 0.3.1
256
281
  def duplicate(value)
257
282
  case value
258
283
  when NilClass, FalseClass, TrueClass, Symbol, Numeric
@@ -9,7 +9,7 @@ module Lotus
9
9
  class PathPrefix < Lotus::Utils::String
10
10
  # Path separator
11
11
  #
12
- # @since x.x.x
12
+ # @since 0.3.1
13
13
  # @api private
14
14
  DEFAULT_SEPARATOR = '/'.freeze
15
15
 
@@ -31,7 +31,7 @@ module Lotus
31
31
  # Joins self with the given token.
32
32
  # It cleans up all the `separator` repetitions.
33
33
  #
34
- # @param string [::String] the token we want to join
34
+ # @param strings [::String] the token(s) we want to join
35
35
  #
36
36
  # @return [Lotus::Utils::PathPrefix] the joined string
37
37
  #
@@ -61,7 +61,7 @@ module Lotus
61
61
  # Joins self with the given token, without prefixing it with `separator`.
62
62
  # It cleans up all the `separator` repetitions.
63
63
  #
64
- # @param string [::String] the token we want to join
64
+ # @param strings [::String] the tokens we want to join
65
65
  # @param separator [::String] the separator used between tokens
66
66
  #
67
67
  # @return [Lotus::Utils::PathPrefix] the joined string
@@ -93,7 +93,7 @@ module Lotus
93
93
  #
94
94
  # @return [self]
95
95
  #
96
- # @since x.x.x
96
+ # @since 0.3.1
97
97
  # @api private
98
98
  #
99
99
  # @see #absolute
@@ -107,7 +107,7 @@ module Lotus
107
107
  #
108
108
  # @return [TrueClass,FalseClass]
109
109
  #
110
- # @since x.x.x
110
+ # @since 0.3.1
111
111
  # @api private
112
112
  #
113
113
  # @example
@@ -123,7 +123,7 @@ module Lotus
123
123
  #
124
124
  # @return [self]
125
125
  #
126
- # @since x.x.x
126
+ # @since 0.3.1
127
127
  # @api private
128
128
  #
129
129
  # @see #relative
@@ -124,7 +124,7 @@ module Lotus
124
124
  self.class.new split(NAMESPACE_SEPARATOR).first
125
125
  end
126
126
 
127
- # It iterates thru the tokens and calls the given block.
127
+ # It iterates through the tokens and calls the given block.
128
128
  # A token is a substring wrapped by `()` and separated by `|`.
129
129
  #
130
130
  # @yield the block that is called for each token.
@@ -1,5 +1,5 @@
1
1
  module Lotus
2
2
  module Utils
3
- VERSION = '0.3.1'.freeze
3
+ VERSION = '0.3.2'.freeze
4
4
  end
5
5
  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.1
4
+ version: 0.3.2
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-23 00:00:00.000000000 Z
11
+ date: 2014-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,6 +63,7 @@ files:
63
63
  - LICENSE.md
64
64
  - README.md
65
65
  - lib/lotus/utils.rb
66
+ - lib/lotus/utils/attributes.rb
66
67
  - lib/lotus/utils/callbacks.rb
67
68
  - lib/lotus/utils/class.rb
68
69
  - lib/lotus/utils/class_attribute.rb