Class: Choosy::DSL::ArgumentBuilder

Inherits:
Object
  • Object
show all
Includes:
BaseBuilder
Defined in:
lib/choosy/dsl/argument_builder.rb

Direct Known Subclasses

OptionBuilder

Instance Method Summary collapse

Methods included from BaseBuilder

#evaluate!, #method_missing

Constructor Details

#initializeArgumentBuilder

Returns a new instance of ArgumentBuilder.



5
6
7
# File 'lib/choosy/dsl/argument_builder.rb', line 5

def initialize
  @count_called = false
end

Dynamic Method Handling

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

Instance Method Details

#cast(ty) ⇒ Object



73
74
75
76
77
78
# File 'lib/choosy/dsl/argument_builder.rb', line 73

def cast(ty)
  entity.cast_to = Choosy::Converter.for(ty)
  if entity.cast_to.nil?
    raise Choosy::ConfigurationError.new("Unknown conversion cast: #{ty}")
  end
end

#count(restriction) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/choosy/dsl/argument_builder.rb', line 48

def count(restriction)
  @count_called = true
  if restriction.is_a?(Hash)
    lower_bound = restriction[:at_least] || restriction[:exactly] || 1
    upper_bound = restriction[:at_most] || restriction[:exactly] || 1000

    check_count(lower_bound)
    check_count(upper_bound)
    if lower_bound > upper_bound
      raise Choosy::ConfigurationError.new("The upper bound (#{upper_bound}) is less than the lower bound (#{lower_bound}).")
    end
    
    entity.arity = (lower_bound .. upper_bound)
  elsif restriction.is_a?(Range)
    entity.arity = restriction
  elsif restriction == :zero || restriction == :none
    entity.boolean!
  elsif restriction == :once
    entity.single!
  else
    check_count(restriction)
    entity.arity = (restriction .. restriction)
  end
end

#die(msg) ⇒ Object



84
85
86
# File 'lib/choosy/dsl/argument_builder.rb', line 84

def die(msg)
  raise Choosy::ValidationError.new("argument error: #{msg}")
end

#entityObject



9
10
11
# File 'lib/choosy/dsl/argument_builder.rb', line 9

def entity
  @entity ||= Choosy::Argument.new
end

#metaname(meta) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/choosy/dsl/argument_builder.rb', line 36

def metaname(meta)
  return if meta.nil?
  entity.metaname = meta
  return if @count_called
  
  if meta =~ /\+$/
    entity.multiple!
  else
    entity.single!
  end
end

#only(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/choosy/dsl/argument_builder.rb', line 21

def only(*args)
  if args.nil? || args.empty?
    raise Choosy::ConfigurationError.new("'only' requires at least one argument")
  end

  entity.allowable_values = args
  if args.length > 0 && entity.cast_to.nil?
    if args[0].is_a?(Symbol) 
      cast :symbol
    else
      cast :string
    end
  end
end

#required(value = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/choosy/dsl/argument_builder.rb', line 13

def required(value=nil)
  entity.required = if value.nil? || value == true
                      true
                    else 
                      false
                    end
end

#validate(&block) ⇒ Object



80
81
82
# File 'lib/choosy/dsl/argument_builder.rb', line 80

def validate(&block)
  entity.validation_step = block
end