Class: Taipo::TypeElement Private

Inherits:
Object
  • Object
show all
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)

Since:

  • 1.0.0

Defined Under Namespace

Classes: Children, Constraint, Constraints

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

Raises:

  • (::TypeError)

    if name, children or constraints was of the wrong type

  • (::ArgumentError)

    if name, children or constraints was blank/empty

Since:

  • 1.0.0



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

#childrenObject

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

Since:

  • 1.4.0



25
26
27
# File 'lib/taipo/type_element.rb', line 25

def children
  @children
end

#constraintsObject

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

Since:

  • 1.0.0



31
32
33
# File 'lib/taipo/type_element.rb', line 31

def constraints
  @constraints
end

#nameObject

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

Since:

  • 1.0.0



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

Parameters:

Returns:

  • (Boolean)

    the result

Raises:

  • (::TypeError)

    if comp is of the wrong type

Since:

  • 1.0.0



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

Parameters:

  • arg (Object)

    the argument to compare

Returns:

  • (Boolean)

    the result

Raises:

Since:

  • 1.0.0



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

Parameters:

  • arg (Object)

    the argument to compare

Returns:

  • (Boolean)

    the result

Since:

  • 1.4.0



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

Parameters:

  • arg (Object)

    the argument to compare

Returns:

  • (Boolean)

    the result

Raises:

Since:

  • 1.0.0



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

Parameters:

  • arg (Object)

    the argument to compare

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



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.

Note:

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.

Returns:

  • (Boolean)

    the result

Since:

  • 1.3.0



205
206
207
# File 'lib/taipo/type_element.rb', line 205

def optional?
  @name[-1] == '?'
end

#to_sString

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

Returns:

  • (String)

    the representation as a String

Since:

  • 1.1.0



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