active_object 5.1.1 → 5.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +60 -0
- data/lib/active_object/array.rb +36 -0
- data/lib/active_object/hash.rb +18 -0
- data/lib/active_object/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47a0aa2dc4a4b25366edbc710fdd8637d962f36c7a1b4ea884493010265d238f
|
4
|
+
data.tar.gz: 37619cb45f4d37824df1c08e390119b58fb4a5f24ee6f25d0e4db44c711c82f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3967a1be9e5d446d75eab76dd6038a182b5a0f4599e38cd54de5a464118e5585f4f60da1a15a9676f9a713e08348ad242815781cf01af483cb381dbaa7d23139
|
7
|
+
data.tar.gz: a6a91d091f337d3d982fa7badfc8e8347ce57aeff6fa8d431cd36c791127db46e6de3f68bd0aaaea02c7a66272b5faec93b701a33aed9a1c8bf00f19fb1071a4
|
data/README.md
CHANGED
@@ -115,6 +115,14 @@ end
|
|
115
115
|
[1, 2, 3, 4].delete_values(1, 3) #=> [2, 4]
|
116
116
|
```
|
117
117
|
|
118
|
+
**Demote:**
|
119
|
+
`demote` and `demote!` moves given value to tail of array.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
[1, 2, 2, 3].demote(2) #=> [1, 3, 2, 2]
|
123
|
+
[1, 2, 2, 3].demote!(4) #=> [1, 2, 2, 3]
|
124
|
+
```
|
125
|
+
|
118
126
|
**Denillify:**
|
119
127
|
`denillify` and `denillify!` converts nils into a given value.
|
120
128
|
|
@@ -179,6 +187,13 @@ end
|
|
179
187
|
%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false) #=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10']]
|
180
188
|
```
|
181
189
|
|
190
|
+
**Indexes:**
|
191
|
+
`indexes` returns all the indexes of the matching value.
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
[:a, :b, :a, :c].indexes(:a) #=> [0, 2]
|
195
|
+
```
|
196
|
+
|
182
197
|
**Merge:**
|
183
198
|
`merge` concats multiple arrays.
|
184
199
|
|
@@ -201,6 +216,28 @@ end
|
|
201
216
|
[:a, :b, :c, :c].probability #=> { a: 0.25, b: 0.25, c: 0.5 }
|
202
217
|
```
|
203
218
|
|
219
|
+
**Promote:**
|
220
|
+
`promote` and `promote!` moves given value to head of array.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
[1, 2, 2, 3].promote(2) #=> [2, 2, 1, 3]
|
224
|
+
[1, 2, 2, 3].promote!(4) #=> [1, 2, 2, 3]
|
225
|
+
```
|
226
|
+
|
227
|
+
**Position:**
|
228
|
+
`position` returns the position of the first matching value.
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
[:a, :b, :a, :c].position(:a) #=> 1
|
232
|
+
```
|
233
|
+
|
234
|
+
**Positions:**
|
235
|
+
`positions` returns all the positions of the matching value.
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
[:a, :b, :a, :c].positions(:a) #=> [1, 3]
|
239
|
+
```
|
240
|
+
|
204
241
|
**Reject Values:**
|
205
242
|
`reject_values` delete multiple values from array from a array copy.
|
206
243
|
|
@@ -208,6 +245,13 @@ end
|
|
208
245
|
[1, 2, 3, 4, 5].reject_values(2,4) #=> [1, 3, 5]
|
209
246
|
```
|
210
247
|
|
248
|
+
**Rposition:**
|
249
|
+
`rposition` returns the position of the last matching value.
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
[:a, :b, :a, :c].rposition(:a) #=> 3
|
253
|
+
```
|
254
|
+
|
211
255
|
**Sample:**
|
212
256
|
`sample!` deletes a random value and returns that value.
|
213
257
|
|
@@ -559,6 +603,14 @@ h2 = { a: false, b: { x: [3, 4, 5] } }
|
|
559
603
|
h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
560
604
|
```
|
561
605
|
|
606
|
+
**Demote:**
|
607
|
+
`demote` and `demote!` moves a key value pair to the tail of the hash.
|
608
|
+
|
609
|
+
```ruby
|
610
|
+
{ a: 0, b: 1, c: 2 }.demote(:b) #=> { a: 0, c: 2, b: 1 }
|
611
|
+
{ a: 0, b: 1, c: 2 }.demote!(:d) #=> { a: 0, b: 1, c: 2 }
|
612
|
+
```
|
613
|
+
|
562
614
|
**Denillify:**
|
563
615
|
`denillify` and `denillify!` converts nils into a given value.
|
564
616
|
|
@@ -609,6 +661,14 @@ h1.dig(:a, :b, :c) #=> :d
|
|
609
661
|
{ :foo => 'foo', :baz => 'baz', :bar => 'bar' }.only(:baz, :bar) #=> { :baz => 'baz', :bar => 'bar' }
|
610
662
|
```
|
611
663
|
|
664
|
+
**Promote:**
|
665
|
+
`promote` and `promote!` moves a key value pair to the head of the hash.
|
666
|
+
|
667
|
+
```ruby
|
668
|
+
{ a: 0, b: 1, c: 2 }.promote(:b) #=> { b: 1, a: 0, c: 2 }
|
669
|
+
{ a: 0, b: 1, c: 2 }.promote!(:d) #=> { a: 0, b: 1, c: 2 }
|
670
|
+
```
|
671
|
+
|
612
672
|
**Rename Keys:**
|
613
673
|
`rename_keys` and `rename_keys!` rename the keys of a hash.
|
614
674
|
|
data/lib/active_object/array.rb
CHANGED
@@ -37,6 +37,14 @@ module ActiveObject
|
|
37
37
|
result
|
38
38
|
end
|
39
39
|
|
40
|
+
def demote(value)
|
41
|
+
sort_by { |val| val == value ? 0 : -1 }
|
42
|
+
end
|
43
|
+
|
44
|
+
def demote!(value)
|
45
|
+
replace(demote(value))
|
46
|
+
end
|
47
|
+
|
40
48
|
def denillify(value = 0)
|
41
49
|
map { |val| val.nil? ? value : val }
|
42
50
|
end
|
@@ -117,6 +125,12 @@ module ActiveObject
|
|
117
125
|
end
|
118
126
|
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
119
127
|
|
128
|
+
def indexes(value)
|
129
|
+
results = []
|
130
|
+
each_with_index { |val, i| results << i if value == val }
|
131
|
+
results
|
132
|
+
end
|
133
|
+
|
120
134
|
def merge(*values)
|
121
135
|
values.each { |val| concat(val) }
|
122
136
|
self
|
@@ -130,6 +144,15 @@ module ActiveObject
|
|
130
144
|
replace(nillify)
|
131
145
|
end
|
132
146
|
|
147
|
+
def position(n)
|
148
|
+
idx = index(n)
|
149
|
+
(idx + 1) unless idx.nil?
|
150
|
+
end
|
151
|
+
|
152
|
+
def positions(value)
|
153
|
+
indexes(value).map { |val| val + 1 }
|
154
|
+
end
|
155
|
+
|
133
156
|
def probability
|
134
157
|
hash = ::Hash.new(0.0)
|
135
158
|
differ = 0.0
|
@@ -143,10 +166,23 @@ module ActiveObject
|
|
143
166
|
hash
|
144
167
|
end
|
145
168
|
|
169
|
+
def promote(value)
|
170
|
+
sort_by { |val| val == value ? -1 : 0 }
|
171
|
+
end
|
172
|
+
|
173
|
+
def promote!(value)
|
174
|
+
replace(promote(value))
|
175
|
+
end
|
176
|
+
|
146
177
|
def reject_values(*args)
|
147
178
|
reject { |val| args.include?(val) }
|
148
179
|
end
|
149
180
|
|
181
|
+
def rposition(n)
|
182
|
+
idx = rindex(n)
|
183
|
+
(idx + 1) unless idx.nil?
|
184
|
+
end
|
185
|
+
|
150
186
|
def sample!
|
151
187
|
delete_at(::Random.rand(length - 1))
|
152
188
|
end
|
data/lib/active_object/hash.rb
CHANGED
@@ -67,6 +67,15 @@ module ActiveObject
|
|
67
67
|
end
|
68
68
|
# rubocop:enable Metrics/MethodLength
|
69
69
|
|
70
|
+
def demote(key)
|
71
|
+
return self unless key?(key)
|
72
|
+
merge(key => delete(key))
|
73
|
+
end
|
74
|
+
|
75
|
+
def demote!(key)
|
76
|
+
replace(demote(key))
|
77
|
+
end
|
78
|
+
|
70
79
|
def denillify(value = 0)
|
71
80
|
each { |key, val| self[key] = val.nil? ? value : val }
|
72
81
|
end
|
@@ -122,6 +131,15 @@ module ActiveObject
|
|
122
131
|
replace(hash)
|
123
132
|
end
|
124
133
|
|
134
|
+
def promote(key)
|
135
|
+
return self unless key?(key)
|
136
|
+
{ key => delete(key) }.merge(self)
|
137
|
+
end
|
138
|
+
|
139
|
+
def promote!(key)
|
140
|
+
replace(promote(key))
|
141
|
+
end
|
142
|
+
|
125
143
|
def rename_keys(*keys)
|
126
144
|
dup.rename_keys!(*keys)
|
127
145
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1.
|
4
|
+
version: 5.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|