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.



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

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



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

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/choosy/dsl/argument_builder.rb', line 53

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



89
90
91
# File 'lib/choosy/dsl/argument_builder.rb', line 89

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

#entityObject



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

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

#metaname(meta) ⇒ Object



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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/choosy/dsl/argument_builder.rb', line 26

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



18
19
20
21
22
23
24
# File 'lib/choosy/dsl/argument_builder.rb', line 18

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

#validate(&block) ⇒ Object



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

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