Class: Algebrick::Matchers::Product
- Defined in:
- lib/algebrick/matchers/product.rb
Instance Attribute Summary collapse
-
#algebraic_type ⇒ Object
readonly
Returns the value of attribute algebraic_type.
-
#field_matchers ⇒ Object
readonly
Returns the value of attribute field_matchers.
Attributes inherited from Abstract
Instance Method Summary collapse
- #==(other) ⇒ Object
- #children ⇒ Object
-
#initialize(algebraic_type, *field_matchers) ⇒ Product
constructor
A new instance of Product.
- #to_s ⇒ Object
Methods inherited from Abstract
#!, #&, #===, #>, #assign!, #assign?, #assign_to_s, #assigned?, #assigns, #case, #children_including_self, #inspect, #matched?, #to_a, #|
Methods included from TypeCheck
#Child!, #Child?, #Match!, #Match?, #Type!, #Type?
Constructor Details
#initialize(algebraic_type, *field_matchers) ⇒ Product
Returns a new instance of Product.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/algebrick/matchers/product.rb', line 20 def initialize(algebraic_type, *field_matchers) super() @algebraic_type = Type! algebraic_type, Algebrick::ProductVariant, Algebrick::ParametrizedType raise ArgumentError unless algebraic_type.fields @field_matchers = case # AProduct.() when field_matchers.empty? ::Array.new(algebraic_type.fields.size) { Algebrick.any } # AProduct.(field_name: a_matcher) when field_matchers.size == 1 && field_matchers.first.is_a?(Hash) field_matchers = field_matchers.first unless (dif = field_matchers.keys - algebraic_type.field_names).empty? raise ArgumentError, "no #{dif} fields in #{algebraic_type}" end algebraic_type.field_names.map do |field| field_matchers.key?(field) ? field_matchers[field] : Algebrick.any end # normal else field_matchers end unless algebraic_type.fields.size == @field_matchers.size raise ArgumentError end end |
Instance Attribute Details
#algebraic_type ⇒ Object (readonly)
Returns the value of attribute algebraic_type.
18 19 20 |
# File 'lib/algebrick/matchers/product.rb', line 18 def algebraic_type @algebraic_type end |
#field_matchers ⇒ Object (readonly)
Returns the value of attribute field_matchers.
18 19 20 |
# File 'lib/algebrick/matchers/product.rb', line 18 def field_matchers @field_matchers end |
Instance Method Details
#==(other) ⇒ Object
57 58 59 60 61 |
# File 'lib/algebrick/matchers/product.rb', line 57 def ==(other) other.kind_of? self.class and self.algebraic_type == other.algebraic_type and self.field_matchers == other.field_matchers end |
#children ⇒ Object
49 50 51 |
# File 'lib/algebrick/matchers/product.rb', line 49 def children find_children @field_matchers end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/algebrick/matchers/product.rb', line 53 def to_s assign_to_s + "#{@algebraic_type.name}.(#{@field_matchers.join(', ')})" end |