Class: Snapi::Argument

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

Overview

Arguments are an intrinsic part of Capability and Function declaration in that Capabilities are made up of Functions and Functions may have one or more Arguments defined for them.

Arguments are a code representation of a meta-programming way of defining what arguments or paramater SHOULD or MUST be passed to a method which represents a function on the Capabilities library class

Instance Method Summary collapse

Constructor Details

#initializeArgument

set for the argument record. This initializer simply sets that value as



14
15
16
# File 'lib/snapi/argument.rb', line 14

def initialize
  @attributes = {}
end

Instance Method Details

#[](key) ⇒ Object

Allow the record to behave like a hash by giving access to @attributes via [] getter



22
23
24
# File 'lib/snapi/argument.rb', line 22

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object

Allow the record to behave like a hash by giving access to @attributes via []= setter

Validates the key requested to set is included in the valid_attributes white list and then uses the uses the various setter methods below to set the value.

Parameters:

  • key,

    attribute name

  • value,

    value to set

Raises:



35
36
37
38
# File 'lib/snapi/argument.rb', line 35

def []=(key, value)
  raise InvalidArgumentAttributeError unless valid_attributes.include?(key)
  send(key, value)
end

#attributesObject

Get the @attributes hash



43
44
45
# File 'lib/snapi/argument.rb', line 43

def attributes
  @attributes
end

#default_value(val) ⇒ Object

DSL Setter Set a default value for the argument if one is not provided

Parameters:

  • val,

    Value to use in case one isn’t provided,

Raises:



61
62
63
64
# File 'lib/snapi/argument.rb', line 61

def default_value(val)
  raise InvalidStringError unless val.class == String
  @attributes[:default_value] = val
end

#format(format) ⇒ Object

DSL Setter Set a format the check string types against using the format_types outlined in Snapi::Validator

Parameters:

  • format,

    Symbol of format to match against

Raises:



71
72
73
74
# File 'lib/snapi/argument.rb', line 71

def format(format)
  raise InvalidFormatError unless Validator.valid_regex_format?(format)
  @attributes[:format] = format
end

#list(bool) ⇒ Object

DSL Setter Is the argument a list of options? If true it will be assumed that this argument will be an array of objects of the sort set as type

Parameters:

  • bool,

    Boolean value

Raises:



81
82
83
84
# File 'lib/snapi/argument.rb', line 81

def list(bool)
  raise InvalidBooleanError unless [true,false].include? bool
  @attributes[:list] = bool
end

#required(bool) ⇒ Object

DSL Setter Is the argument a required?

Parameters:

  • bool,

    Boolean value

Raises:



90
91
92
93
# File 'lib/snapi/argument.rb', line 90

def required(bool)
  raise InvalidBooleanError unless [true,false].include? bool
  @attributes[:required] = bool
end

#type(type) ⇒ Object

DSL Setter What type of value is this argument. This will impact the way in which this argument value gets validated later on

Valid types are: :boolean, :enum, :string, :number, :timestamp

Parameters:

  • type,

    Symbol indicating type

Raises:



102
103
104
105
106
# File 'lib/snapi/argument.rb', line 102

def type(type)
  valid_types = [:boolean, :enum, :string, :number, :timestamp]
  raise InvalidTypeError unless valid_types.include?(type)
  @attributes[:type] = type
end

#valid_attributesObject

Whitelist of attribute names



50
51
52
53
54
55
# File 'lib/snapi/argument.rb', line 50

def valid_attributes
  [
    :default_value, :format, :list,
    :required, :type, :values, :name, :description
  ]
end

#valid_input?(input) ⇒ Boolean

Check if a value provided will suffice for the way this argument is defined.

Parameters:

  • input,

    Just about anything…

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/snapi/argument.rb', line 128

def valid_input?(input)
  case @attributes[:type]
  when :boolean
    [true,false].include?(input)
  when :enum
    raise MissingValuesError unless @attributes[:values]
    raise InvalidValuesError unless @attributes[:values].class == Array

    @attributes[:values].include?(input)
  when :string
    format = @attributes[:format] || :anything
    Validator.valid_input?(format, input)
  when :number
    [Integer, Fixnum].include?(input.class)
  when :timestamp
    # TODO timestamp pending
    raise PendingBranchError
  else
    false
  end
end

#values(values) ⇒ Object

DSL Setter What are the values that can be selected? This only applies to :enum typed arguments and allow the argument to define a list of valid values to select from for this.

In a form this would map to a select box. Alternative to using a :string argument with a format to validate against.

Basically creates a whitelist of values for this argument.

Parameters:

  • values,

    Array

Raises:



118
119
120
121
# File 'lib/snapi/argument.rb', line 118

def values(values)
  raise InvalidValuesError unless values.class == Array
  @attributes[:values] = values
end