Module: Taipo::Utilities Private

Defined in:
lib/taipo/utilities.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Utility methods for Taipo

Since:

  • 1.4.0

Class Method Summary collapse

Class Method Details

.extract_variable(name:, object:, context:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a named variable from either an object or a binding

Parameters:

  • name (String)

    the name of the variable

  • object (Object)

    the object in which the variable may exist

  • context (Binding)

    the binding in which the variable may exist

Returns:

  • (Object)

    the variable

Raises:

Since:

  • 1.5.0



24
25
26
27
28
29
30
31
32
33
# File 'lib/taipo/utilities.rb', line 24

def self.extract_variable(name:, object:, context:)
  if name[0] == '@' && object.instance_variable_defined?(name)
    object.instance_variable_get name
  elsif name[0] != '@' && context.local_variable_defined?(name)
    context.local_variable_get name
  else
    msg = "Argument '#{name}' is not defined."
    raise Taipo::NameError, msg
  end
end

.instance_method?(str) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

All this does is check whether the given string begins with a hash symbol.

Check if a string is the name of an instance method

Parameters:

  • str (String)

    the string to check

Returns:

  • (Boolean)

    the result

Since:

  • 1.4.0



46
47
48
# File 'lib/taipo/utilities.rb', line 46

def self.instance_method?(str)
  str[0] == '#'
end

.match?(object:, definition:) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if an object matches a given type definition

Parameters:

  • object (Object)

    the object to check

  • definition (String)

    the type definiton to check against

Returns:

  • (Boolean)

    the result

Raises:

  • (::TypeError)

    if definition is not a String

  • (Taipo::SyntaxError)

    if the type definitions in checks are invalid

Since:

  • 1.5.0



63
64
65
66
67
68
69
# File 'lib/taipo/utilities.rb', line 63

def self.match?(object:, definition:)
  msg = "The 'definition' argument must be of type String."
  raise ::TypeError, msg unless definition.is_a? String

  types = Taipo::Parser.parse definition
  types.any? { |t| t.match? object }
end

.object_to_type_def(obj) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This assume that each element returned by Enumerator#each has the same number of components.

Return the type definition for an object

Parameters:

  • obj (Object)

    the object

Returns:

  • (String)

    a type definition of the object

Since:

  • 1.4.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/taipo/utilities.rb', line 82

def self.object_to_type_def(obj)
  return obj.class.name unless obj.is_a? Enumerable

  if obj.is_a? Array
    element_types = Hash.new
    obj.each { |o| element_types[self.object_to_type_def(o)] = true }
    if element_types.empty?
      obj.class.name
    else
      obj.class.name + '<' + element_types.keys.join('|') + '>'
    end
  else
    element_types = Array.new
    obj.each.with_index do |element,index_e|
      element.each.with_index do |component,index_c|
        element_types[index_c] = Hash.new if index_e == 0
        c_type = self.object_to_type_def(component)
        element_types[index_c][c_type] = true
      end
    end
    inner = element_types.reduce('') do |memo,e|
      e_type = e.keys.join('|')
      (memo == '') ? e_type : memo + ',' + e_type
    end
    if element_types.empty?
      obj.class.name
    else
      obj.class.name + '<' + inner + '>'
    end
  end
end

.symbol?(str) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

All this does is check whether the given string begins with a colon.

Check if a string is the name of a symbol

Parameters:

  • str (String)

    the string to check

Returns:

  • (Boolean)

    the result

Since:

  • 1.4.0



124
125
126
# File 'lib/taipo/utilities.rb', line 124

def self.symbol?(str)
  str[0] == ':'
end

.throw_error(object:, name:, definition:, result: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Throw an error with an appropriate message for a given object not matching a given type definition

Parameters:

  • object (Object)

    the object that does not match definition

  • name (String)

    the name of the object (with the code that originally called the #check method)

  • definition (String)

    the type definition that does not match object

  • result (Boolean) (defaults to: false)

    whether this is being called in respect of a return value (such as by Result::ClassMethods#result)

Raises:

Since:

  • 1.5.0



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/taipo/utilities.rb', line 142

def self.throw_error(object:, name:, definition:, result: false)
  subject = (result) ? "The return value of #{name}" : "Object '#{name}'"

  if Taipo::Utilities.instance_method? definition
    msg = "#{subject} does not respond to #{definition}."
  elsif Taipo::Utilities.symbol? definition
    msg = "#{subject} is not equal to #{definition}."
  elsif object.is_a? Enumerable
    type_def = Taipo::Utilities.object_to_type_def object
    msg = "#{subject} is #{type_def} but expected #{definition}."
  else
    class_name = object.class.name
    msg = "#{subject} is #{class_name} but expected #{definition}."
  end

  raise Taipo::TypeError, msg
end