Class: Versionomy::Format::Delimiter::MappingSymbolRecognizer

Inherits:
RecognizerBase
  • Object
show all
Defined in:
lib/versionomy/format/delimiter.rb

Overview

A recognizer for symbolic fields that recognizes a mapping of values to regular expressions.

Instance Method Summary collapse

Methods inherited from RecognizerBase

#parse, #requires_next_field, #setup, #unparse

Constructor Details

#initialize(field_, opts_ = {}, &block_) ⇒ MappingSymbolRecognizer

:nodoc:



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/versionomy/format/delimiter.rb', line 1012

def initialize(field_, opts_={}, &block_)
  @mappings_in_order = []
  @mappings_by_value = {}
  builder_ = Delimiter::MappingSymbolBuilder.new(@mappings_in_order, @mappings_by_value)
  ::Blockenspiel.invoke(block_, builder_)
  regexps_ = @mappings_in_order.map{ |map_| "(#{map_[0]})" }
  setup(field_, regexps_.join('|'), opts_)
  @mappings_in_order.each do |map_|
    map_[0] = ::Regexp.new("\\A(#{map_[0]})", @regexp_options)
  end
end

Instance Method Details

#parsed_value(value_, parse_params_) ⇒ Object



1024
1025
1026
1027
1028
1029
# File 'lib/versionomy/format/delimiter.rb', line 1024

def parsed_value(value_, parse_params_)
  @mappings_in_order.each do |map_|
    return [map_[2], nil] if map_[0].match(value_)
  end
  nil
end

#should_unparse?(value_, style_) ⇒ Boolean

Returns:

  • (Boolean)


1035
1036
1037
# File 'lib/versionomy/format/delimiter.rb', line 1035

def should_unparse?(value_, style_)
  style_ == @style && @mappings_by_value.include?(value_)
end

#unparsed_value(value_, style_, unparse_params_) ⇒ Object



1031
1032
1033
# File 'lib/versionomy/format/delimiter.rb', line 1031

def unparsed_value(value_, style_, unparse_params_)
  @mappings_by_value[value_][1]
end