class Snapi::Argument

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

Public Class Methods

new() click to toggle source

@attributes tracks the various meta-attributes which can be set for the argument record. This initializer simply sets that value as

# File lib/snapi/argument.rb, line 14
def initialize
  @attributes = {}
end

Public Instance Methods

[](key) click to toggle source

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

@params key, key to query @attributes with

# File lib/snapi/argument.rb, line 22
def [](key)
  @attributes[key]
end
[]=(key, value) click to toggle source

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.

@param key, attribute name @param value, value to set

# File lib/snapi/argument.rb, line 35
def []=(key, value)
  raise InvalidArgumentAttributeError unless valid_attributes.include?(key)
  send(key, value)
end
attributes() click to toggle source

Get the @attributes hash

@returns Hash

# File lib/snapi/argument.rb, line 43
def attributes
  @attributes
end
default_value(val) click to toggle source

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

@param val, Value to use in case one isn't provided,

# File lib/snapi/argument.rb, line 62
def default_value(val)
  raise InvalidStringError unless val.class == String
  @attributes[:default_value] = val
end
description(desc) click to toggle source

DSL Setter Description of the argument

@param desc, String

# File lib/snapi/argument.rb, line 101
def description(desc)
  raise InvalidDescriptionError unless desc.class == String
  @attributes[:description] = desc
end
format(format) click to toggle source

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

@param format, Symbol of format to match against

# File lib/snapi/argument.rb, line 72
def format(format)
  raise InvalidFormatError unless Validator.valid_regex_format?(format)
  @attributes[:format] = format
end
list(bool) click to toggle source

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

@param bool, Boolean value

# File lib/snapi/argument.rb, line 82
def list(bool)
  #TODO work out kinks in list behavior...
  raise InvalidBooleanError unless [true,false].include? bool
  @attributes[:list] = bool
end
required(bool) click to toggle source

DSL Setter Is the argument a required?

@param bool, Boolean value

# File lib/snapi/argument.rb, line 92
def required(bool)
  raise InvalidBooleanError unless [true,false].include? bool
  @attributes[:required] = bool
end
type(type) click to toggle source

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

@param type, Symbol indicating type

# File lib/snapi/argument.rb, line 113
def type(type)
  valid_types = [:boolean, :enum, :string, :number, :timestamp]
  raise InvalidTypeError unless valid_types.include?(type)
  @attributes[:type] = type
end
valid_attributes() click to toggle source

Whitelist of attribute names

@returns Array of Symbols

# File lib/snapi/argument.rb, line 50
def valid_attributes
  [
    :default_value, :format, :list,
    :required, :type, :values, :name,
    :description
  ]
end
valid_input?(input) click to toggle source

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

@param input, Just about anything… @returns Boolean. true if valid

# File lib/snapi/argument.rb, line 139
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
    true
  else
    false
  end
end
values(values) click to toggle source

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.

@param values, Array

# File lib/snapi/argument.rb, line 129
def values(values)
  raise InvalidValuesError unless values.class == Array
  @attributes[:values] = values
end