Module: KwargTypes
- Included in:
- KwargDict
- Defined in:
- lib/kdict/kwargdict/kwargtypes.rb
Overview
KwargTypes is a collection of generic definitions that are called as parameters from KwargDict. Each definition or typedef validates a user's input against models built in the dictionary. Every typedef receives a user's argument and an associated struct, and checks to see if that argument passes the struct's unique conditions. All typedefs return Boolean. Some typedefs can receive a Proc that further modifies the struct's behavior by adding to (but never overridding) its conditions for truthiness.
Constant Summary collapse
- @@types =
Used internally. Gives typedefs access to a modified list of generic datatypes.
[Numeric, Float, Integer, Hash, Array, Bool, String, Symbol]
- @@typedefs =
Used by KwargDict to check against listed definitions before accepting a new KwargModel.
[:typeof, :arrayof, :anyof, :anyNof, :formof, :kwargsof, :adv_formof, :and_kwargsof]
Instance Method Summary collapse
-
#adv_formof(arg, struct) ⇒ Object
Your struct contains an array of exact length, each of whom's elements has its own subset of typedef, struct, and possibly prc.
-
#and_kwargsof(arg, struct) ⇒ Object
Your struct is structured like :adv_formof but the last element MUST be a :kwargsof typedef.
-
#anyNof(arg, struct, prc = nil) ⇒ Object
Your struct contains an element OR specific datatype in an array.
-
#anyof(arg, struct) ⇒ Boolean
Your struct contains a singular element that can be found in an array.
-
#arrayof(arg, struct, prc = nil) ⇒ Boolean
Your struct contains an array of any length but contain only one datatype.
-
#formof(arg, struct, prc = nil) ⇒ Object
Your struct contains an array of exact length, you expect each element to be contained in a specific order and be a generic datatype.
-
#kwargsof(arg, struct) ⇒ Object
Your struct contains a hash that corresponds to another KwargDict.
-
#typeof(arg, struct, prc = nil) ⇒ Boolean
Your struct contains a singular value of exactly one datatype.
Instance Method Details
#adv_formof(arg, struct) ⇒ Object
Your struct contains an array of exact length, each of whom's elements has its own subset of typedef, struct, and possibly prc
-
structs - Must be an array of exact length, each element of which is a sub array containing typedef, struct, and an optional prc, in that order. Can accept :typeof, :arrayof, :anyof, :anyNof, :formof, and :adv_formof.
-
Will NOT accept :kwargsof or :and_kwargsof.
-
Will return false if user input appears out of order with respect to the struct.
-
Will not accept a Proc outside of a sub typedef. Each element in struct may have its own unique Proc, however, if allowed for that typedef.
146 147 148 149 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 146 def adv_formof(arg, struct) return false if !arg.is_a?(Array) || arg.length != struct.length arg.zip(struct).all? { |a, s| valid?(a, *s) } end |
#and_kwargsof(arg, struct) ⇒ Object
Your struct is structured like :adv_formof but the last element MUST be a :kwargsof typedef
-
structs - MUST be an array in the form of :adv_formof, but the last sub typedef MUST be a kwargsof
-
Will return true if given a single value that returns true from the first sub typedef in the array.
-
Will return true if given an array of values in the order they appear in struct, without skipping one, and each returns true from its respective sub typedef
-
Will return true if the previous statement is true but the given array also includes a hash that corresponds to the last sub typedef in the struct, which again must always be :kwargsof.
-
Will return false if only a hash is present, even if hash elements are all valid to its nested KwargDict
-
Each sub typedef may receive a Proc if allowed for that typedef
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 175 def and_kwargsof(arg, struct) return false if arg.is_a?(Hash) # If only one arg of several are present and it's the first... if !arg.is_a?(Array) return valid?(arg, *struct[0]) end if !arg[-1].is_a?(Hash) return adv_formof(arg, struct[0...arg.length]) else return false if !adv_formof(arg[0...-1], struct[0...arg.length-1]) return valid?(arg[-1], *struct[-1]) end end |
#anyNof(arg, struct, prc = nil) ⇒ Object
Your struct contains an element OR specific datatype in an array.
-
structs - An array of any mixed things you'd like plus generic datatype keywords Numeric, Float, Integer, Bool, String, Symbol
-
Can accept a Proc that will only operate on a given value if it's identified by a generic datatype in struct after a check for explicit existence in the array
81 82 83 84 85 86 87 88 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 81 def anyNof(arg, struct, prc=nil) prc ||= Proc.new {true} return true if anyof(arg, struct) if struct.select { |ele| @@types.include?(ele) }.any? { |type| arg.is_a?(type) && prc.call(arg) } return true end false end |
#anyof(arg, struct) ⇒ Boolean
Your struct contains a singular element that can be found in an array.
-
structs - An array of any mix of things you'd like, EXCEPT other instances of KwargModels or KwargDicts.
-
Will not accept a Proc and must not contain generic datatype symbols.
63 64 65 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 63 def anyof(arg, struct) struct.include?(arg) end |
#arrayof(arg, struct, prc = nil) ⇒ Boolean
Your struct contains an array of any length but contain only one datatype.
-
structs - Again some of Ruby's built-in types plus Bool: Numeric, Float, Integer, Bool, String, Symbol
-
Multi-dimensional arrays will be treated as flattened and all values will still be checked against the given struct
-
Can accept a Proc that must return true for ALL values in the array.
48 49 50 51 52 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 48 def arrayof(arg, struct, prc=nil) return false if !arg.is_a?(Array) prc ||= Proc.new{true} arg.flatten.all? { |ele| ele.is_a?(struct) && prc.call(ele) } end |
#formof(arg, struct, prc = nil) ⇒ Object
Your struct contains an array of exact length, you expect each element to be contained in a specific order and be a generic datatype
-
structs - An array of set length containing generic datatype keywords Numeric, Float, Integer, Bool, String, Symbol, in a specific order. Will return false if user input appears out of order with respect to the struct.
-
Can accept a Proc that must return true for ALL values in the array.
104 105 106 107 108 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 104 def formof(arg, struct, prc=nil) prc ||= Proc.new {true} return false if !arg.is_a?(Array) arg.zip(struct).all? { |a,s| a.is_a?(s) && prc.call(a) } ? (return true) : (return false) end |
#kwargsof(arg, struct) ⇒ Object
Your struct contains a hash that corresponds to another KwargDict
-
**structs** - Can only be another KwargDict instance.
-
Will not accept a Proc
-
Input hash need not contain every kword from the nested KwargDict to return true
-
Will return true only if every key-arg pair in input hash is valid in nested KwargDict
123 124 125 126 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 123 def kwargsof(arg, struct) return false if !arg.keys.all? { |sub_kword| struct.has_key?(sub_kword) } arg.all? { |sub_word, sub_arg| valid?(sub_arg, *struct[sub_word].splay) } end |
#typeof(arg, struct, prc = nil) ⇒ Boolean
Your struct contains a singular value of exactly one datatype.
-
structs - Some of Ruby's built-in types plus Bool, an added datatype for true/false collectively. Acceptable struct arguments then, are: Numeric, Float, Integer, Bool, String, Symbol
-
Can accept a Proc that must also return true for successful validation
30 31 32 33 34 |
# File 'lib/kdict/kwargdict/kwargtypes.rb', line 30 def typeof(arg, struct, prc=nil) return false if [Hash, Array].any? { |type| arg.is_a?(type) } prc ||= Proc.new{true} arg.is_a?(struct) && prc.call(arg) end |