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
62 63 64 65 |
# File 'lib/snapi/argument.rb', line 62 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
72 73 74 75 |
# File 'lib/snapi/argument.rb', line 72 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
82 83 84 85 86 |
# 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) ⇒ Object
DSL Setter Is the argument a required?
92 93 94 95 |
# File 'lib/snapi/argument.rb', line 92 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
104 105 106 107 108 |
# File 'lib/snapi/argument.rb', line 104 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 56 |
# 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/snapi/argument.rb', line 130 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) ⇒ 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.
120 121 122 123 |
# File 'lib/snapi/argument.rb', line 120 def values(values) raise InvalidValuesError unless values.class == Array @attributes[:values] = values end |