Module: Brine::ParameterTransforming
- Included in:
- Brine, Selecting::Selector
- Defined in:
- lib/brine/transforming.rb
Overview
Convert provided paramters to richer types.
This eases use of types beyond the Cucumber-provided simple strings.
Defined Under Namespace
Classes: Transformer
Constant Summary collapse
- DATE =
Constants used for DateTime transformation.
'\d{4}-\d{2}-\d{2}'
- TIME =
'\d{2}:\d{2}:\d{2}'
- MILLIS =
'(?:\.\d{3})?'
- TZ =
'(?:Z|(?:[+-]\d{2}:\d{2}))'
Instance Method Summary collapse
-
#expand(val, binding) ⇒ Object
Expand val if needed and transform the result.
-
#parameter_transformers ⇒ Object
Expose the chain of Transformers which will be used to convert parameters.
-
#transformed_parameter(input) ⇒ Object
Transform the provided input using #parameter_transformers.
Instance Method Details
permalink #expand(val, binding) ⇒ Object
Expand val if needed and transform the result.
If val is not ‘expand`able it will be returned as is.
153 154 155 156 157 158 159 |
# File 'lib/brine/transforming.rb', line 153 def (val, binding) if val.respond_to? :expand transformed_parameter(val.(binding)) else val end end |
permalink #parameter_transformers ⇒ Object
Expose the chain of Transformers which will be used to convert parameters.
In the default implicit mode the list will be iterated over in sequence and the first Transformer which can handle the input will be used. The order of the Transformers is therefore significant: higher priority or more specific Transfomers should be earlier in the list than those that are lower priority or can handle a superset of supported input relative to those previous.
This exposed for direct list manipulation as an advanced customization point.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/brine/transforming.rb', line 85 def parameter_transformers @parameter_transformers ||= [ # These will be deprecated in preference for explicit type specification. Transformer.new('Double Quoted', /\A".*"\z/) {|input| input[1..-2] }, Transformer.new('Single Quoted', /\A'.*'\z/) {|input| input[1..-2] }, # Whitespace removal transforms # Handle stripping leading and trailing whitespace. # These are split out from the transforms to consolidate the behavior. # They call transform on the stripped value so that subsequent transforms no longer # have to deal with such whitespace. # # Note that these need to deal with multiline string arguments which require # the multiline flag and \A/\z anchors to properly operate on the full string rather than # being line oriented. The calls to +#strip+ are also not likely to properly clean up # multiline strings but is just meant as a (potentially ineffective) optimization over # recursive calls and capturing. Transformer.new('Trailing Whitespace', /\A\s+.*\z/) {|input| transformed_parameter(input.strip) }, Transformer.new('Leading Whitespace', /\A.*\s+\z/m) {|input| transformed_parameter(input.strip) }, # Template Expansion Transformer.new('Template', /.*{{.*}}.*/) {|input| as_template(input) }, # Scalars Transformer.new('Integer', /\A-?\d+\z/) {|input| input.to_i }, Transformer.new('Boolean', /\Atrue|false\z/) {|input| input.to_s == "true" }, # This presently does not support flags after the closing slash, support for these should be added as needed Transformer.new('Regex', /\A\/.*\/\z/) {|input| Regexp.new(input[1..-2]) }, Transformer.new('DateTime', /^#{DATE}T#{TIME}#{MILLIS}#{TZ}$/) {|input| Time.parse(input) }, # Structures Transformer.new('Array', /\A\[.*\]\z/m) {|input| JSON.parse(input) }, Transformer.new('Object', /\A{.*}\z$/m) {|input| JSON.parse(input) }, # String sentinel...this is last to act as a catch-all. Transformer.new('String', /.*/) {|input| input }, ] end |
permalink #transformed_parameter(input) ⇒ Object
Transform the provided input using #parameter_transformers.
140 141 142 143 |
# File 'lib/brine/transforming.rb', line 140 def transformed_parameter(input) parameter_transformers.find {|it| it.can_handle? input } .transform(input) end |