Class: Snapi::Argument
- Inherits:
-
Object
- Object
- Snapi::Argument
- 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
-
#[](key) ⇒ Object
Allow the record to behave like a hash by giving access to @attributes via [] getter.
-
#[]=(key, value) ⇒ Object
Allow the record to behave like a hash by giving access to @attributes via []= setter.
-
#attributes ⇒ Object
Get the @attributes hash.
-
#default_value(val) ⇒ Object
DSL Setter Set a default value for the argument if one is not provided.
-
#format(format) ⇒ Object
DSL Setter Set a format the check string types against using the format_types outlined in Snapi::Validator.
-
#initialize ⇒ Argument
constructor
set for the argument record.
-
#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.
-
#required(bool) ⇒ Object
DSL Setter Is the argument a required?.
-
#type(type) ⇒ Object
DSL Setter What type of value is this argument.
-
#valid_attributes ⇒ Object
Whitelist of attribute names.
-
#valid_input?(input) ⇒ Boolean
Check if a value provided will suffice for the way this argument is defined.
-
#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.
Constructor Details
#initialize ⇒ Argument
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.
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 |
#attributes ⇒ Object
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
58 59 60 61 |
# File 'lib/snapi/argument.rb', line 58 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
68 69 70 71 |
# File 'lib/snapi/argument.rb', line 68 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
78 79 80 81 |
# File 'lib/snapi/argument.rb', line 78 def list(bool) raise InvalidBooleanError unless [true,false].include? bool @attributes[:list] = bool end |
#required(bool) ⇒ Object
DSL Setter Is the argument a required?
87 88 89 90 |
# File 'lib/snapi/argument.rb', line 87 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
99 100 101 102 103 |
# File 'lib/snapi/argument.rb', line 99 def type(type) valid_types = [:boolean, :enum, :string, :number, :timestamp] raise InvalidTypeError unless valid_types.include?(type) @attributes[:type] = type end |
#valid_attributes ⇒ Object
Whitelist of attribute names
50 51 52 |
# File 'lib/snapi/argument.rb', line 50 def valid_attributes [:default_value, :format, :list, :required, :type, :values] end |
#valid_input?(input) ⇒ Boolean
Check if a value provided will suffice for the way this argument is defined.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/snapi/argument.rb', line 125 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.
115 116 117 118 |
# File 'lib/snapi/argument.rb', line 115 def values(values) raise InvalidValuesError unless values.class == Array @attributes[:values] = values end |