Class: Versionomy::Format::Delimiter::BasicIntegerRecognizer

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

Overview

A recognizer for a numeric integer field

Instance Method Summary (collapse)

Methods inherited from RecognizerBase

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

Constructor Details

- (BasicIntegerRecognizer) initialize(field_, opts_ = {})

:nodoc:



891
892
893
894
895
# File 'lib/versionomy/format/delimiter.rb', line 891

def initialize(field_, opts_={})
  @strip_leading_zeros = opts_[:strip_leading_zeros]
  @width_unparse_param_key = "#{field_.name}_width".to_sym
  setup(field_, '\d+', opts_)
end

Instance Method Details

- (Object) parsed_value(value_, parse_params_)



897
898
899
900
901
902
903
# File 'lib/versionomy/format/delimiter.rb', line 897

def parsed_value(value_, parse_params_)
  if !@strip_leading_zeros && value_ =~ /^0\d/
    [value_.to_i, {@width_unparse_param_key => value_.length}]
  else
    [value_.to_i, nil]
  end
end

- (Object) unparsed_value(value_, style_, unparse_params_)



905
906
907
908
909
910
911
# File 'lib/versionomy/format/delimiter.rb', line 905

def unparsed_value(value_, style_, unparse_params_)
  if !@strip_leading_zeros && (width_ = unparse_params_[@width_unparse_param_key])
    "%0#{width_.to_i}d" % value_
  else
    value_.to_s
  end
end