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

#initialize(config = nil) ⇒ Parser

Initializes new parser Sets up @settings



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

#configConfig (readonly)



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

def config
  @config
end

#settingsHash (readonly)



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

def settings
  @settings
end

Class Method Details

.inject(*options) ⇒ Parser

Deprecated.

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



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

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

.let(&block) ⇒ Parser

Deprecated.

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



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

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

Instance Method Details

#[](index) ⇒ Object

Retrieves value 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

#has_key?(key) ⇒ Boolean

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



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

#inject(*options) ⇒ Parser

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'


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