cmis-ruby 0.5.27 → 0.5.28

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: 20241ffbac494304de2f6b3040e8dc5694caa336
4
- data.tar.gz: 1e3c05723c15c72bee5763d251a854e083495a9d
3
+ metadata.gz: c1c897e1a19d696085646c92c0fc1c63500765a7
4
+ data.tar.gz: 627e5e1aaeca5ff09435f4f6f04654cf14433958
5
5
  SHA512:
6
- metadata.gz: 660254a432b7e889a797e8f4acb502e3ccceeb461df8a85e87744279f6fcce14d09aa95ecbf098c08c33891d8e754b176f926cdd4be682692f4fb806f187caa1
7
- data.tar.gz: bbc22a2faa241375b5a1ae3be5579d3a72ec447e63d248c2d5f92e9170be1450e13908f81f70e1a3ffca380f2e17970701b8ffbd495a1cd5fd864f6c7c36b2a2
6
+ metadata.gz: ad4518ef5d0e1150bd7c62ee17dfc333dff16ab0bd0921867334235c664173702f8770eda6d4e2a93cf26bf29ec32e73ee844b8a7891768512604ba8e1cd1227
7
+ data.tar.gz: 30ed1ea6be7428319cc65ffd86e00f6a235a0f6cb0cd0bd678102d2f8b4d242f83ad38d3ccd56a53089fcae93498aa61beab3d6c6fbf17c3bb328138a306fb17
data/lib/cmis.rb CHANGED
@@ -1,4 +1,7 @@
1
- require 'ext/all'
1
+ require 'ext/hash/except'
2
+ require 'ext/hash/keys'
3
+ require 'ext/hash/slice'
4
+ require 'ext/string/as_ruby_property'
2
5
 
3
6
  require 'cmis/exceptions'
4
7
  require 'cmis/helpers'
data/lib/cmis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CMIS
2
- VERSION = '0.5.27'
2
+ VERSION = '0.5.28'
3
3
  end
@@ -0,0 +1,22 @@
1
+ class Hash
2
+ # Returns a hash that includes everything except given keys.
3
+ # hash = { a: true, b: false, c: nil }
4
+ # hash.except(:c) # => { a: true, b: false }
5
+ # hash.except(:a, :b) # => { c: nil }
6
+ # hash # => { a: true, b: false, c: nil }
7
+ #
8
+ # This is useful for limiting a set of parameters to everything but a few known toggles:
9
+ # @person.update(params[:person].except(:admin))
10
+ def except(*keys)
11
+ dup.except!(*keys)
12
+ end
13
+
14
+ # Removes the given keys from hash and returns it.
15
+ # hash = { a: true, b: false, c: nil }
16
+ # hash.except!(:c) # => { a: true, b: false }
17
+ # hash # => { a: true, b: false }
18
+ def except!(*keys)
19
+ keys.each { |key| delete(key) }
20
+ self
21
+ end
22
+ end
data/lib/ext/hash/keys.rb CHANGED
@@ -1,21 +1,27 @@
1
1
  class Hash
2
- # Returns a new hash with all keys converted using the block operation.
2
+ # Returns a new hash with all keys converted using the +block+ operation.
3
3
  #
4
4
  # hash = { name: 'Rob', age: '28' }
5
5
  #
6
- # hash.transform_keys{ |key| key.to_s.upcase }
7
- # # => {"NAME"=>"Rob", "AGE"=>"28"}
6
+ # hash.transform_keys { |key| key.to_s.upcase } # => {"NAME"=>"Rob", "AGE"=>"28"}
7
+ #
8
+ # If you do not provide a +block+, it will return an Enumerator
9
+ # for chaining with other methods:
10
+ #
11
+ # hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
8
12
  def transform_keys
9
- result = {}
13
+ return enum_for(:transform_keys) { size } unless block_given?
14
+ result = self.class.new
10
15
  each_key do |key|
11
16
  result[yield(key)] = self[key]
12
17
  end
13
18
  result
14
19
  end
15
20
 
16
- # Destructively convert all keys using the block operations.
17
- # Same as transform_keys but modifies +self+.
21
+ # Destructively converts all keys using the +block+ operations.
22
+ # Same as +transform_keys+ but modifies +self+.
18
23
  def transform_keys!
24
+ return enum_for(:transform_keys!) { size } unless block_given?
19
25
  keys.each do |key|
20
26
  self[yield(key)] = delete(key)
21
27
  end
@@ -27,15 +33,15 @@ class Hash
27
33
  # hash = { name: 'Rob', age: '28' }
28
34
  #
29
35
  # hash.stringify_keys
30
- # # => { "name" => "Rob", "age" => "28" }
36
+ # # => {"name"=>"Rob", "age"=>"28"}
31
37
  def stringify_keys
32
- transform_keys{ |key| key.to_s }
38
+ transform_keys(&:to_s)
33
39
  end
34
40
 
35
- # Destructively convert all keys to strings. Same as
41
+ # Destructively converts all keys to strings. Same as
36
42
  # +stringify_keys+, but modifies +self+.
37
43
  def stringify_keys!
38
- transform_keys!{ |key| key.to_s }
44
+ transform_keys!(&:to_s)
39
45
  end
40
46
 
41
47
  # Returns a new hash with all keys converted to symbols, as long as
@@ -44,22 +50,24 @@ class Hash
44
50
  # hash = { 'name' => 'Rob', 'age' => '28' }
45
51
  #
46
52
  # hash.symbolize_keys
47
- # # => { name: "Rob", age: "28" }
53
+ # # => {:name=>"Rob", :age=>"28"}
48
54
  def symbolize_keys
49
55
  transform_keys{ |key| key.to_sym rescue key }
50
56
  end
51
57
  alias_method :to_options, :symbolize_keys
52
58
 
53
- # Destructively convert all keys to symbols, as long as they respond
59
+ # Destructively converts all keys to symbols, as long as they respond
54
60
  # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
55
61
  def symbolize_keys!
56
62
  transform_keys!{ |key| key.to_sym rescue key }
57
63
  end
58
64
  alias_method :to_options!, :symbolize_keys!
59
65
 
60
- # Validate all keys in a hash match <tt>*valid_keys</tt>, raising ArgumentError
61
- # on a mismatch. Note that keys are NOT treated indifferently, meaning if you
62
- # use strings for keys but assert symbols as keys, this will fail.
66
+ # Validates all keys in a hash match <tt>*valid_keys</tt>, raising
67
+ # +ArgumentError+ on a mismatch.
68
+ #
69
+ # Note that keys are treated differently than HashWithIndifferentAccess,
70
+ # meaning that string and symbol keys will not match.
63
71
  #
64
72
  # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
65
73
  # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
@@ -75,53 +83,45 @@ class Hash
75
83
 
76
84
  # Returns a new hash with all keys converted by the block operation.
77
85
  # This includes the keys from the root hash and from all
78
- # nested hashes.
86
+ # nested hashes and arrays.
79
87
  #
80
88
  # hash = { person: { name: 'Rob', age: '28' } }
81
89
  #
82
90
  # hash.deep_transform_keys{ |key| key.to_s.upcase }
83
91
  # # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
84
92
  def deep_transform_keys(&block)
85
- result = {}
86
- each do |key, value|
87
- result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
88
- end
89
- result
93
+ _deep_transform_keys_in_object(self, &block)
90
94
  end
91
95
 
92
- # Destructively convert all keys by using the block operation.
96
+ # Destructively converts all keys by using the block operation.
93
97
  # This includes the keys from the root hash and from all
94
- # nested hashes.
98
+ # nested hashes and arrays.
95
99
  def deep_transform_keys!(&block)
96
- keys.each do |key|
97
- value = delete(key)
98
- self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value
99
- end
100
- self
100
+ _deep_transform_keys_in_object!(self, &block)
101
101
  end
102
102
 
103
103
  # Returns a new hash with all keys converted to strings.
104
104
  # This includes the keys from the root hash and from all
105
- # nested hashes.
105
+ # nested hashes and arrays.
106
106
  #
107
107
  # hash = { person: { name: 'Rob', age: '28' } }
108
108
  #
109
109
  # hash.deep_stringify_keys
110
110
  # # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
111
111
  def deep_stringify_keys
112
- deep_transform_keys{ |key| key.to_s }
112
+ deep_transform_keys(&:to_s)
113
113
  end
114
114
 
115
- # Destructively convert all keys to strings.
115
+ # Destructively converts all keys to strings.
116
116
  # This includes the keys from the root hash and from all
117
- # nested hashes.
117
+ # nested hashes and arrays.
118
118
  def deep_stringify_keys!
119
- deep_transform_keys!{ |key| key.to_s }
119
+ deep_transform_keys!(&:to_s)
120
120
  end
121
121
 
122
122
  # Returns a new hash with all keys converted to symbols, as long as
123
123
  # they respond to +to_sym+. This includes the keys from the root hash
124
- # and from all nested hashes.
124
+ # and from all nested hashes and arrays.
125
125
  #
126
126
  # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
127
127
  #
@@ -131,10 +131,40 @@ class Hash
131
131
  deep_transform_keys{ |key| key.to_sym rescue key }
132
132
  end
133
133
 
134
- # Destructively convert all keys to symbols, as long as they respond
134
+ # Destructively converts all keys to symbols, as long as they respond
135
135
  # to +to_sym+. This includes the keys from the root hash and from all
136
- # nested hashes.
136
+ # nested hashes and arrays.
137
137
  def deep_symbolize_keys!
138
138
  deep_transform_keys!{ |key| key.to_sym rescue key }
139
139
  end
140
+
141
+ private
142
+ # support methods for deep transforming nested hashes and arrays
143
+ def _deep_transform_keys_in_object(object, &block)
144
+ case object
145
+ when Hash
146
+ object.each_with_object({}) do |(key, value), result|
147
+ result[yield(key)] = _deep_transform_keys_in_object(value, &block)
148
+ end
149
+ when Array
150
+ object.map {|e| _deep_transform_keys_in_object(e, &block) }
151
+ else
152
+ object
153
+ end
154
+ end
155
+
156
+ def _deep_transform_keys_in_object!(object, &block)
157
+ case object
158
+ when Hash
159
+ object.keys.each do |key|
160
+ value = object.delete(key)
161
+ object[yield(key)] = _deep_transform_keys_in_object!(value, &block)
162
+ end
163
+ object
164
+ when Array
165
+ object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
166
+ else
167
+ object
168
+ end
169
+ end
140
170
  end
@@ -1,6 +1,12 @@
1
1
  class Hash
2
- # Slice a hash to include only the given keys. This is useful for
3
- # limiting an options hash to valid keys before passing to a method:
2
+ # Slices a hash to include only the given keys. Returns a hash containing
3
+ # the given keys.
4
+ #
5
+ # { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
6
+ # # => {:a=>1, :b=>2}
7
+ #
8
+ # This is useful for limiting an options hash to valid keys before
9
+ # passing to a method:
4
10
  #
5
11
  # def search(criteria = {})
6
12
  # criteria.assert_valid_keys(:mass, :velocity, :time)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmis-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.27
4
+ version: 0.5.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenneth Geerts
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-01 00:00:00.000000000 Z
12
+ date: 2016-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -77,7 +77,7 @@ files:
77
77
  - lib/cmis/type.rb
78
78
  - lib/cmis/utils.rb
79
79
  - lib/cmis/version.rb
80
- - lib/ext/all.rb
80
+ - lib/ext/hash/except.rb
81
81
  - lib/ext/hash/keys.rb
82
82
  - lib/ext/hash/slice.rb
83
83
  - lib/ext/string/as_ruby_property.rb
data/lib/ext/all.rb DELETED
@@ -1,4 +0,0 @@
1
- require 'ext/hash/keys'
2
- require 'ext/hash/slice'
3
-
4
- require 'ext/string/as_ruby_property'