Class: Taipo::TypeElement Private
- Inherits:
-
Object
- Object
- Taipo::TypeElement
- Defined in:
- lib/taipo/type_element.rb,
lib/taipo/type_element/children.rb,
lib/taipo/type_element/constraint.rb,
lib/taipo/type_element/constraints.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
An element representing a type (including children and constraints)
Defined Under Namespace
Classes: Children, Constraint, Constraints
Instance Attribute Summary collapse
-
#children ⇒ Object
private
The children for this element.
-
#constraints ⇒ Object
private
The constraints for this element.
-
#name ⇒ Object
private
The name of the element.
Instance Method Summary collapse
-
#==(comp) ⇒ Boolean
private
Compare the element with
comp
. -
#initialize(name:, children: nil, constraints: nil) ⇒ TypeElement
constructor
private
Initialize a new type element.
-
#match?(arg) ⇒ Boolean
private
Check if the argument matches the element.
-
#match_children?(arg) ⇒ Boolean
private
Check if the class of the argument’s children match.
-
#match_class?(arg) ⇒ Boolean
private
Check if the class of the argument itself matches this element.
-
#match_constraints?(arg) ⇒ Boolean
private
Check if the argument fits within the constraints.
-
#optional? ⇒ Boolean
private
Check whether this element is an optional.
-
#to_s ⇒ String
private
Return the String representation of this TypeElement.
Constructor Details
#initialize(name:, children: nil, constraints: nil) ⇒ TypeElement
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.
Initialize a new type element
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/taipo/type_element.rb', line 48 def initialize(name:, children: nil, constraints: nil) msg = 'Argument name was not a String.' raise ::TypeError, msg unless name.is_a? String msg = 'Argument name was an empty string.' raise ::ArgumentError if name.empty? msg = 'Argument children was not a Taipo::TypeElement::Children.' raise ::TypeError unless children.nil? || children.is_a?(Taipo::TypeElement::Children) msg = 'Argument children was empty.' raise ::ArgumentError if !children.nil? && children.empty? msg = 'Argument constraints was not a Taipo::TypeElement::Constraints.' raise ::TypeError unless constraints.nil? || constraints.is_a?(Taipo::TypeElement::Constraints) msg = 'Argument constraints was empty.' raise ::ArgumentError if !constraints.nil? && constraints.empty? @name = name @children = children @constraints = constraints end |
Instance Attribute Details
#children ⇒ 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.
The children for this element
25 26 27 |
# File 'lib/taipo/type_element.rb', line 25 def children @children end |
#constraints ⇒ 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.
The constraints for this element
31 32 33 |
# File 'lib/taipo/type_element.rb', line 31 def constraints @constraints end |
#name ⇒ 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.
The name of the element
19 20 21 |
# File 'lib/taipo/type_element.rb', line 19 def name @name end |
Instance Method Details
#==(comp) ⇒ 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.
Compare the element with comp
81 82 83 84 85 86 |
# File 'lib/taipo/type_element.rb', line 81 def ==(comp) msg = 'Object to be compared must be of type Taipo::TypeElement.' raise ::TypeError, msg unless comp.is_a? Taipo::TypeElement @name == comp.name && @children == comp.children end |
#match?(arg) ⇒ 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 the argument matches the element
125 126 127 128 129 |
# File 'lib/taipo/type_element.rb', line 125 def match?(arg) return true if optional? && arg.nil? match_class?(arg) && match_constraints?(arg) && match_children?(arg) end |
#match_children?(arg) ⇒ 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 the class of the argument’s children match
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/taipo/type_element.rb', line 160 def match_children?(arg) self_childless = @children.nil? arg_childless = !arg.is_a?(Enumerable) || arg.count == 0 return true if self_childless return false if !self_childless && arg_childless arg.all? do |a| if !arg.is_a?(Array) && a.is_a?(Array) a.each.with_index.reduce(nil) do |memo,(component,index)| result = @children[index].any? { |c| c.match? component } (memo.nil?) ? result : memo && result end else # The elements of this collection have no components @children.first.any? { |c| c.match? a } end end end |
#match_class?(arg) ⇒ 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 the class of the argument itself matches this element
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/taipo/type_element.rb', line 141 def match_class?(arg) actual_name = (optional?) ? @name[0..-2] : @name if actual_name == 'Boolean' arg.is_a?(TrueClass) || arg.is_a?(FalseClass) else msg = "Class to match #{actual_name} is not defined" raise Taipo::SyntaxError, msg unless Object.const_defined?(actual_name) arg.is_a? Object.const_get(actual_name) end end |
#match_constraints?(arg) ⇒ 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 the argument fits within the constraints
186 187 188 189 190 |
# File 'lib/taipo/type_element.rb', line 186 def match_constraints?(arg) return true if @constraints.nil? @constraints.all? { |c| c.constrain?(arg) } end |
#optional? ⇒ 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.
This merely checks whether #name ends in a question mark.
Check whether this element is an optional
An optional type is a variation on a normal type that also matches nil
. Taipo borrows the syntax used in some other languages of denoting optional types by appending a question mark to the end of the class name.
205 206 207 |
# File 'lib/taipo/type_element.rb', line 205 def optional? @name[-1] == '?' end |
#to_s ⇒ 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.
Return the String representation of this TypeElement
215 216 217 218 219 220 |
# File 'lib/taipo/type_element.rb', line 215 def to_s name_str = @name children_str = (@children.nil?) ? '' : @children.to_s constraints_str = (@constraints.nil?) ? '' : @constraints.to_s name_str + children_str + constraints_str end |