cdt-utilities 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
  SHA1:
3
- metadata.gz: b35d92ec31a7122dd1ae13949cceef741fbf338a
4
- data.tar.gz: 207f03d7041d2f356701ed17b11bf8405e07bfe4
3
+ metadata.gz: b4e8878b78d5eabb0daa6cffde78960045a0d1ce
4
+ data.tar.gz: 82254c3b751bb6545821b59124ee857ba4c586a3
5
5
  SHA512:
6
- metadata.gz: 40748ace7a1a55a0328369e28b534a36c0feecec27c208340c41e019ba15a35fb5ba55efc7d806506c956e4c6edd95e74c8bd8951dedb51418077bbcc6ac4825
7
- data.tar.gz: d2d5eb380260d1f30f8bcba6d8650d45b29ed8b3a5b44c05aeaa8475ed15da70ab9ea3443a5d967296485322352982b8ff75b91e5c9b1c2fb2057d5eb46b0e9b
6
+ metadata.gz: f691ee2fcf6446639552826ddf11d4ba6ef41fa61489c6a7678ca75fbcc077ea663c01a2a8ac854652fec08330f2e4d2c2fb2324b7bbea9768043acb4ed65c8f
7
+ data.tar.gz: 3ceca0564c007c4d01c3eb65625d685a22f64eb42b69e8ee6b498dad01c947e33e6087fb6001336281bef7887a9cc64f4a6a51171f29c5145b6a79a3ef629152
data/README.md CHANGED
@@ -13,7 +13,7 @@ Some Ruby utilities.
13
13
  Add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'cdt-utilities', "~> 0.1"
16
+ gem 'cdt-utilities', "~> 0.3"
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -26,7 +26,7 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
- ### CdT.bind_evaluation_of
29
+ ### CdT.bind_block_evaluation_to
30
30
 
31
31
  Utility to evaluate blocks binding `self` to an object only if the block
32
32
  does not take parameters.
@@ -43,12 +43,12 @@ require 'cdt/utilities/bind'
43
43
 
44
44
 
45
45
  # binds self to 'hello!'
46
- CdT.bind_evaluation_of proc { puts self }, to: 'hello!'
46
+ CdT.bind_block_evaluation_to('hello!') { puts self }
47
47
 
48
48
  # binding to self is not changed
49
- CdT.bind_evaluation_of proc { |string| puts string }, to: 'hello!')
49
+ CdT.bind_block_evaluation_to('hello!') { |string| puts string }
50
50
 
51
- CdT.bind_evaluation_of proc { |string, arg_1, arg_2| puts string + arg_1 + arg_2}, to: 'hello', with_args: [' world', '!'])
51
+ CdT.bind_block_evaluation_to('hello', ' world', '!') { |string, arg_1, arg_2| puts string + arg_1 + arg_2}
52
52
  ```
53
53
 
54
54
  ### CdT.subclass_responsibility
@@ -64,9 +64,55 @@ require 'cdt/utilities'
64
64
  require 'cdt/utilities/subclass-responsibility'
65
65
 
66
66
  class SomeClass
67
- def do_somehting()
68
- CdT.subclass_responsibility
69
- end
67
+ def do_somehting()
68
+ CdT.subclass_responsibility
69
+ end
70
+ end
71
+ ```
72
+
73
+ ### CdT.if_nil / if_not_nil
74
+
75
+ Utility to evaluate a block dependending on if an object is nil or not.
76
+
77
+ Examples:
78
+
79
+ ```ruby
80
+ require 'cdt/utilities'
81
+ # or if you just want this utility
82
+ require 'cdt/utilities/if-not-nil'
83
+
84
+ CdT.if_nil(nil) {7} # Evaluates to 7
85
+ CdT.if_nil(42) {7} # Evaluates to 42
86
+
87
+
88
+ CdT.if_not_nil(42) { |object| object + 1} # Evaluates to 43
89
+ CdT.if_not_nil(nil) { |object| object + 1} # Evaluates to nil
90
+
91
+ # Evaluates to 43
92
+ CdT.object 42,
93
+ if_not_nil: proc { |object| object + 1},
94
+ if_nil: proc { 7 }
95
+
96
+ CdT.object 42,
97
+ if_not_nil: proc { |object| object + 1},
98
+
99
+ # Evaluates to 7
100
+ CdT.object nil,
101
+ if_not_nil: proc { |object| object + 1},
102
+ if_nil: proc { 7 }
103
+
104
+ # Evaluates to 42
105
+ CdT.object 42,
106
+ if_nil: proc { 7 }
107
+ ```
108
+
109
+ A real world example of use is
110
+
111
+ ```ruby
112
+ def customer_name(customer_id)
113
+ CdT.object find_customer_by_id(customer_id),
114
+ if_not_nil: proc { |customer| customer.name },
115
+ if_nil: proc { raise "Customer with id=#{customer_id} not found." }
70
116
  end
71
117
  ```
72
118
 
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'cdt/utilities/version'
4
+ require 'cdt/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "cdt-utilities"
data/lib/cdt/utilities.rb CHANGED
@@ -1,3 +1,4 @@
1
- require_relative 'utilities/version'
1
+ require_relative 'version'
2
2
  require_relative 'utilities/bind'
3
- require_relative 'utilities/subclass-responsibility'
3
+ require_relative 'utilities/subclass-responsibility'
4
+ require_relative 'utilities/if-not-nil'
@@ -1,4 +1,13 @@
1
1
  module CdT
2
+ #
3
+ # This method is deprecated and will be removed in v1.0.0.
4
+ # Please use #bind_block_evaluation_to instead.
5
+ #
6
+ def self.bind_evaluation_of(block, to: nil, with_args: [])
7
+ warn "[DEPRECATION] `bind_evaluation_of` is deprecated and will be removed in v1.0.0. Please use `bind_block_evaluation_to` instead."
8
+ bind_block_evaluation_to(to, *with_args, &block)
9
+ end
10
+
2
11
  #
3
12
  # Utility to evaluate blocks binding :self to an object, but only if the block
4
13
  # does not take parameters.
@@ -6,12 +15,11 @@ module CdT
6
15
  # the binding as it is.
7
16
  #
8
17
  # Example:
9
- # CdT.bind_evaluation_of proc { puts self }, to: 'hello!' # binds self to 'hello!'
18
+ # CdT.bind_block_evaluation_to('hello!') { puts self } # binds self to 'hello!'
10
19
  #
11
- # CdT.bind_evaluation_of proc { |string| puts string }, to: 'hello!') # same binding
20
+ # CdT.bind_block_evaluation_to('hello!') { |string| puts string } # same binding
12
21
  #
13
- def self.bind_evaluation_of(block, to: nil, with_args: [])
14
- object = to
15
- block.arity == 0 ? object.instance_exec(&block) : block.call(object, *with_args)
22
+ def self.bind_block_evaluation_to(object, *args, &block)
23
+ block.arity == 0 ? object.instance_exec(&block) : block.call(object, *args)
16
24
  end
17
25
  end
@@ -0,0 +1,53 @@
1
+ module CdT
2
+ #
3
+ # Utility to evaluate a block only if an object is nil. Otherwise answers nil.
4
+ #
5
+ # Examples:
6
+ # CdT.if_nil(nil) {7} # Evaluates to 7
7
+ # CdT.if_nil(42) {7} # Evaluates to 42
8
+ #
9
+ def self.if_nil(object, &block)
10
+ return block.call if object.nil?
11
+ object
12
+ end
13
+
14
+ #
15
+ # Utility to evaluate a block only if an object is not nil. Otherwise answers nil.
16
+ #
17
+ # Examples:
18
+ # CdT.if_not_nil(42) { |object| object + 1} # Evaluates to 43
19
+ # CdT.if_not_nil(nil) { |object| object + 1} # Evaluates to nil
20
+ #
21
+ def self.if_not_nil(object, &block)
22
+ return block.call(object) unless object.nil?
23
+ object
24
+ end
25
+
26
+ #
27
+ # Utility to evaluate a block dependending on if the object is nil or not.
28
+ #
29
+ # Examples:
30
+ #
31
+ # # Evaluates to 43
32
+ # CdT.object 42,
33
+ # if_not_nil: proc { |object| object + 1},
34
+ # if_nil: proc { 7 }
35
+ #
36
+ # CdT.object 42,
37
+ # if_not_nil: proc { |object| object + 1},
38
+ #
39
+ # # Evaluates to 7
40
+ # CdT.object nil,
41
+ # if_not_nil: proc { |object| object + 1},
42
+ # if_nil: proc { 7 }
43
+ #
44
+ # # Evaluates to 42
45
+ # CdT.object 42,
46
+ # if_nil: proc { 7 }
47
+ #
48
+ def self.object(object, if_nil: nil, if_not_nil: nil)
49
+ return if_nil(object, &if_nil) if object.nil? && !if_nil.nil?
50
+ return if_not_nil(object, &if_not_nil) if !object.nil? && !if_not_nil.nil?
51
+ object
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module CdT
2
+ VERSION = "0.3.0"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdt-utilities
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
  - Martin Rubi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-15 00:00:00.000000000 Z
11
+ date: 2015-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,8 +56,9 @@ files:
56
56
  - lib/cdt/errors/subclass-responsibility-error.rb
57
57
  - lib/cdt/utilities.rb
58
58
  - lib/cdt/utilities/bind.rb
59
+ - lib/cdt/utilities/if-not-nil.rb
59
60
  - lib/cdt/utilities/subclass-responsibility.rb
60
- - lib/cdt/utilities/version.rb
61
+ - lib/cdt/version.rb
61
62
  homepage: https://github.com/cabeza-de-termo/ruby-utilities
62
63
  licenses:
63
64
  - MIT
@@ -78,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  version: '0'
79
80
  requirements: []
80
81
  rubyforge_project:
81
- rubygems_version: 2.4.6
82
+ rubygems_version: 2.4.5.1
82
83
  signing_key:
83
84
  specification_version: 4
84
85
  summary: Ruby utilities. No monkey patching if possible.
@@ -1,3 +0,0 @@
1
- module CdT
2
- VERSION = "0.2.0"
3
- end