Sha256: ea60120dab49f992d3e60e0dd2fc1d05479ebef33e161ded5bcd448801f1b69e

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

module Chandler
  module Refinements
    # Monkey patch String to provide conveniences for identifying strings that
    # represent a version, and converting between between tags (e.g "v1.0.2")
    # and version numbers ("1.0.2").
    #
    module VersionFormat
      refine String do
        # Does this string represent a version?
        #
        # "1.0.2".version?  # => true
        # "v1.0.2".version? # => true
        # "nope".version?   # => false
        # "".version?       # => false
        #
        def version?
          !!version_number
        end

        # The version number portion of the string, with the optional "v"
        # prefix removed.
        #
        # "1.0.2".version_number  # => "1.0.2"
        # "v1.0.2".version_number # => "1.0.2"
        # "nope".version_number   # => nil
        # "".version_number       # => nil
        #
        def version_number
          self[/^v?(#{Gem::Version::VERSION_PATTERN})$/, 1]
        end

        # The version number reformatted as a tag, by prefixing "v".
        #
        # "1.0.2".version_tag  # => "v1.0.2"
        # "v1.0.2".version_tag # => "v1.0.2"
        # "nope".version_tag   # => nil
        # "".version_tag       # => nil
        #
        def version_tag
          number = version_number
          number && "v#{version_number}"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
chandler-0.9.0 lib/chandler/refinements/version_format.rb
chandler-0.8.0 lib/chandler/refinements/version_format.rb
chandler-0.7.0 lib/chandler/refinements/version_format.rb
chandler-0.6.0 lib/chandler/refinements/version_format.rb
chandler-0.5.0 lib/chandler/refinements/version_format.rb
chandler-0.4.0 lib/chandler/refinements/version_format.rb
chandler-0.3.1 lib/chandler/refinements/version_format.rb
chandler-0.3.0 lib/chandler/refinements/version_format.rb
chandler-0.2.0 lib/chandler/refinements/version_format.rb
chandler-0.1.2 lib/chandler/refinements/version_format.rb
chandler-0.1.1 lib/chandler/refinements/version_format.rb
chandler-0.1.0 lib/chandler/refinements/version_format.rb