Class: Choosy::DSL::OptionBuilder

Inherits:
ArgumentBuilder show all
Defined in:
lib/choosy/dsl/option_builder.rb

Constant Summary collapse

HELP =
:__help__
VERSION =
:__version__

Instance Method Summary collapse

Methods inherited from ArgumentBuilder

#cast, #count, #metaname, #only, #required, #validate

Methods included from BaseBuilder

#evaluate!, #method_missing

Constructor Details

#initialize(name, &block) ⇒ OptionBuilder

Returns a new instance of OptionBuilder.



6
7
8
9
10
11
12
# File 'lib/choosy/dsl/option_builder.rb', line 6

def initialize(name, &block)
  super()
  @name = name
  if block_given?
    self.instance_eval(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Choosy::DSL::BaseBuilder

Instance Method Details

#default(value) ⇒ Object



38
39
40
# File 'lib/choosy/dsl/option_builder.rb', line 38

def default(value)
  entity.default_value = value
end

#depends_on(*args) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/choosy/dsl/option_builder.rb', line 42

def depends_on(*args)
  if args.length == 1 && args[0].is_a?(Array)
    entity.dependent_options = args[0]
  else
    entity.dependent_options = args
  end
end

#desc(description) ⇒ Object



34
35
36
# File 'lib/choosy/dsl/option_builder.rb', line 34

def desc(description)
  entity.description = description
end

#die(msg) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/choosy/dsl/option_builder.rb', line 55

def die(msg)
  flag_fmt = if entity.short_flag && entity.long_flag
               "#{entity.short_flag}/#{entity.long_flag}"
             end
  flag_fmt ||= entity.short_flag || entity.long_flag
  flag_meta = if entity.metaname
                 " #{entity.metaname}"
               end
  raise Choosy::ValidationError.new("#{flag_fmt}#{flag_meta}: #{msg}")
end

#entityObject



14
15
16
# File 'lib/choosy/dsl/option_builder.rb', line 14

def entity
  @entity ||= Choosy::Option.new(@name)
end

#flags(shorter, longer = nil, meta = nil) ⇒ Object



28
29
30
31
32
# File 'lib/choosy/dsl/option_builder.rb', line 28

def flags(shorter, longer=nil, meta=nil)
  short(shorter)
  long(longer) if longer
  metaname(meta) if meta
end

#from_hash(hash) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/choosy/dsl/option_builder.rb', line 66

def from_hash(hash)
  raise Choosy::ConfigurationError.new("Only hash arguments allowed") if !hash.is_a?(Hash)

  hash.each do |k, v|
    if respond_to?(k)
      if v.is_a?(Array)
        self.send(k, *v)
      else
        self.send(k, v)
      end
    else
      raise Choosy::ConfigurationError.new("Not a recognized option: #{k}")
    end
  end
end

#long(flag, meta = nil) ⇒ Object



23
24
25
26
# File 'lib/choosy/dsl/option_builder.rb', line 23

def long(flag, meta=nil)
  entity.long_flag = flag
  metaname(meta)
end

#negate(prefix = nil) ⇒ Object



50
51
52
53
# File 'lib/choosy/dsl/option_builder.rb', line 50

def negate(prefix=nil)
  prefix ||= 'no'
  entity.negation = prefix
end

#short(flag, meta = nil) ⇒ Object



18
19
20
21
# File 'lib/choosy/dsl/option_builder.rb', line 18

def short(flag, meta=nil)
  entity.short_flag = flag
  metaname(meta)
end