Class: Magni

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = []) ⇒ Magni

Returns a new instance of Magni.



86
87
88
89
90
91
92
93
94
95
# File 'lib/magni.rb', line 86

def initialize(options=[])
  self.class.mappings ||= {}
  @options            ||= {}
  
  options = extract_keywords(options)
  options.each do |opt|
    flag, *values = opt.gsub(/[-]+/, "").split(/[=,]/)
    process(flag, values)
  end
end

Class Attribute Details

.keywordsObject

Returns the value of attribute keywords.



6
7
8
# File 'lib/magni.rb', line 6

def keywords
  @keywords
end

.mappingsObject

Returns the value of attribute mappings.



5
6
7
# File 'lib/magni.rb', line 5

def mappings
  @mappings
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



84
85
86
# File 'lib/magni.rb', line 84

def options
  @options
end

Class Method Details

.delegate_to(klass, method) ⇒ Object

Tells Magni where to send the processed flags and arguments.

# runner.rb
class Runner < Magni
  map "--help" => :boolean

  def start
    ...
  end
end

# bin_script.rb
Magni.delegate_to(Runner, :start)

Parameters

klass:  Your "runner" class
method: The method you want executed after flags are processed


73
74
75
# File 'lib/magni.rb', line 73

def delegate_to(klass, method)
  klass.new(ARGV.dup).send(method)
end

.map(mapping = {}) ⇒ Object

Maps a flag to a argument type. Flags are then put into the options instance variable. You can map two types:

Literal Double Dash Flags:

map "--maxerrors" => :integer

These can be one of four types: :integer, :string, :array and :boolean.

:integer #=> --count=2
:string  #=> --word=bird
:array   #=> --ravens=huginn,munnin
:boolean #=> --all

Special Keywords are used to pull out a non-flag argument. This is useful when the first or last argument needs to be the target of your script; a file for example.

Special keywords map to an instance variable on the instance your class. The following example will pull out the last argument from the list and put it in the @file instance variable. Currently there are only two keyword mappings, :first and last:

map :first => :foo
map :last  => :bar

Parameters

mapping: Hash[ Flag/Keyword => Type/iVar ]


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/magni.rb', line 41

def map(mapping={})
  @mappings ||= {}
  @keywords ||= {}
  
  mapping.each do |flag, type|
    if is_map_keyword?(flag)
      @keywords[flag] = type
    else
      @mappings[flag.gsub(/[-]+/, "")] = type
    end
  end
end

Instance Method Details

#extract_keywords(opts = []) ⇒ Object

Pulls the keyworded flags out of the ARGV array and puts them in the mapped instance variables.



100
101
102
103
104
105
106
# File 'lib/magni.rb', line 100

def extract_keywords(opts=[])
  self.class.keywords.each do |keyword, ivar|
    Magni.class_eval { attr_accessor ivar }
    instance_variable_set("@#{ ivar }", opts.delete(opts.send(keyword)))
  end
  opts
end

#process(flag, values) ⇒ Object

Pulls the type of argument flag is set to in mappings then sets the options key to value “coerced” to the matching argument type.



112
113
114
115
116
117
# File 'lib/magni.rb', line 112

def process(flag, values)
  type = self.class.mappings[flag]
  unless type.nil?
    @options[flag.to_sym] = send("to_#{ type }", values)
  end
end