first_of 0.0.1 → 0.0.2

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: bfa6409aeab593f113e0a33c7101b4436e48a376
4
- data.tar.gz: 7819687d70b99d685eb4b558a2650849250f6df5
3
+ metadata.gz: 0a26712d5e174fa9c5c61029edd9abc2e65059b5
4
+ data.tar.gz: 4e3159d3fc6d1a3ff7c71cb769e49f34719158db
5
5
  SHA512:
6
- metadata.gz: 520d9061901771af7850f611e2696376a090447d6a236f6dc6d28819a176d00924ccb7c0b4b520990e1e37e9bed6a1fc3d8cb982b352874d8fb79787990202ac
7
- data.tar.gz: 0b218207ef65993a280fa96b1ffc79728350a954ab43075214165c6fd9747c47cfcd4d9896f80457ecf5c8e400065593ec1ed62301914cfe2d3a472125a87661
6
+ metadata.gz: d742cc1e16c2811b4f091d1e45b0a34472ad28cb8e41c24dcd3c62228786f02c689f37d67316e0a69cd339e6c8d77e626fb67eef28a6c25acb1abb56a8707c0e
7
+ data.tar.gz: 00e45ca8b4d912bde4a646521cf6a3422c1f5a2760952bda06daeb80aa62b82649709a57e55807c543aabe0905845a2aaa5c9062ea1d9451876bacea06ca0a9f
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # FirstOf
2
2
 
3
- TODO: Write a gem description
3
+ FirstOf can be used when multiple values can be reduced into a single return value.
4
+ This is common in things like XML/JSON where the "intended" value is available in
5
+ 2 fields and is determined by the presence of a value in the document and is often
6
+ prioritized (first check field 1, then field 2, then field x)
7
+
8
+ FirstOf handles calling methods/lambdas and prioritizing them.
4
9
 
5
10
  ## Installation
6
11
 
@@ -18,7 +23,37 @@ Or install it yourself as:
18
23
 
19
24
  ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ ```ruby
27
+ require 'first_of'
28
+
29
+ class Widget
30
+ include ::FirstOf
31
+
32
+ def nothing; nil; end
33
+ def selfie; self; end
34
+ def something; "something"; end
35
+ end
36
+
37
+ widget = Widget.new
38
+
39
+ # Use it to get to something usable on self
40
+ widget.first_of(:nothing, :something, :self) # => "something"
41
+ widget.first_of(:nothing, :self, :something) # => self
42
+ widget.first_of(:nothing, :nothing, :something) # => "something"
43
+
44
+ # Wrap calls in an Array to `try_chain` them
45
+ widget.first_of([:self, :nothing], :something) # => "something"
46
+
47
+ # Wrap a call in a lambda (or any callable) to delay execution
48
+ widget.first_of(:something, lambda { "derp" }) # => "something"
49
+ widget.first_of(lambda { "derp" }, :something) # => "derp"
50
+ widget.first_of(lambda { nothing }, :something) # => "something"
51
+
52
+ # Prioritize the calls/call chains if desired
53
+ widget.first_of(2 => :something, 1 => lambda { "derp" }) # => "derp"
54
+ widget.first_of(2 => lambda { "derp" }, 1 => :something) # => "something"
55
+ widget.first_of(2 => lambda { nothing }, 1 => :something) # => "something"
56
+ ```
22
57
 
23
58
  ## Contributing
24
59
 
@@ -16,28 +16,29 @@ module FirstOf
16
16
  # first_of(1 => :symbol1, 2 => 4) # will prioritize and return value (4) if first not present
17
17
  # first_of(lambda { _call }, 1 => :symbol1, 2 => :symbol2)
18
18
  def first_of(*args)
19
- extract_hash = args.extract_options!
19
+ return _first_of(*args, :try_chain, :proxy_try_chain)
20
+ end
20
21
 
21
- # Don't care if the hash is empty or not because of #each call
22
- sorted_keys = extract_hash.keys.sort
23
- sorted_keys.each do |key|
24
- args << extract_hash[key]
25
- end
22
+ def first_of!(*args)
23
+ return _first_of(*args, :try_chain!, :proxy_try_chain!)
24
+ end
26
25
 
27
- args.each do |argument|
28
- if argument.respond_to?(:call)
29
- value = argument.call
26
+ private
27
+
28
+ def _extract_from_message_chain(methods_or_value, try_method = :try_chain, proxy_try_method = :proxy_try_chain)
29
+ case methods_or_value
30
+ when Symbol, Array then
31
+ if self.respond_to?(:try_chain)
32
+ self.__send__(try_method, *methods_or_value)
30
33
  else
31
- value = _extract_from_message_chain(argument) # calls try_chain if array, or try if symbol, or returns value
34
+ ::TryChain.__send__(proxy_try_method, self, *methods_or_value)
32
35
  end
33
-
34
- return value if _valid_value?(value) # return value if found
36
+ else
37
+ methods_or_value
35
38
  end
36
-
37
- return nil
38
39
  end
39
40
 
40
- def first_of!(*args)
41
+ def _first_of(*args, try_method, proxy_try_method)
41
42
  extract_hash = args.extract_options!
42
43
 
43
44
  # Don't care if the hash is empty or not because of #each call
@@ -50,7 +51,7 @@ module FirstOf
50
51
  if argument.respond_to?(:call)
51
52
  value = argument.call
52
53
  else
53
- value = _extract_from_message_chain(argument, :try_chain!, :proxy_try_chain!) # calls try_chain if array, or try if symbol, or returns value
54
+ value = _extract_from_message_chain(argument, try_method, proxy_try_method)
54
55
  end
55
56
 
56
57
  return value if _valid_value?(value) # return value if found
@@ -59,21 +60,6 @@ module FirstOf
59
60
  return nil
60
61
  end
61
62
 
62
- private
63
-
64
- def _extract_from_message_chain(methods_or_value, try_method = :try_chain, proxy_try_method = :proxy_try_chain)
65
- case methods_or_value
66
- when Symbol, Array then
67
- if self.respond_to?(:try_chain)
68
- self.__send__(try_method, *methods_or_value)
69
- else
70
- ::TryChain.__send__(proxy_try_method, self, *methods_or_value)
71
- end
72
- else
73
- methods_or_value
74
- end
75
- end
76
-
77
63
  def _valid_value?(value)
78
64
  value.present? || [true, false].include?(value)
79
65
  end
@@ -1,3 +1,3 @@
1
1
  module FirstOf
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: first_of
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-19 00:00:00.000000000 Z
11
+ date: 2013-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport