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
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
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
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?
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
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_attributes ⇒ Object
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.
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.
118 119 120 121 |
# File 'lib/snapi/argument.rb', line 118 def values(values) raise InvalidValuesError unless values.class == Array @attributes[:values] = values end |