Sha256: 03a37e3247d812cf40e5a84e851d7b0f145673506b23e91fb097fcdaebe3286b

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Raph
  # This module consists of argument parsers for Humans
  module Parser
    # Base class for all argument parsers.
    #
    # You can create a custom parser by subclassing
    # `Raph::Parser::BaseParser` and overriding some methods,
    # or by implementing all the methods by duck typing.
    class BaseParser
      # Parser unique id.
      # If parser class name follows a convention NameParser
      # then it's id will be automatically determined as it's
      # name in snake case plus suffix 's'
      #
      # Example:
      #    FlagParser.new.id                  # => :flags
      #    FileParser.new.id                  # => :files
      #    BaseArgumentParser.new.id          # => :base_arguments
      def id
        name = class_name.gsub(/parser$/i, '')
        name << 's' # make it plural
        to_underscore(name).to_sym
      end

      # Parses arguments and returns results of parsing.
      def parse(args)
      end

      private

      # Returns name of current class.
      def class_name
        self.class.name.split('::').last || ''
      end

      # Returns underscored version of string
      # (snake case format).
      def to_underscore(str)
       str.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raph-0.0.1 lib/raph/parser/base_parser.rb