Class: Ropet::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ropet/parser.rb

Overview

Main class for Ropet, handles all injecting logic, parsing flags and setting options

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Parser) initialize(config = nil)

Initializes new parser Sets up @settings

Parameters:

  • Config (Config, nil)

    to use for the parser



16
17
18
19
20
21
# File 'lib/ropet/parser.rb', line 16

def initialize(config = nil)
  @settings = {}
  @config = config || Config.new
  
  return self
end

Instance Attribute Details

- (Config) config (readonly)

Returns Config

Returns:



11
12
13
# File 'lib/ropet/parser.rb', line 11

def config
  @config
end

- (Hash) settings (readonly)

Returns Assigned options

Returns:

  • (Hash)

    Assigned options



8
9
10
# File 'lib/ropet/parser.rb', line 8

def settings
  @settings
end

Class Method Details

+ (Parser) inject(*options)

Deprecated.

Creates new Parser with default Config. Calls Parser#inject on `*options`.

Parameters:

  • *options (<Symbol,String>, Hash)

    Options to parse

Returns:



63
64
65
66
# File 'lib/ropet/parser.rb', line 63

def self.inject(*options)
  obj = self.new(Config.new)
  obj.inject options
end

+ (Parser) let(&block)

Deprecated.

Creates new Parser with a block of dsl methods to pass for Config initialization

Parameters:

  • &block

    Block to pass

Returns:

  • (Parser)

    New parser created from initialized Config



28
29
30
# File 'lib/ropet/parser.rb', line 28

def self.let(&block)
  self.new(Config.from_dsl(&block))
end

Instance Method Details

- (Object) [](index)

Retrieves value for given `key`.

Parameters:

  • index (Symbol, String)

    Key to search for

Returns:

  • (Object)

    Value stored for given key



80
81
82
83
84
85
86
87
# File 'lib/ropet/parser.rb', line 80

def [](index)
  index = index.to_sym
  unless @settings[index] == nil
    @settings[index]
  else
    config.defaults[index]
  end
end

- (Boolean) has_key?(key)

Checks existence of `key` in defined settings or defaults.

Parameters:

  • key (String, Symbol)

    Key to search for

Returns:

  • (Boolean)

    Boolean telling if Parser has this key



72
73
74
75
# File 'lib/ropet/parser.rb', line 72

def has_key?(key)
  key = key.to_sym
  @settings.has_key?(key) or config.defaults.has_key?(key)
end

- (Parser) inject(*options)

Main method of Ropet. Parses options and flags and stores them properly.

Examples:

Injecting flags mixed with hash options

parser.inject :debug, toolchain: 'gcc-4.0'

Parameters:

  • *options (<Symbol,String>, Hash)

    Options to parse

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ropet/parser.rb', line 39

def inject(*options)
  options.each do |option|
    if option.is_a?(Symbol) || option.is_a?(String)
      flag = option.to_s
      prefix_range = 0...(config.false_prefix.length)
      if flag[prefix_range] == config.false_prefix
        flag.slice! prefix_range # remove prefix from string if it exists there
        @settings[flag.to_sym] = false
      else
        @settings[option] = true
      end
    elsif option.is_a? Hash
      @settings.merge! option
    end
  end

  return self
end