Class: KwargDict
- Inherits:
-
Hash
- Object
- Hash
- KwargDict
- Includes:
- KwargTypes
- Defined in:
- lib/kdict/kwargdict.rb
Overview
Creates a dictionary of generic, keyword-argument models. Allows user to validate a keyword and value against the models added to the dictionary.
Be sure to check out KwargTypes for a better understanding of what kind of models can be built and added to the dictionary.
Instance Method Summary collapse
-
#[]=(kword, type_struct_prc) ⇒ KwargModel.new(type, struct, prc)
Adds a new keyword-KwargModel to self.
-
#add(*kwargmodel_list) ⇒ KwargModel.new(type, struct, prc)
Can add multiple KwargModels in the form of arrays.
-
#check(kword, arg) ⇒ Boolean
Checks a kword and arg passed to it against the list of KwargModels added to self.
-
#initialize ⇒ KwargDict
constructor
A new instance of KwargDict.
Methods included from KwargTypes
#adv_formof, #and_kwargsof, #anyNof, #anyof, #arrayof, #formof, #kwargsof, #typeof
Constructor Details
#initialize ⇒ KwargDict
Returns a new instance of KwargDict
13 14 15 |
# File 'lib/kdict/kwargdict.rb', line 13 def initialize super end |
Instance Method Details
#[]=(kword, type_struct_prc) ⇒ KwargModel.new(type, struct, prc)
Adds a new keyword-KwargModel to self.
30 31 32 33 34 |
# File 'lib/kdict/kwargdict.rb', line 30 def []=(kword, type_struct_prc) type, struct, prc = type_struct_prc typedef?(type) super(kword, KwargModel.new(type, struct, prc)) end |
#add(*kwargmodel_list) ⇒ KwargModel.new(type, struct, prc)
Can add multiple KwargModels in the form of arrays. Remember that prc is optional.
44 45 46 47 48 |
# File 'lib/kdict/kwargdict.rb', line 44 def add(*kwargmodel_list) kwargmodel_list.each do |kword, type, struct, prc| self[kword] = type, struct, prc end end |
#check(kword, arg) ⇒ Boolean
Checks a kword and arg passed to it against the list of KwargModels added to self. Returns false if the kword doesn't exist or if the arg passed in fails validation. If the KwargModel has a Proc, the proc call will also have to evaluate to true and does not replace or override the struct.
64 65 66 67 |
# File 'lib/kdict/kwargdict.rb', line 64 def check(kword, arg) return false if !self.has_key?(kword) valid?(arg, *self[kword].splay) end |