creative_rails_utilities 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +5 -0
- data/README.md +20 -0
- data/lib/creative_rails_utilities/object.rb +36 -0
- data/lib/creative_rails_utilities/version.rb +1 -1
- data/lib/creative_rails_utilities.rb +1 -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: 620bfb4a460b454df919b37aab5763681d8d40aa
|
4
|
+
data.tar.gz: 219d14e7b22adec3b646e0228b1ae9526d27308f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f769642de2b237235ccdb934993bbdca02b0932008fc8dc1b2c1a56a5d355d935e11c8c7c8eecbd985b4d3715723427fb7dd058372a1311d349753be18d39a68
|
7
|
+
data.tar.gz: 734ce1eaed645703c867ac60d4defcec7b43996197dceb3f8da9ae4d9ef80966376513d4a3ac8eae18a90441a928a978c22d2da0f4f30290e54e79d339f9a7fa
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -69,6 +69,7 @@ some_hash.fast_sort_keys #=> some_sorted_hash
|
|
69
69
|
# integer into letters
|
70
70
|
1.to_s26 #=> "a"
|
71
71
|
28.to_s26 #=> "ab"
|
72
|
+
# NB, not to be confused with 28.to_s(26) which operates as base (n) interpretation and starts with numbers.
|
72
73
|
```
|
73
74
|
|
74
75
|
```ruby
|
@@ -105,6 +106,25 @@ some_hash.fast_sort_keys #=> some_sorted_hash
|
|
105
106
|
(Time.zone.now.to_i - 3.minutes.ago.to_i).to_time_hash #=> {days: 0, hours: 0, minutes: 3, seconds: 0}
|
106
107
|
```
|
107
108
|
|
109
|
+
##### Object
|
110
|
+
|
111
|
+
__Object.chain_if(switch, operator, *args)__
|
112
|
+
__Object#chain_if(switch, operator, *args)__
|
113
|
+
```rb
|
114
|
+
# Allows smart conditional chaining of method calls or Procs
|
115
|
+
# Works on any type of object, class or instance
|
116
|
+
|
117
|
+
String.chain_if(true, :new, "abc") #=> "abc"
|
118
|
+
String.chain_if(false, :new, "abc") #=> String
|
119
|
+
String.chain_if(true, Proc.new{|a| a.new("abc") }) #=> "abc"
|
120
|
+
String.chain_if(false, Proc.new{|a| a.new("abc") }) #=> String
|
121
|
+
|
122
|
+
1.chain_if(false, Proc.new{raise}, 0).chain_if("yes", :*, 2).chain_if(!nil, Proc.new{|a| a.to_s}) #=> "2"
|
123
|
+
# Here `chain_if(false, Proc.new{raise}, 0)` would raise, but it is set to false, so it is skipped.
|
124
|
+
# `chain_if("yes", :*, 2)` multiplies by 2, and is ON (since "yes" is truthy)
|
125
|
+
# Finally, `chain_if(!nil, Proc.new{|a| a.to_s})` turns result to a string and also is ON.
|
126
|
+
```
|
127
|
+
|
108
128
|
##### String
|
109
129
|
|
110
130
|
```ruby
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
def self.chain_if(switch, operator, *args)
|
4
|
+
raise_chain_if_argument_error unless operator.class == Proc || operator.respond_to?(:to_sym)
|
5
|
+
|
6
|
+
return self unless switch
|
7
|
+
|
8
|
+
if operator.class == Proc
|
9
|
+
operator.call(self)
|
10
|
+
elsif operator.respond_to?(:to_sym)
|
11
|
+
return self.public_send(operator.to_sym, *args)
|
12
|
+
else
|
13
|
+
raise_chain_if_argument_error
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def chain_if(switch, operator, *args)
|
18
|
+
raise_chain_if_argument_error unless operator.class == Proc || operator.respond_to?(:to_sym)
|
19
|
+
|
20
|
+
return self unless switch
|
21
|
+
|
22
|
+
if operator.class == Proc
|
23
|
+
operator.call(self)
|
24
|
+
elsif operator.respond_to?(:to_sym)
|
25
|
+
return self.public_send(operator.to_sym, *args)
|
26
|
+
else
|
27
|
+
raise_chain_if_argument_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def raise_chain_if_argument_error
|
33
|
+
raise ArgumentError.new(":operator argument is bad, use a Proc ar a Symbol.")
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -8,6 +8,7 @@ require "creative_rails_utilities/error_helper"
|
|
8
8
|
require "creative_rails_utilities/hash"
|
9
9
|
require "creative_rails_utilities/date"
|
10
10
|
require "creative_rails_utilities/numeric"
|
11
|
+
require "creative_rails_utilities/object"
|
11
12
|
require "creative_rails_utilities/string"
|
12
13
|
require "creative_rails_utilities/version"
|
13
14
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: creative_rails_utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Creative
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -175,6 +175,7 @@ files:
|
|
175
175
|
- lib/creative_rails_utilities/error_helper.rb
|
176
176
|
- lib/creative_rails_utilities/hash.rb
|
177
177
|
- lib/creative_rails_utilities/numeric.rb
|
178
|
+
- lib/creative_rails_utilities/object.rb
|
178
179
|
- lib/creative_rails_utilities/railtie.rb
|
179
180
|
- lib/creative_rails_utilities/string.rb
|
180
181
|
- lib/creative_rails_utilities/version.rb
|