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
Class Method Summary collapse
-
.extract_variable(name:, object:, context:) ⇒ Object
private
Return a named variable from either an object or a binding.
-
.instance_method?(str) ⇒ Boolean
private
Check if a string is the name of an instance method.
-
.match?(object:, definition:) ⇒ Boolean
private
Check if an object matches a given type definition.
-
.object_to_type_def(obj) ⇒ String
private
Return the type definition for an object.
-
.symbol?(str) ⇒ Boolean
private
Check if a string is the name of a symbol.
-
.throw_error(object:, name:, definition:, result: false) ⇒ Object
private
Throw an error with an appropriate message for a given object not matching a given type definition.
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
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.
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
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
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.
This assume that each element returned by Enumerator#each has the same number of components.
Return the type definition for an object
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.
All this does is check whether the given string begins with a colon.
Check if a string is the name of a symbol
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
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 |