exec_if 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -3
- data/examples/usage.rb +7 -0
- data/lib/exec_if.rb +4 -6
- data/lib/exec_if/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ac67b2c6ec5888f840f2db51739610a920c0f96f106c3876dec4c0db54df449
|
4
|
+
data.tar.gz: c8e05d319530c7fddb58a15de72e732673004ff71719fa32f23ffde045e6bd21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7023c5c850a6e74d62acaee5cae35a3912e29bc98a0e6b3d65babc32abd8312fd3a1bdbbb948a68b1b07e412dc950d268a5a5b6824093ab37428ac2b763e886a
|
7
|
+
data.tar.gz: 06a1bda18c4608c5cb72747ac6798f4d5a9571ef325a3a99eeb36b40a9d6f1b982393bc2df74fd24ebbe5c36df66f4632ef50ce91f333e48d32c8d2f40350684
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Or install it yourself as:
|
|
24
24
|
require 'exec_if'
|
25
25
|
|
26
26
|
[1, 2, 3, 4, 5].map do |i|
|
27
|
-
i.exec_if(i.even?) {
|
27
|
+
i.exec_if(i.even?) { 'even' }
|
28
28
|
end # => [1, 'even', 3, 'even', 5]
|
29
29
|
```
|
30
30
|
|
@@ -32,11 +32,20 @@ It is especially useful for long method chains.
|
|
32
32
|
|
33
33
|
```ruby
|
34
34
|
Item.active.
|
35
|
-
exec_if(params[:published_to].
|
36
|
-
exec_if(params[:name].
|
35
|
+
exec_if(params[:published_to].present?) { where(published_at: nil..params[:published_to]) }.
|
36
|
+
exec_if(params[:name].present?) { where("name LIKE ?", "%#{params[:name]}%")}.
|
37
37
|
order(published_at: :desc)
|
38
38
|
```
|
39
39
|
|
40
|
+
Condition part of `exec_if` can be one of these:
|
41
|
+
|
42
|
+
* `Proc`, which is called with self
|
43
|
+
* `Symbol`, which is the name of the message of send
|
44
|
+
* `String`, which is `eval`uated in context of self
|
45
|
+
* `Object`, which is simply used to check truthyness
|
46
|
+
|
47
|
+
See `examples` directory for usage.
|
48
|
+
|
40
49
|
## Development
|
41
50
|
|
42
51
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/examples/usage.rb
ADDED
data/lib/exec_if.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
|
-
require "exec_if/version"
|
2
|
-
|
3
1
|
class Object
|
4
2
|
def exec_if(obj, &blk)
|
5
3
|
case obj
|
6
4
|
when Proc
|
7
|
-
obj.call(self) ?
|
5
|
+
obj.call(self) ? instance_exec(obj, &blk) : self
|
8
6
|
when Symbol
|
9
|
-
public_send(obj) ?
|
7
|
+
public_send(obj) ? instance_exec(obj, &blk) : self
|
10
8
|
when String
|
11
|
-
|
9
|
+
instance_eval(obj) ? instance_exec(obj, &blk) : self
|
12
10
|
when Object
|
13
|
-
obj ?
|
11
|
+
obj ? instance_exec(obj, &blk) : self
|
14
12
|
when nil, false
|
15
13
|
self
|
16
14
|
end
|
data/lib/exec_if/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exec_if
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
|
+
- examples/usage.rb
|
71
72
|
- exec_if.gemspec
|
72
73
|
- lib/exec_if.rb
|
73
74
|
- lib/exec_if/version.rb
|