Sha256: 9eea42d578bb7a109ac93d1f981eed6ce2f58361549ef2d63d172699f16fc562

Contents?: true

Size: 961 Bytes

Versions: 13

Compression:

Stored size: 961 Bytes

Contents

# frozen_string_literal: true

require 'ostruct'

require_relative 'parts'
require_relative 'format'
require_relative 'visitors'
require_relative 'value_equality'

module Shivers
  class VersionDefinition
    PART_TYPES = {
      numeric: Parts::Numeric,
      alphanumeric: Parts::Alphanumeric,
      alphanumeric_or_hyphen: Parts::AlphanumericOrHyphen,
      static: Parts::Static
    }.freeze

    include ValueEquality

    attr_reader :parts, :format

    def initialize(definition)
      @parts =
        definition[:parts]
        .transform_values { |part| PART_TYPES[part[:type]].new(part) }
      @format = Format.new(definition[:formatter])
    end

    def parse(value)
      extract_visitor = Visitors::ExtractVisitor.new(@parts, value)

      @format.visit(extract_visitor)

      Version2.new(
        parts: @parts, format: @format,
        values: extract_visitor.result
      )
    end

    def state
      [@parts, @format]
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shivers-0.6.0.pre.15 lib/shivers/version_definition.rb
shivers-0.6.0.pre.14 lib/shivers/version_definition.rb
shivers-0.6.0.pre.13 lib/shivers/version_definition.rb
shivers-0.6.0.pre.12 lib/shivers/version_definition.rb
shivers-0.6.0.pre.11 lib/shivers/version_definition.rb
shivers-0.6.0.pre.10 lib/shivers/version_definition.rb
shivers-0.6.0.pre.9 lib/shivers/version_definition.rb
shivers-0.6.0.pre.8 lib/shivers/version_definition.rb
shivers-0.6.0.pre.7 lib/shivers/version_definition.rb
shivers-0.6.0.pre.6 lib/shivers/version_definition.rb
shivers-0.6.0.pre.5 lib/shivers/version_definition.rb
shivers-0.6.0.pre.4 lib/shivers/version_definition.rb
shivers-0.6.0.pre.3 lib/shivers/version_definition.rb