selfie_chain 1.0.1 → 1.1.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: 033ae7411b48d5416bc91fa424357e9b1d317a99
4
- data.tar.gz: 62f2ebc53dbc2d55cae58239cbee3dfda247e7e1
3
+ metadata.gz: 0e43376cb6c32cb841c0d9fb7dfac7352295aae5
4
+ data.tar.gz: 2639603a7f4d8fac95d09007a9c50cbccb6911a9
5
5
  SHA512:
6
- metadata.gz: af27d883dbd70607629ab8d3ba6f0721fc1673b98b1f950f23d211132df60f725795248248cb1e580ecf882f7ff7d62158b55cd2deb887f6900e761113584693
7
- data.tar.gz: a0565cb31fb1c7efc99ecc186e375ba28547642ba783b721b7d7b1b0d038d070d332a673d82ba6db8d9b0916fa73ba02698d4cb293e23e7dba56de5e7964a0a9
6
+ metadata.gz: f71b161f6e16d1fb7f95f01d42f3dc77a67f1118bc2ba62001145248e8b02ce35973854c7911e979a54328d0c24f98eeeed94dd7f1334412d32cc0bc256a0851
7
+ data.tar.gz: 13271f04584c5cee98eab03ef43d5ade3e971cbbbfcf3f855efb67cf26b8d3828a84b659afd5728004e110e5a7a3c4013337c65f3f8482b3ffc92e003161475f
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SelfieChain
2
2
 
3
- Now every object can take a selfie. SelfieChain is an attempt to one-shot-complement ruby's ternary operator, null guard and somehow extend Rails' :try method (but you don't need Rails). I realised that there are use cases, that the above mentioned tools fail to address in sufficently elegant way. Look below for some explanatory examples and start taking selfies :)
3
+ Now every object can take a selfie. SelfieChain is an attempt to complement ruby's ternary operator, null guard and somehow extend Rails' :try method (but you don't need Rails), all in one-shot. I realised that there are use cases, that the above mentioned tools fail to address in sufficently elegant way. Look below for some explanatory examples and start taking selfies :)
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- SelfieChain adds a :selfie method to Object class (as well as a SelfieChain < BasicObject class). Calling it with or without a block changes it's behavior as follows:
23
+ SelfieChain adds a :selfie method to Object class (as well as a SelfieChain:Wrapper < BasicObject class). Calling it with or without a block changes it's behavior as follows:
24
24
 
25
25
  ####1. With block (and one optional argument that defaults to nil).
26
26
 
@@ -47,9 +47,24 @@ This let's you stop worrying about checking for possible nil every time you chai
47
47
  This time selfie returns SelfieChain.new(self) - instance of SelfieChain < BasicObject class that wraps the receiver. This wrapper delegates every method call (except for :share_selfie and those defined by BasicObject)
48
48
  to the wrapped object - and swaps it with the result unless the wrapped object doesn't respond_to? the method, in which case the wrapper stores nil. NOTE, that once the wrapped object hits nil, it always stays nil (this conforms to the Rails' :try method mechanics). You can end this method calling chain party and retrieve the result by calling :share_selfie.
49
49
 
50
- ________________________________
50
+ ####3. Share_selfie with block (from v.1.1.0)
51
+
52
+ Now you can pass a block to share selfie:
53
+
54
+ h[:a][:b][:c] = {"d" => "14"}
55
+ h.selfie[:a][:b][:c]["d"].to_i.share_selfie(-1) {|x| false} => -1
56
+
57
+ It works just like (...).share_selfie.selfie(-1) {|x| false} with the only exception, that when share_selfie is to yield nil, it still yields nil no matter what the given block evaluates to:
58
+
59
+ h[:a][:b] = {c: "14"}
60
+ h.selfie[:a][:b][:c]["d"].to_i.share_selfie(-1) {|x| false} => nil
61
+ h.selfie[:a][:b][:c]["d"].to_i.share_selfie.selfie(-1) {|x| false} => -1
62
+
63
+
64
+ ----------------------------
51
65
  * Here we use string "d" instead of symbol :d to avoid known "no implicit conversion of Symbol into Integer" exception raised by calling "14"[:d].
52
66
 
67
+
53
68
  ## Development
54
69
 
55
70
  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.
@@ -1,21 +1,5 @@
1
1
  require "selfie_chain/version"
2
-
3
- class SelfieChain < BasicObject
4
-
5
- def initialize(subject)
6
- @subject = subject
7
- end
8
-
9
- def method_missing(method, *args, &block)
10
- @subject = !@subject.nil? && @subject.respond_to?(method) ? @subject.public_send(method, *args, &block) : nil
11
- self
12
- end
13
-
14
- def share_selfie
15
- @subject
16
- end
17
-
18
- end
2
+ require "selfie_chain/wrapper"
19
3
 
20
4
  class Object
21
5
 
@@ -23,7 +7,8 @@ class Object
23
7
  if block_given?
24
8
  yield(self) ? self : other
25
9
  else
26
- SelfieChain.new(self)
10
+ SelfieChain::Wrapper.new(self)
27
11
  end
28
12
  end
13
+
29
14
  end
@@ -1,4 +1,4 @@
1
- class SelfieChain < BasicObject
2
- VERSION = "1.0.1"
1
+ module SelfieChain
2
+ VERSION = "1.1.0"
3
3
  end
4
4
 
@@ -0,0 +1,29 @@
1
+ module SelfieChain
2
+ class Wrapper < BasicObject
3
+
4
+ def initialize(subject)
5
+ @subject = subject
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ if !@subject.nil? && @subject.respond_to?(method)
10
+ @subject = @subject.public_send(method, *args, &block)
11
+ end
12
+ self
13
+ end
14
+
15
+ # returns @subject or other, if the optional block evaluates to true (and @subject is not nil)
16
+ def share_selfie(other = nil, &block)
17
+ # this means it's NOT equivalent to .share_selfie.selfie(other, &block)
18
+ return if @subject.nil?
19
+
20
+ # block_given? isn't defined on BasicObject
21
+ if block.nil?
22
+ @subject
23
+ else
24
+ yield(@subject) ? @subject : other
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Maciej Satkiewicz"]
10
10
  spec.email = ["msatkiewicz@gmail.com"]
11
11
 
12
- spec.summary = %q{Adds Object#selfie (and SelfieChain < BasicObject) to complement ternary operator, null guard and Rails' :try method at once}
13
- spec.description = %q{Complements ternary operator, null guard and Rails' :try method by adding simple Object#selfie and SelfieChain < BasicObject}
12
+ spec.summary = %q{Adds Object#selfie (and SelfieChain::Wrapper < BasicObject) to complement ternary operator, null guard and Rails' :try method at once}
13
+ spec.description = %q{Complements ternary operator, null guard and Rails' :try method by adding simple Object#selfie and SelfieChain::Wrapper < BasicObject}
14
14
  spec.homepage = "https://github.com/swarzkopf314/selfie_chain"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfie_chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Satkiewicz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-30 00:00:00.000000000 Z
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Complements ternary operator, null guard and Rails' :try method by adding
70
- simple Object#selfie and SelfieChain < BasicObject
70
+ simple Object#selfie and SelfieChain::Wrapper < BasicObject
71
71
  email:
72
72
  - msatkiewicz@gmail.com
73
73
  executables: []
@@ -84,6 +84,7 @@ files:
84
84
  - bin/setup
85
85
  - lib/selfie_chain.rb
86
86
  - lib/selfie_chain/version.rb
87
+ - lib/selfie_chain/wrapper.rb
87
88
  - selfie_chain.gemspec
88
89
  homepage: https://github.com/swarzkopf314/selfie_chain
89
90
  licenses:
@@ -108,6 +109,6 @@ rubyforge_project:
108
109
  rubygems_version: 2.4.8
109
110
  signing_key:
110
111
  specification_version: 4
111
- summary: Adds Object#selfie (and SelfieChain < BasicObject) to complement ternary
112
- operator, null guard and Rails' :try method at once
112
+ summary: Adds Object#selfie (and SelfieChain::Wrapper < BasicObject) to complement
113
+ ternary operator, null guard and Rails' :try method at once
113
114
  test_files: []