Class: Chelsy::Syntax::Coercer

Inherits:
Constraint show all
Defined in:
lib/chelsy/syntax.rb

Overview

This constraint accepts an instance of specific type of node, or can coerce some other type of objects to such a node.

Instance Attribute Summary

Attributes inherited from Constraint

#name

Instance Method Summary collapse

Methods inherited from Constraint

#===, #ensure

Constructor Details

#initialize(klass) {|value| ... } ⇒ Coercer

Initialize an instance.

Parameters:

  • klass (Class)

    type of acceptable node.

Yield Parameters:

  • value

    the value will be coerced.

Yield Returns:

  • (Node, nil)

    the coerced value of nil



54
55
56
57
58
59
# File 'lib/chelsy/syntax.rb', line 54

def initialize(klass, &block)
  @class = klass
  @coercer_block = block

  super klass.name
end

Instance Method Details

#accept?(node) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chelsy/syntax.rb', line 61

def accept?(node)
  @class === node || super(node)
end

#coerce(node) ⇒ Object



65
66
67
# File 'lib/chelsy/syntax.rb', line 65

def coerce(node)
  @coercer_block.call(node)
end