Class: Brine::ParameterTransforming::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/brine/transforming.rb

Overview

Transform supported input.

This implementation is designed around instances being defined with patterns which define whether they should handle provided input, and procs which should do the transformation (when the patterns match).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, pattern, &xfunc) ⇒ Transformer

Construct a new Transformer instance configured as specified.

Parameters:

  • type (String)

    Define the name of the type produced by this transformer. This will be used for upcoming explicit type conversion.

  • pattern (Regexp)

    Specify a pattern for which this Transformer will handle matching input.

  • xfunk (Proc)

    Provide the function which will be passed string input and will return the output type of this Transformer.

[View source]

39
40
41
42
43
# File 'lib/brine/transforming.rb', line 39

def initialize(type, pattern, &xfunc)
  @type = type
  @pattern = pattern
  @xfunc = xfunc
end

Instance Attribute Details

#typeObject (readonly)

Provide an identifier for this Transformer.


28
29
30
# File 'lib/brine/transforming.rb', line 28

def type
  @type
end

Instance Method Details

#can_handle?(input) ⇒ Boolean

Indicate whether this instance should attempt to transform the input.

Parameters:

  • input (String)

    Pass the String input as provided by Cucumber.

Returns:

  • (Boolean)

    Indicate whether this#transform should be called for input.

[View source]

51
52
53
# File 'lib/brine/transforming.rb', line 51

def can_handle?(input)
  input =~ @pattern
end

#transform(input) ⇒ Object

Transform the provided input.

Parameters:

  • input (String)

    Pass the String input as provided by Cucumber.

Returns:

  • (Object)

    Return input transformed into the appropriate type.

[View source]

61
62
63
64
# File 'lib/brine/transforming.rb', line 61

def transform(input)
  STDERR.puts("Handling #{input} as #{@type}") if ENV['BRINE_LOG_TRANSFORMS']
  @xfunc.call(input)
end