class Object

Public Instance Methods

check_array_type(array, type, blame: nil, nillable: false, strict: false) click to toggle source

A method for type checking Ruby array values.

@param [Array<Object>] array Array of objects to type check. @param [Class] type Class the objects should be an instance of. @param [String, nil] blame Variable name to blame for failed type checks. @param [Boolean] nillable Object can be nil? @param [Boolean] strict Strict or non-strict checking. Uses `instance_of?` and `is_a?` respectively.

@raise [ArgumentError] An error is raise if the type checking fails

@return [Object] The object passed in parameter array

# 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) click to toggle source

A method for type checking Ruby values.

@param [Object] obj Object to type check @param [Class] type Class the object should be an instance of @param [String, nil] blame Variable name to blame for failed type checks @param [Boolean] nillable Object can be nil? @param [Boolean] strict Strict or non-strict checking. Uses `instance_of?` and `is_a?` respectively

@raise [ArgumentError] An error is raise if the type checking fails.

@return [Object] The object passed as parameter obj

# 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) click to toggle source

@example Using guard clauses

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

@param [Object] objects Objects to be matched @param [Proc] block Block containing with clauses.

@return [Object] Result of evaluating the matched pattern's block

# 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) { || ... } click to toggle source

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.

@param [String] file File to be requested @param [Boolean] print_failure To print a message on failure or not

# 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| ... } click to toggle source

Simple implementation of the Y combinator.

@param [Object] value Value to be returned after executing the provided block.

@return [Object] The object passed in parameter value.

# File lib/filigree/object.rb, line 23
def returning(value)
        yield(value)
        value
end