Module: CdT

Defined in:
lib/cdt/version.rb,
lib/cdt/utilities/bind.rb,
lib/cdt/utilities/undefined.rb,
lib/cdt/utilities/if-not-nil.rb,
lib/cdt/utilities/subclass-responsibility.rb,
lib/cdt/errors/subclass-responsibility-error.rb

Defined Under Namespace

Classes: SubclassResponsibilityError, Undefined

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.bind_block_evaluation_to(object, *args, &block) ⇒ Object

Utility to evaluate blocks binding :self to an object, but only if the block does not take parameters. If the block takes parameters, call the block with those parameters keeping the binding as it is.

Example: CdT.bind_block_evaluation_to('hello!') { puts self } # binds self to 'hello!'

	CdT.bind_block_evaluation_to('hello!') { |string| puts string } # same binding


22
23
24
# File 'lib/cdt/utilities/bind.rb', line 22

def self.bind_block_evaluation_to(object, *args, &block)
	block.arity == 0 ? object.instance_exec(&block) : block.call(object, *args)
end

.bind_evaluation_of(block, to: nil, with_args: []) ⇒ Object

This method is deprecated and will be removed in v1.0.0. Please use #bind_block_evaluation_to instead.



6
7
8
9
# File 'lib/cdt/utilities/bind.rb', line 6

def self.bind_evaluation_of(block, to: nil, with_args: [])
	warn "[DEPRECATION] `bind_evaluation_of` is deprecated and will be removed in v1.0.0.  Please use `bind_block_evaluation_to` instead."
	bind_block_evaluation_to(to, *with_args, &block)
end

.if_nil(object, &block) ⇒ Object

Utility to evaluate a block only if an object is nil. Otherwise answers nil.

Examples: CdT.if_nil(nil) 7 # Evaluates to 7 CdT.if_nil(42) 7 # Evaluates to 42



9
10
11
12
# File 'lib/cdt/utilities/if-not-nil.rb', line 9

def self.if_nil(object, &block)
	return block.call if object.nil?
	object
end

.if_not_nil(object, &block) ⇒ Object

Utility to evaluate a block only if an object is not nil. Otherwise answers nil.

Examples: CdT.if_not_nil(42) { |object| object + 1} # Evaluates to 43 CdT.if_not_nil(nil) { |object| object + 1} # Evaluates to nil



21
22
23
24
# File 'lib/cdt/utilities/if-not-nil.rb', line 21

def self.if_not_nil(object, &block)
	return block.call(object) unless object.nil?
	object
end

.object(object, if_nil: nil, if_not_nil: nil) ⇒ Object

Utility to evaluate a block dependending on if the object is nil or not.

Examples:

	# Evaluates to 43
CdT.object 42,
	if_not_nil: proc { |object| object + 1},
	if_nil: 		proc { 7 }

CdT.object 42,
	if_not_nil: proc { |object| object + 1},

	# Evaluates to 7
CdT.object nil,
	if_not_nil: proc { |object| object + 1},
	if_nil: 		proc { 7 }

	# Evaluates to 42
CdT.object 42,
	if_nil: proc { 7 }


48
49
50
51
52
# File 'lib/cdt/utilities/if-not-nil.rb', line 48

def self.object(object, if_nil: nil, if_not_nil: nil)
	return if_nil(object, &if_nil) if object.nil? && !if_nil.nil?
	return if_not_nil(object, &if_not_nil) if !object.nil? && !if_not_nil.nil?
	object
end

.set_undefined(object) ⇒ Object



10
11
12
# File 'lib/cdt/utilities/undefined.rb', line 10

def self.set_undefined(object)
	@undefined = object
end

.subclass_responsibilityObject

Utility to declare a method to be a responsibility of a subclass, and raise an error when the subclass did not implement it.

Example: class SomeClass def do_somehting() CdT.subclass_responsibility end end



15
16
17
# File 'lib/cdt/utilities/subclass-responsibility.rb', line 15

def self.subclass_responsibility()
	raise SubclassResponsibilityError.new
end

.undefinedObject

Do not use lazy initialization with ||= to avoid race conditions



6
7
8
# File 'lib/cdt/utilities/undefined.rb', line 6

def self.undefined()
	@undefined
end