Top Level Namespace

Defined Under Namespace

Modules: Filigree Classes: AbstractClassError, AbstractMethodError, Array, CommandNotFoundError, MatchError

Instance Method Summary collapse

Instance Method Details

#check_array_type(array, type, blame: nil, nillable: false, strict: false) ⇒ Object

A method for type checking Ruby array values.

Parameters:

  • array (Array<Object>)

    Array of objects to type check.

  • type (Class)

    Class the objects should be an instance of.

  • blame (String, nil) (defaults to: nil)

    Variable name to blame for failed type checks.

  • nillable (Boolean) (defaults to: false)

    Object can be nil?

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively.

Returns:

  • (Object)

    The object passed in parameter array

Raises:

  • (ArgumentError)

    An error is raise if the type checking fails



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/filigree/types.rb', line 57

def check_array_type(array, type, blame: nil, nillable: false, strict: false)
	array.each do |obj|
		type_ok = (obj.nil? && nillable) || (strict ? obj.instance_of?(type) : obj.is_a?(type))

		if not type_ok
			if blame
				raise TypeError, "Parameter #{blame} must contain instances of the #{type.name} class."
			else
				raise TypeError, "Expected an object of type #{type.name}."
			end
		end
	end
end

#check_type(obj, type, blame: nil, nillable: false, strict: false) ⇒ Object

A method for type checking Ruby values.

Parameters:

  • obj (Object)

    Object to type check

  • type (Class)

    Class the object should be an instance of

  • blame (String, nil) (defaults to: nil)

    Variable name to blame for failed type checks

  • nillable (Boolean) (defaults to: false)

    Object can be nil?

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively

Returns:

  • (Object)

    The object passed as parameter obj

Raises:

  • (ArgumentError)

    An error is raise if the type checking fails.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/filigree/types.rb', line 30

def check_type(obj, type, blame: nil, nillable: false, strict: false)
	type_ok = (obj.nil? && nillable) || (strict ? obj.instance_of?(type) : obj.is_a?(type))

	if type_ok
		obj
	else
		if blame
			raise TypeError,
				"Parameter #{blame} must be an instance of the #{type.name} class.  Received an instance of #{obj.class.name}."
		else
			raise TypeError,
				"Expected an object of type #{type.name}.  Received an instance of #{obj.class.name}."
		end
	end
end

#match(*objects, &block) ⇒ Object

Returns Result of evaluating the matched pattern’s block.

Examples:

Using guard clauses

match o do
  with(n, -> { n < 0 }) { :NEG  }
  with(0)               { :ZERO }
  with(n, -> { n > 0 }) { :POS  }
end

Parameters:

  • objects (Object)

    Objects to be matched

  • block (Proc)

    Block containing with clauses.

Returns:

  • (Object)

    Result of evaluating the matched pattern’s block



157
158
159
160
161
162
163
# File 'lib/filigree/match.rb', line 157

def match(*objects, &block)
	me = Filigree::MatchEnvironment.new

	me.instance_exec(&block)

	me.find_match(objects)
end

#request_file(file, print_failure = false) ⇒ Object

Require a file, but fail gracefully if it isn’t found. If a block is given it will be called if the file is successfully required.

Parameters:

  • file (String)

    File to be requested

  • print_failure (Boolean) (defaults to: false)

    To print a message on failure or not



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/filigree/request_file.rb', line 23

def request_file(file, print_failure = false)
	begin
		require file
		yield if block_given?
	rescue LoadError
		if print_failure.is_a?(String)
			puts print_failure
		elsif print_failure
			puts "Unable to require file: #{file}"
		end
	end
end

#returning(value) {|value| ... } ⇒ Object

Simple implementation of the Y combinator.

Parameters:

  • value (Object)

    Value to be returned after executing the provided block.

Yields:

  • (value)

Returns:

  • (Object)

    The object passed in parameter value.



23
24
25
26
# File 'lib/filigree/object.rb', line 23

def returning(value)
	yield(value)
	value
end