Class: Versionomy::Format::Delimiter::FieldHandler

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

Overview

This class handles the parsing and unparsing of a single field. It manages an ordered list of recognizers, each understanding a particular syntax. These recognizers are checked in order when parsing and unparsing.

Instance Method Summary collapse

Constructor Details

#initialize(field_, default_opts_ = {}, &block_) ⇒ FieldHandler

Creates a FieldHandler, using a DSL block appropriate to the field type to configure the recognizers.



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/versionomy/format/delimiter.rb', line 666

def initialize(field_, default_opts_={}, &block_)
  @field = field_
  @recognizers = []
  @requires_previous_field = default_opts_.fetch(:requires_previous_field, true)
  @default_value = default_opts_[:default_value] || field_.default_value
  @default_style = default_opts_.fetch(:default_style, nil)
  @style_unparse_param_key = "#{field_.name}_style".to_sym
  if block_
    builder_ = case field_.type
      when :integer
        Delimiter::IntegerFieldBuilder.new(@recognizers, field_, default_opts_)
      when :string
        Delimiter::StringFieldBuilder.new(@recognizers, field_, default_opts_)
      when :symbol
        Delimiter::SymbolFieldBuilder.new(@recognizers, field_, default_opts_)
    end
    ::Blockenspiel.invoke(block_, builder_)
  end
end

Instance Method Details

#default_valueObject

Returns the default value set when this field is missing from a version string.



698
699
700
# File 'lib/versionomy/format/delimiter.rb', line 698

def default_value
  @default_value
end

#get_recognizer(index_) ⇒ Object

Gets the given indexed recognizer. Returns nil if the index is out of range.



706
707
708
# File 'lib/versionomy/format/delimiter.rb', line 706

def get_recognizer(index_)
  @recognizers[index_]
end

#requires_previous_fieldObject

Returns true if this field can appear in an unparsed string only if the previous field also appears.



690
691
692
# File 'lib/versionomy/format/delimiter.rb', line 690

def requires_previous_field
  @requires_previous_field
end

#set_style_unparse_param(style_, unparse_params_) ⇒ Object

Finishes up parsing by setting the appropriate style field in the unparse_params, if needed.



714
715
716
717
718
# File 'lib/versionomy/format/delimiter.rb', line 714

def set_style_unparse_param(style_, unparse_params_)
  if style_ && style_ != @default_style
    unparse_params_[@style_unparse_param_key] = style_
  end
end

#unparse(value_, unparse_params_, required_for_later_) ⇒ Object

Unparse a string from this field value. This may return nil if this field is not required.



724
725
726
727
728
729
730
731
732
733
# File 'lib/versionomy/format/delimiter.rb', line 724

def unparse(value_, unparse_params_, required_for_later_)
  style_ = unparse_params_[@style_unparse_param_key] || @default_style
  @recognizers.each do |recog_|
    if recog_.should_unparse?(value_, style_)
      fragment_ = recog_.unparse(value_, style_, unparse_params_, required_for_later_)
      return fragment_ ? [fragment_, recog_.requires_next_field] : nil
    end
  end
  required_for_later_ ? ['', false] : nil
end