Class: Versionomy::Format::Delimiter::AlphabeticIntegerRecognizer

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

Overview

A recognizer for an alphabetic integer field. Such a field represents values 1-26 as letters of the English alphabet.

Instance Method Summary collapse

Methods inherited from RecognizerBase

#parse, #requires_next_field, #setup, #should_unparse?, #unparse

Constructor Details

#initialize(field_, opts_ = {}) ⇒ AlphabeticIntegerRecognizer

:nodoc:


921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/versionomy/format/delimiter.rb', line 921

def initialize(field_, opts_={})
  @case_unparse_param_key = "#{field_.name}_case".to_sym
  @case = opts_[:case]
  case @case
  when :upper
    value_regexp_ = '[A-Z]'
  when :lower
    value_regexp_ = '[a-z]'
  else #either
    value_regexp_ = '[a-zA-Z]'
  end
  setup(field_, value_regexp_, opts_)
end

Instance Method Details

#parsed_value(value_, parse_params_) ⇒ Object


935
936
937
938
939
940
941
942
943
944
# File 'lib/versionomy/format/delimiter.rb', line 935

def parsed_value(value_, parse_params_)
  value_ = value_.unpack('c')[0]  # Compatible with both 1.8 and 1.9
  if value_ >= 97 && value_ <= 122
    [value_ - 96, {@case_unparse_param_key => :lower}]
  elsif value_ >= 65 && value_ <= 90
    [value_ - 64, {@case_unparse_param_key => :upper}]
  else
    [0, nil]
  end
end

#unparsed_value(value_, style_, unparse_params_) ⇒ Object


946
947
948
949
950
951
952
953
954
955
956
# File 'lib/versionomy/format/delimiter.rb', line 946

def unparsed_value(value_, style_, unparse_params_)
  if value_ >= 1 && value_ <= 26
    if unparse_params_[@case_unparse_param_key] == :upper
      (value_+64).chr
    else
      (value_+96).chr
    end
  else
    value_.to_s
  end
end