Class: DSLCompose::DSL::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl_compose/dsl/arguments.rb,
lib/dsl_compose/dsl/arguments/argument.rb,
lib/dsl_compose/dsl/arguments/argument/interpreter.rb,
lib/dsl_compose/dsl/arguments/argument/in_validation.rb,
lib/dsl_compose/dsl/arguments/argument/is_a_validation.rb,
lib/dsl_compose/dsl/arguments/argument/format_validation.rb,
lib/dsl_compose/dsl/arguments/argument/length_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_in_validation.rb,
lib/dsl_compose/dsl/arguments/argument/end_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/equal_to_validation.rb,
lib/dsl_compose/dsl/arguments/argument/less_than_validation.rb,
lib/dsl_compose/dsl/arguments/argument/start_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/greater_than_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_end_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/not_start_with_validation.rb,
lib/dsl_compose/dsl/arguments/argument/less_than_or_equal_to_validation.rb,
lib/dsl_compose/dsl/arguments/argument/greater_than_or_equal_to_validation.rb

Defined Under Namespace

Classes: Argument, ArgumentAlreadyExistsError, ArgumentDoesNotExistError, ArgumentOrderingError, RequestedOptionalArgumentIsRequiredError, RequestedRequiredArgumentIsOptionalError

Instance Method Summary collapse

Constructor Details

#initializeArguments

Returns a new instance of Arguments.



21
22
23
# File 'lib/dsl_compose/dsl/arguments.rb', line 21

def initialize
  @arguments = {}
end

Instance Method Details

#add_argument(name, required, kwarg, type, array: false, default: nil, &block) ⇒ Object

Takes a method name, unique flag, required flag, and a block and creates a new Argument object.

Argument ‘name` must be unique within the DSLMethod. `required` must be a boolean, and determines if this argument will be required `array` is a boolean which determines if this argument will accept an array of the given type or a single item `default` contains the default value, if one is not provided then nil will be assumed (it is only available for optional arguments) `kwarg` is a boolean which determines if a required Attribute must be provided as a keyword argument. or optional on the method which is exposed in our DSL.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dsl_compose/dsl/arguments.rb', line 92

def add_argument name, required, kwarg, type, array: false, default: nil, &block
  if @arguments.key? name
    raise ArgumentAlreadyExistsError, "An argument with the name `#{name}` already exists for this DSL method"
  end

  # required arguments may not come after optional ones
  if required && optional_arguments.any?
    raise ArgumentOrderingError, "Required arguments can not be added after optional ones"
  end

  @arguments[name] = Argument.new(name, required, kwarg, type, array: array, default: default, &block)
end

#any?Boolean

returns true if there are any arguments, otherwise returns false

Returns:

  • (Boolean)


26
27
28
# File 'lib/dsl_compose/dsl/arguments.rb', line 26

def any?
  @arguments.values.any?
end

#argument(name) ⇒ Object

returns a specific Argument by it’s name, if the Argument does not exist, then an error is raised



47
48
49
50
51
52
53
# File 'lib/dsl_compose/dsl/arguments.rb', line 47

def argument name
  if has_argument? name
    @arguments[name]
  else
    raise ArgumentDoesNotExistError, "The argument `#{name}` does not exist for this DSLMethod"
  end
end

#argumentsObject

Returns an array of all this DSLMethods Argument objects.



31
32
33
# File 'lib/dsl_compose/dsl/arguments.rb', line 31

def arguments
  @arguments.values
end

#has_argument?(name) ⇒ Boolean

Returns ‘true` if an Argument with the provided name exists in this DSLMethod, otherwise it returns `false`.

Returns:

  • (Boolean)


79
80
81
# File 'lib/dsl_compose/dsl/arguments.rb', line 79

def has_argument? name
  @arguments.key? name
end

#optional_argument(name) ⇒ Object

returns a specific optional Argument by it’s name, if the Argument does not exist, or if it is required, then an error is raised



57
58
59
60
61
62
63
64
# File 'lib/dsl_compose/dsl/arguments.rb', line 57

def optional_argument name
  arg = argument name
  if arg.optional?
    @arguments[name]
  else
    raise RequestedOptionalArgumentIsRequiredError, "A specific argument `#{name}` which was expected to be optional was requested, but the argument found was flagged as required"
  end
end

#optional_argumentsObject

Returns an array of only the optional Argument objects on this DSLMethod.



36
37
38
# File 'lib/dsl_compose/dsl/arguments.rb', line 36

def optional_arguments
  arguments.filter(&:optional?)
end

#required_argument(name) ⇒ Object

returns a specific required Argument by it’s name, if the Argument does not exist, or if it is optional, then an error is raised



68
69
70
71
72
73
74
75
# File 'lib/dsl_compose/dsl/arguments.rb', line 68

def required_argument name
  arg = argument name
  if arg.required?
    @arguments[name]
  else
    raise RequestedRequiredArgumentIsOptionalError, "A specific argument `#{name}` which was expected to be required was requested, but the argument found was flagged as optional"
  end
end

#required_argumentsObject

Returns an array of only the required Argument objects on this DSLMethod.



41
42
43
# File 'lib/dsl_compose/dsl/arguments.rb', line 41

def required_arguments
  arguments.filter(&:required?)
end