exec_if 0.2.0 → 0.3.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
  SHA256:
3
- metadata.gz: '02853863be80496f7bab41f2b0c559fb91fa684fa44c17e541cb0bf18881df2c'
4
- data.tar.gz: b868aafc2de22d17d065bb991c56e1d18469a2599f2c0c7fbbec73c8538ff480
3
+ metadata.gz: 4ac67b2c6ec5888f840f2db51739610a920c0f96f106c3876dec4c0db54df449
4
+ data.tar.gz: c8e05d319530c7fddb58a15de72e732673004ff71719fa32f23ffde045e6bd21
5
5
  SHA512:
6
- metadata.gz: 1d6864e5748b400480f768de105dc2e6a83ec3ba64b02fb7810e9599d9f7208809eb1f063d75147e96965747c414f98cae0ab61fd12056bef7aa6efd49588d77
7
- data.tar.gz: 11357d316e387c96f9982fb2ed449de93c72c4925b2b4d5d9cae44562dc16d43aee2f32df7b369a47e26a6445e54a79e5541bd65af52b73c3b29aab07901e18c
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?) {|even| '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].presence) {|item, published_to| item.where(published_at: nil..published_to) }.
36
- exec_if(params[:name].presence) {|item, name| item.where("name LIKE ?", "%#{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.
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/exec_if'
2
+
3
+ puts 'hello'.concat(' world!').
4
+ exec_if(:ascii_only?) { upcase }.
5
+ exec_if("end_with?('?')") { delete_suffix('?') }.
6
+ exec_if(Proc.new {|str| str.length >= 5 }) { delete_prefix('HEL') }.
7
+ == 'LO WORLD!'
@@ -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) ? blk.call(self, obj) : self
5
+ obj.call(self) ? instance_exec(obj, &blk) : self
8
6
  when Symbol
9
- public_send(obj) ? blk.call(self, obj) : self
7
+ public_send(obj) ? instance_exec(obj, &blk) : self
10
8
  when String
11
- eval(obj) ? blk.call(self, obj) : self
9
+ instance_eval(obj) ? instance_exec(obj, &blk) : self
12
10
  when Object
13
- obj ? blk.call(self, obj) : self
11
+ obj ? instance_exec(obj, &blk) : self
14
12
  when nil, false
15
13
  self
16
14
  end
@@ -1,3 +1,3 @@
1
1
  module ExecIf
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-16 00:00:00.000000000 Z
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