active_object 1.4.0 → 2.0.0

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: b908cec473063ce9371f42ad24f3e2160449288d
4
- data.tar.gz: 372517125c14abffe2e4608b087197b7720ae913
3
+ metadata.gz: 0063e4f4ebe0c179ce2e900b5521e88b210d4263
4
+ data.tar.gz: 795f415b25307d864f9fe0655028375d3f2a03bc
5
5
  SHA512:
6
- metadata.gz: 58811199b70a9c498ebb85894461c9ce61b1ff2b414018a3373459908ff5d061d7cd465d9f0825d7b91a8028d361cb1673b85f307e5f44db749e97e32e77708d
7
- data.tar.gz: a335da3d616ac49e2fb1da3655db8fa436287fd6ad470b9a916c1618b41867b510322f6174cdb2e83f0e32d5f3198add747e95c18c78dbc8ae03714c2e35fe62
6
+ metadata.gz: 471c7234bd99b4bb7d860391a2a04faa796dc10d2d5823d9099dc9dd5ec53a5a20a17024cf237cb18f0208d1bde7a941a9a14c452df436715bcbc0a4054b52c4
7
+ data.tar.gz: 0c80a0a97edb2e6d5e97edd82d0f5fb43727656628f364ea1652b178b2afcdf76eddc2b63bc2f613deb87f99bf548dab0c1d59641f9360a3243180380cc75004
data/README.md CHANGED
@@ -13,6 +13,7 @@ Highly recommended extensions:
13
13
  * **String:** Escape Utils - https://github.com/brianmario/escape_utils
14
14
  * **String:** Fast Blank - https://github.com/SamSaffron/fast_blank
15
15
  * **Translation:** Fast Gettext - https://github.com/grosser/fast_gettext
16
+ * **Facets:** Facets - https://github.com/rubyworks/facets
16
17
 
17
18
  ## Installation
18
19
 
@@ -42,6 +43,24 @@ Or install it yourself as:
42
43
 
43
44
  ## Array
44
45
 
46
+ ####After:####
47
+ `after` returns the value after the given value.
48
+
49
+ ```ruby
50
+ ["1", "2", "3"].after("2") #=> "3"
51
+ ["1", "2", "3"].after("3") #=> "1"
52
+ ["1", "2", "3"].after("4") #=> nil
53
+ ```
54
+
55
+ ####Before:####
56
+ `before` returns the value before the given value.
57
+
58
+ ```ruby
59
+ ["1", "2", "3"].before("2") #=> "1"
60
+ ["1", "2", "3"].before("1") #=> "3"
61
+ ["1", "2", "3"].before("4") #=> nil
62
+ ```
63
+
45
64
  ####Delete First:####
46
65
  `delete_first` and `delete_first!` removes the first element from an array. Like Array.shift, but returns the array instead of the removed element.
47
66
 
@@ -56,6 +75,21 @@ Or install it yourself as:
56
75
  ["1", "2", "3"].delete_last #=> ["1", "2"]
57
76
  ```
58
77
 
78
+ ####Delete Values:####
79
+ `delete_values` delete multiple values from array.
80
+
81
+ ```ruby
82
+ [1, 2, 3, 4].delete_values(1, 3) #=> [2, 4]
83
+ ```
84
+
85
+ ####Duplicates:####
86
+ `duplicates` returns list of duplicate elements.
87
+
88
+ ```ruby
89
+ [1, 1, 2, 2, 2, 3].duplicates #=> [1, 2]
90
+ [1, 1, 2, 2, 2, 3].duplicates(3) #=> [2]
91
+ ```
92
+
59
93
  ####From:####
60
94
  `from` returns the tail of the array from position. `Rails Safe`
61
95
 
@@ -90,6 +124,27 @@ Or install it yourself as:
90
124
  %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"]]
91
125
  ```
92
126
 
127
+ ####Probablity:####
128
+ `probability` generates a hash mapping each unique element in the array to the relative frequency, i.e. the probablity, of it appearence.
129
+
130
+ ```ruby
131
+ [:a, :b, :c, :c].probability #=> { a: 0.25, b: 0.25, c: 0.5 }
132
+ ```
133
+
134
+ ####Reject Values:####
135
+ `reject_values` delete multiple values from array from a array copy.
136
+
137
+ ```ruby
138
+ [1, 2, 3, 4, 5].reject_values(2,4) #=> [1, 3, 5]
139
+ ```
140
+
141
+ ####Sample:####
142
+ `sample!` deletes a random value and returns that value.
143
+
144
+ ```ruby
145
+ [1, 2, 3, 4, 5].sample! #=> 2
146
+ ```
147
+
93
148
  ####Split:####
94
149
  `split` divides the array into one or more subarrays based on a delimiting value or the result of an optional block. `Rails Safe`
95
150
 
@@ -134,6 +189,13 @@ Or install it yourself as:
134
189
 
135
190
  ## Enumerable
136
191
 
192
+ ####Cluster:####
193
+ `cluster` clusters together adjacent elements into a list of sub-arrays.
194
+
195
+ ```ruby
196
+ [2,2,2,3,3,4,2,2,1].cluster { |x| x } #=> [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
197
+ ```
198
+
137
199
  ####Difference:####
138
200
  `difference` returns the difference of a collection of numbers.
139
201
 
@@ -187,6 +249,13 @@ Or install it yourself as:
187
249
  [1, 2, 3].exclude?(3) #=> false
188
250
  ```
189
251
 
252
+ ####Expand:####
253
+ `expand` expand all elements of an Enumerable object.
254
+
255
+ ```ruby
256
+ [0, 2..3, 5..7].expand #=> [0,[2, 3],[5,6,7]]
257
+ ```
258
+
190
259
  ####Exponential:####
191
260
  `exponential` returns the exponential of a collection of numbers.
192
261
 
@@ -204,6 +273,14 @@ Or install it yourself as:
204
273
  [1, :symbol, 'string', 3, :symbol, 1].frequencies #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }
205
274
  ```
206
275
 
276
+ ####Incase:####
277
+ `incase?` the same as #include? but tested using #=== instead of #==.
278
+
279
+ ```ruby
280
+ [1, 2, "a"].incase?(String) #=> true
281
+ [1, 2, "a"].incase?(3) #=> false
282
+ ```
283
+
207
284
  ####Many:####
208
285
  `many?` returns if collection has more than one element while respecting nil and false as an element. `Rails Safe`
209
286
 
@@ -407,6 +484,50 @@ h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
407
484
  { foo: "bar" }.reverse_merge!(baz: "boo", boo: "bam") #=> { foo: "bar", baz: "boo", boo: "bam" }
408
485
  ```
409
486
 
487
+ ####Sample:####
488
+ `sample` returns a random key-value pair.
489
+ `sample!` deletes a random key-value pair and returns that pair.
490
+
491
+ ```ruby
492
+ h = { a: 1, b: 2, c: 3, d: 4 }
493
+
494
+ h.sample #=> [:c, 3]
495
+ h.sample! #=> [:a, 1]
496
+ ```
497
+
498
+ ####Sample Key:####
499
+ `sample_key` returns a random key.
500
+ `sample_key!` delete a random key-value pair, returning the key.
501
+
502
+ ```ruby
503
+ h = { a: 1, b: 2, c: 3, d: 4 }
504
+
505
+ h.sample_key #=> :b
506
+ h.sample_key! #=> :d
507
+ ```
508
+
509
+ ####Sample Value:####
510
+ `sample_value` returns a random value.
511
+ `sample_value!` delete a random key-value pair, returning the value.
512
+
513
+ ```ruby
514
+ h = { a: 1, b: 2, c: 3, d: 4 }
515
+
516
+ h.sample_value #=> 1
517
+ h.sample_value! #=> 3
518
+ ```
519
+
520
+ ####Shuffle:####
521
+ `shuffle` returns a copy of the hash with values arranged in new random order.
522
+ `shuffle!` returns the hash with values arranged in new random order.
523
+
524
+ ```ruby
525
+ h = { a: 1, b: 2, c: 3, d: 4 }
526
+
527
+ h.shuffle #=> { b: 2, c: 3, a: 1, d: 4 }
528
+ h.shuffle! #=> { d: 4, b: 2, c: 3, a: 1 }
529
+ ```
530
+
410
531
  ####Slice:####
411
532
  `slice` a hash to include only the given keys. Returns a hash containing the given keys. `Rails Safe`
412
533
  `slice!` replaces the hash with only the given keys. Returns a hash containing the removed key/value pairs. `Rails Safe`
@@ -464,6 +585,27 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
464
585
 
465
586
  ## Integer
466
587
 
588
+ ####Factorial:####
589
+ `factorial` calculate the factorial of an integer.
590
+
591
+ ```ruby
592
+ 4.factorial #=> 24
593
+ ```
594
+
595
+ ####Of:####
596
+ `of` is like #times but returns a collection of the yield results.
597
+
598
+ ```ruby
599
+ 3.of { |i| "#{i+1}" } #=> ["1", "2", "3"]
600
+ ```
601
+
602
+ ####Roman:####
603
+ `roman` converts this integer to a roman numeral.
604
+
605
+ ```ruby
606
+ 49.roman #=> "XLIX"
607
+ ```
608
+
467
609
  ####Time:####
468
610
  `time` returns a Time object for the given Integer.
469
611
 
@@ -508,6 +650,13 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
508
650
  3.centuries_in_seconds #=> 9467280000.0
509
651
  ```
510
652
 
653
+ ####Days in Seconds:####
654
+ `day_in_seconds` and `days_in_seconds` returns the amount of seconds in n days.
655
+
656
+ ```ruby
657
+ 3.days_in_seconds #=> 259200
658
+ ```
659
+
511
660
  ####Decades in Seconds:####
512
661
  `decade_in_seconds` and `decades_in_seconds` returns the amount of seconds in n decades.
513
662
 
@@ -542,11 +691,12 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
542
691
  3.decimeters_in_meters #=> 0.3
543
692
  ```
544
693
 
545
- ####Days in Seconds:####
546
- `day_in_seconds` and `days_in_seconds` returns the amount of seconds in n days.
694
+ ####Distance:####
695
+ `distance` returns the absolute difference between numbers.
547
696
 
548
697
  ```ruby
549
- 3.days_in_seconds #=> 259200
698
+ 5.distance(3) #=> 2
699
+ 3.distance(5) #=> 2
550
700
  ```
551
701
 
552
702
  ####Divide:####
@@ -934,6 +1084,13 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
934
1084
  3.weeks_in_seconds #=> 1814400
935
1085
  ```
936
1086
 
1087
+ ####Within:####
1088
+ `within?` determines if another number is approximately equal within a given epsilon
1089
+
1090
+ ```ruby
1091
+ 10.006.within?(10, 0.1) #=> true
1092
+ ```
1093
+
937
1094
  ####Yards in Inches:####
938
1095
  `yard_in_inches` and `yards_in_inches` returns the amount of inches in n yards.
939
1096
 
@@ -994,6 +1151,13 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
994
1151
 
995
1152
  ## Range
996
1153
 
1154
+ ####Combine:####
1155
+ `combine` returns two concated ranges.
1156
+
1157
+ ```ruby
1158
+ (1..3).combine(7..9) #=> [1, 2, 3, 7, 8, 9]
1159
+ ```
1160
+
997
1161
  ####Include With Range:####
998
1162
  `include_with_range?` determines if a range includes another range. `Rails Safe`
999
1163
 
@@ -1011,6 +1175,30 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
1011
1175
  (1..5).overlaps?(7..9) # => false
1012
1176
  ```
1013
1177
 
1178
+ ####Sample:####
1179
+ `sample` returns a random element from the range.
1180
+
1181
+ ```ruby
1182
+ (1..5).sample # => 4
1183
+ ```
1184
+
1185
+ ####Shuffle:####
1186
+ `shuffle` returns a copy of a shuffled range of elements.
1187
+ `shuffle!` returns a shuffled range of elements.
1188
+
1189
+ ```ruby
1190
+ (1..5).shuffle # => [2, 5, 1, 4, 3]
1191
+ (1..5).shuffle! # => [3, 4, 5, 2, 1]
1192
+ ```
1193
+
1194
+ ####Within:####
1195
+ `within?` determines if one range is within another.
1196
+
1197
+ ```ruby
1198
+ (1..5).within?(2..4) # => true
1199
+ (1..5).within?(4..6) # => false
1200
+ ```
1201
+
1014
1202
  ## String
1015
1203
 
1016
1204
  ####Any:####
@@ -1232,6 +1420,14 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
1232
1420
  "this is <b>bold</b> and <em>emphatic</em>".strip_tags #=> "this is bold and emphatic"
1233
1421
  ```
1234
1422
 
1423
+ ####Sample:####
1424
+ `sample` and `sample!` deletes a random value and returns that value.
1425
+
1426
+ ```ruby
1427
+ "this thing that".sample #=> "thing"
1428
+ "this thing that".sample(" thing ") #=> "that"
1429
+ ```
1430
+
1235
1431
  ####Shift:####
1236
1432
  `shift` and `shift!` removes the first instance of a string.
1237
1433
 
@@ -1240,6 +1436,14 @@ h.slice!(:a, :b) #=> { c: 3, d: 4 }
1240
1436
  "this thing that thing".shift("this", "that") #=> " thing thing"
1241
1437
  ```
1242
1438
 
1439
+ ####Shuffle:####
1440
+ `shuffle` and `shuffle!` randomizes the characters in a string.
1441
+
1442
+ ```ruby
1443
+ "ruby rules".sample #=> "e lybrsuur"
1444
+ "ruby rules".sample! #=> "rblse syru"
1445
+ ```
1446
+
1243
1447
  ####Slugify:####
1244
1448
  `slugify` and `slugify!` generates a permalink-style string, with odd characters removed.
1245
1449
 
@@ -1375,8 +1579,8 @@ Time.now.format("month_name day, year hour:minute ampm") #=> "January 09, 2014 0
1375
1579
  | Time Zone - hour and minute offset | `zz` or `time_zone_offset` | %z | +09:00 |
1376
1580
  | Time Zone - hour, minute and second offset | `zzz` or `time_zone_offset_full` | %z | +09:00:00 |
1377
1581
 
1378
- ####To Format:####
1379
- `to_format` converts a Date or Time object to a predefined format.
1582
+ ####To Format / Stamp:####
1583
+ `to_format` and `stamp` converts a Date or Time object to a predefined format.
1380
1584
 
1381
1585
  **For a full list check out the time extention file.**
1382
1586
 
@@ -1,5 +1,17 @@
1
1
  class Array
2
2
 
3
+ def after(value)
4
+ return(nil) unless include?(value)
5
+
6
+ self[(index(value).to_i + 1) % size]
7
+ end
8
+
9
+ def before(value)
10
+ return(nil) unless include?(value)
11
+
12
+ self[(index(value).to_i - 1) % size]
13
+ end
14
+
3
15
  def delete_first
4
16
  self[1..-1]
5
17
  end
@@ -16,6 +28,19 @@ class Array
16
28
  replace(delete_last)
17
29
  end
18
30
 
31
+ def delete_values(*args)
32
+ result = []
33
+ args.each { |v| result << delete(v) }
34
+ return(result)
35
+ end
36
+
37
+ def duplicates(minimum=2)
38
+ hash = Hash.new(0)
39
+
40
+ each { |i| hash[i] += 1 }
41
+ hash.delete_if { |k, v| v < minimum }.keys
42
+ end
43
+
19
44
  unless defined?(Rails)
20
45
  def from(position)
21
46
  self[position, size] || []
@@ -66,6 +91,27 @@ class Array
66
91
  end
67
92
  end
68
93
 
94
+ def probability
95
+ hash = Hash.new(0.0)
96
+ differ = 0.0
97
+
98
+ each do |e|
99
+ hash[e] += 1.0
100
+ differ += 1.0
101
+ end
102
+
103
+ hash.keys.each { |e| hash[e] /= differ }
104
+ hash
105
+ end
106
+
107
+ def reject_values(*args)
108
+ reject { |x| args.include?(x) }
109
+ end
110
+
111
+ def sample!
112
+ delete_at(Random.rand(size - 1))
113
+ end
114
+
69
115
  unless defined?(Rails)
70
116
  def split(number=nil)
71
117
  if block_given?
@@ -1,5 +1,19 @@
1
1
  module Enumerable
2
2
 
3
+ def cluster(&block)
4
+ result = []
5
+
6
+ each do |element|
7
+ if result.last && (yield(result.last.last) == yield(element))
8
+ result.last << element
9
+ else
10
+ result << [element]
11
+ end
12
+ end
13
+
14
+ result
15
+ end
16
+
3
17
  def difference(identity=0, &block)
4
18
  if block_given?
5
19
  map(&block).difference(identity)
@@ -45,6 +59,10 @@ module Enumerable
45
59
  end
46
60
  end
47
61
 
62
+ def expand
63
+ map { |n| n.is_a?(Enumerable) ? n.expand : n }
64
+ end
65
+
48
66
  def exponential(identity=0, &block)
49
67
  if block_given?
50
68
  map(&block).exponential(identity)
@@ -57,6 +75,10 @@ module Enumerable
57
75
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
58
76
  end
59
77
 
78
+ def incase?(object)
79
+ any? { |x| object === x }
80
+ end
81
+
60
82
  unless defined?(Rails)
61
83
  def many?
62
84
  found_count = 0
@@ -102,6 +102,46 @@ class Hash
102
102
  end
103
103
  end
104
104
 
105
+ def sample
106
+ key = sample_key
107
+ [key, fetch(key)]
108
+ end
109
+
110
+ def sample!
111
+ key, value = sample
112
+ delete(key)
113
+ [key, value]
114
+ end
115
+
116
+ def sample_key
117
+ hash_keys = keys
118
+ hash_keys.at(Random.rand(hash_keys.size - 1))
119
+ end
120
+
121
+ def sample_key!
122
+ key, value = sample
123
+ delete(key)
124
+ key
125
+ end
126
+
127
+ def sample_value
128
+ fetch(sample_key)
129
+ end
130
+
131
+ def sample_value!
132
+ key, value = sample
133
+ delete(key)
134
+ value
135
+ end
136
+
137
+ def shuffle
138
+ Hash[to_a.sample(size)]
139
+ end
140
+
141
+ def shuffle!
142
+ replace(shuffle)
143
+ end
144
+
105
145
  unless defined?(Rails)
106
146
  def slice(*keys)
107
147
  keys.flatten.each_with_object(self.class.new) { |k, h| h[k] = self[k] if has_key?(k) }
@@ -1,5 +1,39 @@
1
1
  class Integer
2
2
 
3
+ ROMAN_VALUES = {
4
+ "M" => 1000,
5
+ "CM" => 900,
6
+ "D" => 500,
7
+ "CD" => 400,
8
+ "C" => 100,
9
+ "XC" => 90,
10
+ "L" => 50,
11
+ "XL" => 40,
12
+ "X" => 10,
13
+ "IX" => 9,
14
+ "V" => 5,
15
+ "IV" => 4,
16
+ "I" => 1
17
+ }
18
+
19
+ def factorial
20
+ return(1) if zero?
21
+ 2.upto(self).inject(1) { |product, n| product * n }
22
+ end
23
+
24
+ def of(&block)
25
+ Array.new(self, &block)
26
+ end
27
+
28
+ def roman
29
+ return("") if zero?
30
+ return("-#{(-self).roman}") if self < 0
31
+
32
+ ROMAN_VALUES.each do |key, value|
33
+ return(key + (self - value).roman) if value <= self
34
+ end
35
+ end
36
+
3
37
  def time
4
38
  Time.at(self)
5
39
  end
@@ -99,6 +99,10 @@ class Numeric
99
99
 
100
100
  alias_method :decimeter_in_meters, :decimeters_in_meters
101
101
 
102
+ def distance(n)
103
+ (self - n).abs
104
+ end
105
+
102
106
  def divide(n)
103
107
  self / n
104
108
  end
@@ -508,6 +512,14 @@ class Numeric
508
512
 
509
513
  alias_method :week_in_seconds, :weeks_in_seconds
510
514
 
515
+ def within?(number, epsilon=0.01)
516
+ return(self == number) if epsilon.zero?
517
+
518
+ a, b = to_f, number.to_f
519
+
520
+ (a.zero? || b.zero?) ? (a - b).abs < epsilon : (a / b - 1).abs < epsilon
521
+ end
522
+
511
523
  def yards_in_inches
512
524
  self * YARD
513
525
  end
@@ -1,12 +1,16 @@
1
1
  class Range
2
2
 
3
+ def combine(other)
4
+ to_a.concat(other.to_a)
5
+ end
6
+
3
7
  unless defined?(Rails)
4
- def include_with_range?(value)
5
- if value.is_a?(::Range)
6
- operator = exclude_end? && !value.exclude_end? ? :< : :<=
7
- include?(value.first) && value.last.send(operator, last)
8
+ def include_with_range?(other)
9
+ if other.is_a?(::Range)
10
+ operator = exclude_end? && !other.exclude_end? ? :< : :<=
11
+ include?(other.first) && other.last.send(operator, last)
8
12
  else
9
- include?(value)
13
+ include?(other)
10
14
  end
11
15
  end
12
16
  end
@@ -17,4 +21,16 @@ class Range
17
21
  end
18
22
  end
19
23
 
24
+ def sample
25
+ to_a.sample
26
+ end
27
+
28
+ def shuffle
29
+ to_a.shuffle
30
+ end
31
+
32
+ def within?(other)
33
+ cover?(other.first) && cover?(other.last)
34
+ end
35
+
20
36
  end
@@ -223,6 +223,14 @@ class String
223
223
  replace(remove_tags)
224
224
  end
225
225
 
226
+ def sample(separator=' ')
227
+ split(separator).sample
228
+ end
229
+
230
+ def sample!(separator=' ')
231
+ replace(sample(separator))
232
+ end
233
+
226
234
  def shift(*patterns)
227
235
  string = dup
228
236
  patterns.flatten.each do |pattern|
@@ -235,6 +243,14 @@ class String
235
243
  replace(shift(*patterns))
236
244
  end
237
245
 
246
+ def shuffle(separator='')
247
+ split(separator).shuffle.join
248
+ end
249
+
250
+ def shuffle!(separator='')
251
+ replace(shuffle(separator))
252
+ end
253
+
238
254
  def slugify
239
255
  gsub(/[^\x00-\x7F]+/, '').
240
256
  gsub(/[^\w_ \-]+/i, '').
@@ -147,4 +147,6 @@ class Time
147
147
  strftime(format_units.fetch(key))
148
148
  end
149
149
 
150
+ alias_method :stamp, :to_format
151
+
150
152
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveObject
2
- VERSION = "1.4.0"
2
+ VERSION = "2.0.0"
3
3
  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: 1.4.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-11 00:00:00.000000000 Z
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler