kdict 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/.yardopts +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +301 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docs/Between.html +302 -0
- data/docs/Bool.html +121 -0
- data/docs/Example1.md +195 -0
- data/docs/FalseClass.html +143 -0
- data/docs/KDict/Error.html +135 -0
- data/docs/KDict.html +128 -0
- data/docs/Kdict/Error.html +135 -0
- data/docs/Kdict.html +157 -0
- data/docs/Kdict_.html +143 -0
- data/docs/KwargDict.html +697 -0
- data/docs/KwargModel.html +638 -0
- data/docs/KwargTypes.html +988 -0
- data/docs/Numeric.html +148 -0
- data/docs/TrueClass.html +143 -0
- data/docs/_index.html +184 -0
- data/docs/class_list.html +51 -0
- data/docs/css/common.css +1 -0
- data/docs/css/full_list.css +58 -0
- data/docs/css/style.css +496 -0
- data/docs/file.Example1.html +240 -0
- data/docs/file.README.html +370 -0
- data/docs/file_list.html +61 -0
- data/docs/frames.html +17 -0
- data/docs/index.html +370 -0
- data/docs/js/app.js +303 -0
- data/docs/js/full_list.js +216 -0
- data/docs/js/jquery.js +4 -0
- data/docs/method_list.html +187 -0
- data/docs/top-level-namespace.html +112 -0
- data/examples/example1.rb +90 -0
- data/kdict.gemspec +39 -0
- data/lib/kdict/kwargdict/kwargtypes/bool.rb +9 -0
- data/lib/kdict/kwargdict/kwargtypes.rb +196 -0
- data/lib/kdict/kwargdict.rb +75 -0
- data/lib/kdict/kwargmodel.rb +40 -0
- data/lib/kdict/version.rb +5 -0
- data/lib/kdict.rb +19 -0
- data/ndoc/checksums +6 -0
- data/ndoc/complete +0 -0
- data/ndoc/object_types +0 -0
- data/ndoc/objects/root.dat +0 -0
- data/ndoc/proxy_types +0 -0
- metadata +150 -0
data/kdict.gemspec
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "kdict/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "kdict"
|
|
8
|
+
spec.version = Kdict::VERSION
|
|
9
|
+
spec.authors = ["Steven K Terry"]
|
|
10
|
+
spec.email = ["stkterry@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{KDict allows you to quickly create powerful Keyword-Argument Dictionaries to automate input validation.}
|
|
13
|
+
spec.description = %q{KDict allows you to quickly create powerful Keyword-Argument Dictionaries. Each
|
|
14
|
+
entry can be used to validate a user's input against it, and is built from a
|
|
15
|
+
generic type defintion (*typedef*) and a unique structure (*struct*).
|
|
16
|
+
|
|
17
|
+
With the included *typedefs* users can create simple to complex validaters in
|
|
18
|
+
just a single line of code.
|
|
19
|
+
|
|
20
|
+
Examples avaible here in the README don't offer up the full scope of usefulness,
|
|
21
|
+
so take the time to look at the example documentation and perhaps run a few of
|
|
22
|
+
them yourself.}
|
|
23
|
+
#spec.homepage = "https://github.com/stkterry/KDict"
|
|
24
|
+
|
|
25
|
+
spec.license = "MIT"
|
|
26
|
+
|
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
31
|
+
end
|
|
32
|
+
spec.bindir = "exe"
|
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
34
|
+
spec.require_paths = ["lib"]
|
|
35
|
+
|
|
36
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
39
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# A useful collection of monkey-wrenching that makes creating KwargTypes and
|
|
2
|
+
# KwargModels a little simpler.
|
|
3
|
+
|
|
4
|
+
# Empty module that's assigned to both {TrueClass} and {FalseClass}.
|
|
5
|
+
module Bool; end
|
|
6
|
+
# Adds the {Bool} module to {TrueClass}.
|
|
7
|
+
class TrueClass; include Bool; end
|
|
8
|
+
# Adds the {Bool} module to {FalseClass}.
|
|
9
|
+
class FalseClass; include Bool; end
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# KwargTypes is a collection of generic definitions that are called as parameters
|
|
2
|
+
# from {KwargDict}. Each definition or *typedef* validates a user's input against
|
|
3
|
+
# models built in the dictionary. Every *typedef* receives a user's argument and
|
|
4
|
+
# an associated *struct*, and checks to see if that argument passes the *struct*'s
|
|
5
|
+
# unique conditions. All *typedefs* return Boolean. Some *typedefs* can receive
|
|
6
|
+
# a Proc that further modifies the *struct*'s behavior by adding to (but never overridding)
|
|
7
|
+
# its conditions for truthiness.
|
|
8
|
+
#
|
|
9
|
+
module KwargTypes
|
|
10
|
+
|
|
11
|
+
# Used internally. Gives *typedefs* access to a modified list of generic datatypes.
|
|
12
|
+
@@types = [Numeric, Float, Integer, Hash, Array, Bool, String, Symbol]
|
|
13
|
+
|
|
14
|
+
# Used by KwargDict to check against listed definitions before accepting
|
|
15
|
+
# a new KwargModel.
|
|
16
|
+
@@typedefs = [:typeof, :arrayof, :anyof, :anyNof, :formof, :kwargsof,
|
|
17
|
+
:adv_formof, :and_kwargsof]
|
|
18
|
+
|
|
19
|
+
# ====Your *struct* contains a singular value of exactly one datatype.
|
|
20
|
+
#
|
|
21
|
+
# * *structs* - Some of Ruby's built-in types plus *Bool*, an added datatype for
|
|
22
|
+
# true/false collectively. Acceptable *struct* arguments then, are: *Numeric*, *Float*, *Integer*, *Bool*, *String*, *Symbol*
|
|
23
|
+
# * Can accept a Proc that must also return true for successful validation
|
|
24
|
+
# @return [Boolean]
|
|
25
|
+
# @example (Called From a KwargDict instance)
|
|
26
|
+
# my_dict[:example] = :typeof, Bool
|
|
27
|
+
# my_dict[:proc_example] = :typeof, Float, Proc.new { |n| n > 5.3 }
|
|
28
|
+
# my_dict.check(:example, false) # => true
|
|
29
|
+
# my_dict.check(:proc_example, 2.1) # => false
|
|
30
|
+
def typeof(arg, struct, prc=nil)
|
|
31
|
+
return false if [Hash, Array].any? { |type| arg.is_a?(type) }
|
|
32
|
+
prc ||= Proc.new{true}
|
|
33
|
+
arg.is_a?(struct) && prc.call(arg)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# ====Your *struct* contains an array of any length but contain only one datatype.
|
|
37
|
+
#
|
|
38
|
+
# * *structs* - Again some of Ruby's built-in types plus Bool: *Numeric*, *Float*, *Integer*, *Bool*, *String*, *Symbol*
|
|
39
|
+
# * Multi-dimensional arrays will be treated as flattened and all values will still
|
|
40
|
+
# be checked against the given *struct*
|
|
41
|
+
# * Can accept a Proc that must return true for ALL values in the array.
|
|
42
|
+
# @return [Boolean]
|
|
43
|
+
# @example (Called From a KwargDict instance)
|
|
44
|
+
# my_dict[:example] = :arrayof, Strings
|
|
45
|
+
# my_dict[:proc_example] = :arrayof, Strings, Proc.new { |s| s.length > 2 }
|
|
46
|
+
# my_dict.check(:example, ['This', 'will', 'return', 'true']) # => true
|
|
47
|
+
# my_dict.check(:proc_example, ['This', 'is', 'still', 'false']) # => false
|
|
48
|
+
def arrayof(arg, struct, prc=nil)
|
|
49
|
+
return false if !arg.is_a?(Array)
|
|
50
|
+
prc ||= Proc.new{true}
|
|
51
|
+
arg.flatten.all? { |ele| ele.is_a?(struct) && prc.call(ele) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# ====Your *struct* contains a singular element that can be found in an array.
|
|
55
|
+
#
|
|
56
|
+
# * *structs* - An array of any mix of things you'd like, EXCEPT other instances of KwargModels or KwargDicts.
|
|
57
|
+
# * Will not accept a Proc and must not contain generic datatype symbols.
|
|
58
|
+
# @return [Boolean]
|
|
59
|
+
# @example (Called From a KwargDict instance)
|
|
60
|
+
# my_dict[:example] = :anyof, ['xx-small', 'x-small', 'large', 3.2]
|
|
61
|
+
# my_dict.check(:example, 3.2) # => true
|
|
62
|
+
# my_dict.check(:example, 'xx-large') # => false
|
|
63
|
+
def anyof(arg, struct)
|
|
64
|
+
struct.include?(arg)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# ====Your *struct* contains an element OR specific datatype in an array.
|
|
68
|
+
#
|
|
69
|
+
# * *structs* - An array of any mixed things you'd like plus generic datatype
|
|
70
|
+
# keywords *Numeric*, *Float*, *Integer*, *Bool*, *String*, *Symbol*
|
|
71
|
+
# * Can accept a Proc that will only operate on a given value if it's identified
|
|
72
|
+
# by a generic datatype in *struct* after a check for explicit existence in the array
|
|
73
|
+
# @example (Called From a KwargDict instance)
|
|
74
|
+
# my_dict[:example] = :anyNof, [Integer, 'red', 'blue', 'magenta']
|
|
75
|
+
# my_dict[:proc_example] = :anyNof, [Float, 'green', 3.1], Proc.new { |n| n > 5 }
|
|
76
|
+
# my_dict.check(:example, 'red') # => true
|
|
77
|
+
# my_dict.check(:example, 34) # => true
|
|
78
|
+
# my_dict.check(:proc_example, 'green') # => true
|
|
79
|
+
# my_dict.check(:proc_example, 6.1) # => true
|
|
80
|
+
# my_dict.check(:proc_example, 3.1) # => true
|
|
81
|
+
def anyNof(arg, struct, prc=nil)
|
|
82
|
+
prc ||= Proc.new {true}
|
|
83
|
+
return true if anyof(arg, struct)
|
|
84
|
+
if struct.select { |ele| @@types.include?(ele) }.any? { |type| arg.is_a?(type) && prc.call(arg) }
|
|
85
|
+
return true
|
|
86
|
+
end
|
|
87
|
+
false
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# ====Your *struct* contains an array of exact length, you expect each element to be contained in a specific order and be a generic datatype
|
|
91
|
+
#
|
|
92
|
+
# * *structs* - An array of set length containing generic datatype keywords *Numeric*,
|
|
93
|
+
# *Float*, *Integer*, *Bool*, *String*, *Symbol*, in a specific order. Will
|
|
94
|
+
# return false if user input appears out of order with respect to the *struct*.
|
|
95
|
+
# * Can accept a Proc that must return true for ALL values in the array.
|
|
96
|
+
# @example (Called From a KwargDict instance)
|
|
97
|
+
# my_dict[:example] = :formof, [Bool, Float, String]
|
|
98
|
+
# my_dict[:proc_example] = :formof, [Integer, Float, Float], Proc.new { |n| n.between?(1, 3) }
|
|
99
|
+
# my_dict.check(:example, [false, 3.1, "Holly Dolly"]) # => true
|
|
100
|
+
# my_dict.check(:example, [false, 3.1]) # => false
|
|
101
|
+
# my_dict.check(:example, ["Holly Dolly", 3.1, false]) # => false
|
|
102
|
+
# my_dict.check(:proc_example, [2, 1.1, 1.2]) # => true
|
|
103
|
+
# my_dict.check(:proc_example, [1.1, 2, 1.4]) # => false
|
|
104
|
+
def formof(arg, struct, prc=nil)
|
|
105
|
+
prc ||= Proc.new {true}
|
|
106
|
+
return false if !arg.is_a?(Array)
|
|
107
|
+
arg.zip(struct).all? { |a,s| a.is_a?(s) && prc.call(a) } ? (return true) : (return false)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# ====Your *struct* contains a hash that corresponds to another KwargDict
|
|
111
|
+
#
|
|
112
|
+
# * ***structs*** - Can only be another KwargDict instance.
|
|
113
|
+
# * Will not accept a Proc
|
|
114
|
+
# * Input hash need not contain every *kword* from the nested KwargDict to return true
|
|
115
|
+
# * Will return true only if every key-arg pair in input hash is valid in nested KwargDict
|
|
116
|
+
# @example (Called From a KwargDict instance)
|
|
117
|
+
# another_dict.add([:one, :typeof, Float], [:two, :arrayof, String], [:three, :typeof, Bool],
|
|
118
|
+
# [:and, :anyof, ['partridge', 'in', 'a', 'pear', 'tree']])
|
|
119
|
+
# my_dict[:example] = :kwargsof, another_dict
|
|
120
|
+
# my_dict.check(:example, {one:3.1, three:false, and:'in'}) # => true
|
|
121
|
+
# my_dict.check(:example, {two:['this', 'works'], four:'without this'}) # => false
|
|
122
|
+
# my_dict.check(:example, {two:['see?']}) # => true
|
|
123
|
+
def kwargsof(arg, struct)
|
|
124
|
+
return false if !arg.keys.all? { |sub_kword| struct.has_key?(sub_kword) }
|
|
125
|
+
arg.all? { |sub_word, sub_arg| valid?(sub_arg, *struct[sub_word].splay) }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# ====Your *struct* contains an array of exact length, each of whom's elements has its own subset of *typedef*, *struct*, and possibly *prc*
|
|
129
|
+
#
|
|
130
|
+
# * *structs* - Must be an array of exact length, each element of which is a sub array
|
|
131
|
+
# containing *typedef*, *struct*, and an optional *prc*, in that order. Can accept
|
|
132
|
+
# *:typeof*, *:arrayof*, *:anyof*, *:anyNof*, *:formof*, and *:adv_formof*.
|
|
133
|
+
# * Will NOT accept *:kwargsof* or *:and_kwargsof*.
|
|
134
|
+
# * Will return false if user input appears out of order with respect to the *struct*.
|
|
135
|
+
# * Will not accept a Proc outside of a sub *typedef*. Each element in *struct* may
|
|
136
|
+
# have its own unique Proc, however, if allowed for that *typedef*.
|
|
137
|
+
# @example (Called From a KwargDict instance)
|
|
138
|
+
# my_dict[:example] = :adv_formof, [[:typeof, Float], [:arrayof, Integer]]
|
|
139
|
+
# my_dict[:proc_example] = :adv_formof, [[:typeof, String, Proc.new { |s| s.length > 5 }],
|
|
140
|
+
# [:formof, [Float]*3 , Proc.new { |n| n.ibetween?(0,1) }]]
|
|
141
|
+
# my_dict.check(:example, [3.1, [1, 2, 3, 4, 5]]) # => true
|
|
142
|
+
# my_dict.check(:example, [3.1, ['1', '2', '3']]) # => false
|
|
143
|
+
# my_dict.check(:proc_example, ["Longer", [0.25, 0.5, 1.0]]) # => true
|
|
144
|
+
# my_dict.check(:proc_example, ["Longer", [-0.25, 0.5, -1.0]]) # => false
|
|
145
|
+
# my_dict.check(:proc_example, ["Short", [0.25, 0.5, 1.0]]) # => false
|
|
146
|
+
def adv_formof(arg, struct)
|
|
147
|
+
return false if !arg.is_a?(Array) || arg.length != struct.length
|
|
148
|
+
arg.zip(struct).all? { |a, s| valid?(a, *s) }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# ====Your *struct* is structured like *:adv_formof* but the last element MUST be a *:kwargsof* *typedef*
|
|
152
|
+
#
|
|
153
|
+
# * *structs* - MUST be an array in the form of *:adv_formof*, but the last sub
|
|
154
|
+
# *typedef* MUST be a *kwargsof*
|
|
155
|
+
# * Will return true if given a single value that returns true from the first sub *typedef* in the array.
|
|
156
|
+
# * Will return true if given an array of values in the order they appear in *struct*,
|
|
157
|
+
# without skipping one, and each returns true from its respective sub *typedef*
|
|
158
|
+
# * Will return true if the previous statement is true but the given array also
|
|
159
|
+
# includes a hash that corresponds to the last sub *typedef* in the *struct*,
|
|
160
|
+
# which again must always be *:kwargsof*.
|
|
161
|
+
# * Will return false if only a hash is present, even if hash elements are
|
|
162
|
+
# all valid to its nested KwargDict
|
|
163
|
+
# * Each sub *typedef* may receive a Proc if allowed for that *typedef*
|
|
164
|
+
# @example (Called From a KwargDict instance)
|
|
165
|
+
# another_dict.add([:one, :typeof, Float], [:two, :arrayof, String])
|
|
166
|
+
# my_dict[:example] = :and_kwargsof, [[:typeof, String], [:typeof, Bool], [:kwargsof, another_dict]]
|
|
167
|
+
# my_dict.check(:example, 'It works!') # => true
|
|
168
|
+
# my_dict.check(:example, ['And this.']) # => true
|
|
169
|
+
# my_dict.check(:example, ['This too.', true]) # => true
|
|
170
|
+
# my_dict.check(:example, ['As does this.', false, {two:['Super!']}]) # => true
|
|
171
|
+
# my_dict.check(:example, ['Even this works.', {two:['Yep.'], one:3.1}]) # => true
|
|
172
|
+
# my_dict.check(:example, true) # => false | Skips the first element of :example
|
|
173
|
+
# my_dict.check(:example, [true, 'It broke!']) # => false | Elements appear out of order
|
|
174
|
+
# my_dict.check(:example, {one:'Nope'}) # => false
|
|
175
|
+
def and_kwargsof(arg, struct)
|
|
176
|
+
return false if arg.is_a?(Hash)
|
|
177
|
+
|
|
178
|
+
# If only one arg of several are present and it's the first...
|
|
179
|
+
if !arg.is_a?(Array)
|
|
180
|
+
return valid?(arg, *struct[0])
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
if !arg[-1].is_a?(Hash)
|
|
184
|
+
return adv_formof(arg, struct[0...arg.length])
|
|
185
|
+
else
|
|
186
|
+
return false if !adv_formof(arg[0...-1], struct[0...arg.length-1])
|
|
187
|
+
return valid?(arg[-1], *struct[-1])
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
private def valid?(arg, type, struct, prc=nil)
|
|
193
|
+
prc ? (self.send type, arg, struct, prc) : (self.send type, arg, struct)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Creates a dictionary of generic, keyword-argument models. Allows user to
|
|
2
|
+
# validate a keyword and value against the models added to the dictionary.
|
|
3
|
+
#
|
|
4
|
+
# Be sure to check out {KwargTypes} for a better understanding of what kind of
|
|
5
|
+
# models can be built and added to the dictionary.
|
|
6
|
+
|
|
7
|
+
class KwargDict < Hash
|
|
8
|
+
|
|
9
|
+
include KwargTypes
|
|
10
|
+
|
|
11
|
+
# @example Instantiating
|
|
12
|
+
# my_dict = KwargDict.new
|
|
13
|
+
def initialize
|
|
14
|
+
super
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @see KwargModel
|
|
18
|
+
# Adds a new keyword-KwargModel to self.
|
|
19
|
+
# @param kword The name of the kword.
|
|
20
|
+
# @param type_struct_prc Collectively the *typedef*, *struct*, and optional *prc*.
|
|
21
|
+
# @param type AKA the *typedef*, a symbolic form of anyone of the included methods from {KwargTypes}
|
|
22
|
+
# @param struct The *typedef* format is dependent upon the model's *typedef*. Be sure to refer to those {KwargTypes} to set *structs* appropriately.
|
|
23
|
+
# @param prc Optional. A Proc that's meant to add to the specificity of *struct*. Not all *typedefs* accept Procs.
|
|
24
|
+
# @return {KwargModel.new(type, struct, prc)}
|
|
25
|
+
# @example Adding a KwargModel to the KwargDict instance:
|
|
26
|
+
# my_dict[:example] = :arrayof, Integer
|
|
27
|
+
# my_dict[:proc_example] = :formof, [Float, Float], Proc.new { |n| n > 2.5 }
|
|
28
|
+
# p my_dict[:example].splay #=> [:arrayof, Integer]
|
|
29
|
+
# p my_dict[:proc_example].splay #=> [:formof, [Float, Float], <Proc:0x000055b98848a6d8@kwargdict.rb:20>]
|
|
30
|
+
def []=(kword, type_struct_prc)
|
|
31
|
+
type, struct, prc = type_struct_prc
|
|
32
|
+
typedef?(type)
|
|
33
|
+
super(kword, KwargModel.new(type, struct, prc))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Can add multiple KwargModels in the form of arrays. Remember that *prc* is optional.
|
|
37
|
+
# @param kwargmodel_list A collection of arrays each in the form [*kword*, *typedef*, *struct*, *prc*].
|
|
38
|
+
# @return {KwargModel.new(type, struct, prc)}
|
|
39
|
+
# @see #[]=
|
|
40
|
+
# @example Adding a KwargModel:
|
|
41
|
+
# my_dict.add([:one, :typeof, String], [:two, :anyof, ['this', 'that']])
|
|
42
|
+
# p my_dict[:one].splay #=> [:typeof, String]
|
|
43
|
+
# p my_dict[:two].splay #=> [:anyof, ['this', 'that']]
|
|
44
|
+
def add(*kwargmodel_list)
|
|
45
|
+
kwargmodel_list.each do |kword, type, struct, prc|
|
|
46
|
+
self[kword] = type, struct, prc
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Checks a kword and arg passed to it against the list of KwargModels added to
|
|
51
|
+
# self. Returns false if the kword doesn't exist or if the arg passed in fails
|
|
52
|
+
# validation. If the KwargModel has a Proc, the proc call will also have to
|
|
53
|
+
# evaluate to true and does not replace or override the *struct*.
|
|
54
|
+
# @param kword The keyword whose *struct* you wish to check arg against.
|
|
55
|
+
# @param arg The argument, expression, etc., that you wish check against the *struct*.
|
|
56
|
+
# @return [Boolean]
|
|
57
|
+
# @example Adding a KwargModel and Checking User Input Against It:
|
|
58
|
+
# my_dict[:label] = :typeof, String
|
|
59
|
+
# my_dict[:rgba] = :formof, [Numeric]*4, Proc.new { |n| n.ibetween(0, 1) }
|
|
60
|
+
# my_dict.check(:label, 'Labels Are Fun!') #=> true
|
|
61
|
+
# my_dict.check(:label, 45) #=> false
|
|
62
|
+
# my_dict.check(:rgba, [0.5, 1, 0.25, 1]) #=> true
|
|
63
|
+
# my_dict.check(:rgba, [0.5, 1, 0.25, 11.5]) #=> false
|
|
64
|
+
def check(kword, arg)
|
|
65
|
+
return false if !self.has_key?(kword)
|
|
66
|
+
valid?(arg, *self[kword].splay)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private def typedef?(type)
|
|
70
|
+
if !@@typedefs.include?(type)
|
|
71
|
+
raise "type must be a type in KwargTypes"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# A convenient class for storing a unique Keyword-Argument Model
|
|
2
|
+
|
|
3
|
+
class KwargModel
|
|
4
|
+
|
|
5
|
+
attr_reader :type, :struct, :prc
|
|
6
|
+
|
|
7
|
+
#@!attribute [r] type
|
|
8
|
+
# @return [typedef]
|
|
9
|
+
# Returns the typdef of the Model.
|
|
10
|
+
|
|
11
|
+
#@!attribute [r] struct
|
|
12
|
+
# Returns the structure of the typedef.
|
|
13
|
+
|
|
14
|
+
#@!attribute [r] prc
|
|
15
|
+
# @return [Proc]
|
|
16
|
+
# Returns a unique Proc if it exists, otherwise nil.
|
|
17
|
+
|
|
18
|
+
# @param type [Symbol] the typedef of the instance. It can be any one of the {KwargTypes typedefs}
|
|
19
|
+
# @param struct the structure of the typedef.
|
|
20
|
+
# @param prc an optional Proc that modifies the 'specificity' of struct
|
|
21
|
+
def initialize(type, struct, prc=nil)
|
|
22
|
+
@type = type
|
|
23
|
+
@struct = struct
|
|
24
|
+
@prc = prc if prc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Returns the attributes of the instance as an array.
|
|
28
|
+
# @return [[type, struct, prc]] if prc exists.
|
|
29
|
+
# @return [[type, struct]] if prc is nil.
|
|
30
|
+
#
|
|
31
|
+
#@example
|
|
32
|
+
# km1 = KwargModel.new(:typeof, Float)
|
|
33
|
+
# km2 = KwargModel.new(:typeof, Float, Proc.new{true})
|
|
34
|
+
# km1.splay #=> [:typeof, Float]
|
|
35
|
+
# km2.splay #=> [:typeof, Float, @prc=#<Proc:0x000055c85d7671b0@kwargmodel.rb:20>]
|
|
36
|
+
def splay
|
|
37
|
+
@prc ? [@type, @struct, @prc] : [@type, @struct]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
data/lib/kdict.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require "kdict/version"
|
|
2
|
+
|
|
3
|
+
# KDict allows you to quickly create powerful Keyword-Argument Dictionaries. Each
|
|
4
|
+
# entry (a KwargModel) can be used to validate a user's input against it,
|
|
5
|
+
# and is built from a generic type defintion (*typedef*) and a unique structure
|
|
6
|
+
# (*struct*).
|
|
7
|
+
#
|
|
8
|
+
# With the included *typedefs* users can create simple to complex validaters in
|
|
9
|
+
# just a single line of code. Be sure to check out {KwargDict} and {KwargTypes}
|
|
10
|
+
# for a detailed look at the underlying code and how it might be used.
|
|
11
|
+
# Especially be sure to check out the {file:README.md} more than anything else!
|
|
12
|
+
module KDict
|
|
13
|
+
# Inheriting SandardError.
|
|
14
|
+
class Error < StandardError; end
|
|
15
|
+
require 'kdict/kwargdict/kwargtypes/bool.rb'
|
|
16
|
+
require 'kdict/kwargdict/kwargtypes.rb'
|
|
17
|
+
require 'kdict/kwargdict.rb'
|
|
18
|
+
require 'kdict/kwargmodel.rb'
|
|
19
|
+
end
|
data/ndoc/checksums
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
lib/kdict.rb 1c241de8a71a827bcf892d1f636e7d953e89eaee
|
|
2
|
+
lib/kdict/version.rb 0d5a275d2281605bbdebbe20851a80f17a145758
|
|
3
|
+
lib/kdict/kwargdict.rb e3c64767b5243437a06d778c9ce336873847f8eb
|
|
4
|
+
lib/kdict/kwargmodel.rb 4896ba2ba6ecf259e293a782ffb809f8d54fc747
|
|
5
|
+
lib/kdict/kwargdict/kwargtypes.rb 22ed645961099b24d2e943361e2fb2a321706713
|
|
6
|
+
lib/kdict/kwargdict/kwargtypes/bool.rb 94eb3a80af453e3b07e23d66afb55cc872009657
|
data/ndoc/complete
ADDED
|
File without changes
|
data/ndoc/object_types
ADDED
|
Binary file
|
|
Binary file
|
data/ndoc/proxy_types
ADDED
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kdict
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Steven K Terry
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-04-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: |-
|
|
56
|
+
KDict allows you to quickly create powerful Keyword-Argument Dictionaries. Each
|
|
57
|
+
entry can be used to validate a user's input against it, and is built from a
|
|
58
|
+
generic type defintion (*typedef*) and a unique structure (*struct*).
|
|
59
|
+
|
|
60
|
+
With the included *typedefs* users can create simple to complex validaters in
|
|
61
|
+
just a single line of code.
|
|
62
|
+
|
|
63
|
+
Examples avaible here in the README don't offer up the full scope of usefulness,
|
|
64
|
+
so take the time to look at the example documentation and perhaps run a few of
|
|
65
|
+
them yourself.
|
|
66
|
+
email:
|
|
67
|
+
- stkterry@gmail.com
|
|
68
|
+
executables: []
|
|
69
|
+
extensions: []
|
|
70
|
+
extra_rdoc_files: []
|
|
71
|
+
files:
|
|
72
|
+
- ".gitignore"
|
|
73
|
+
- ".rspec"
|
|
74
|
+
- ".travis.yml"
|
|
75
|
+
- ".yardopts"
|
|
76
|
+
- CODE_OF_CONDUCT.md
|
|
77
|
+
- Gemfile
|
|
78
|
+
- Gemfile.lock
|
|
79
|
+
- LICENSE.txt
|
|
80
|
+
- README.md
|
|
81
|
+
- Rakefile
|
|
82
|
+
- bin/console
|
|
83
|
+
- bin/setup
|
|
84
|
+
- docs/Between.html
|
|
85
|
+
- docs/Bool.html
|
|
86
|
+
- docs/Example1.md
|
|
87
|
+
- docs/FalseClass.html
|
|
88
|
+
- docs/KDict.html
|
|
89
|
+
- docs/KDict/Error.html
|
|
90
|
+
- docs/Kdict.html
|
|
91
|
+
- docs/Kdict/Error.html
|
|
92
|
+
- docs/Kdict_.html
|
|
93
|
+
- docs/KwargDict.html
|
|
94
|
+
- docs/KwargModel.html
|
|
95
|
+
- docs/KwargTypes.html
|
|
96
|
+
- docs/Numeric.html
|
|
97
|
+
- docs/TrueClass.html
|
|
98
|
+
- docs/_index.html
|
|
99
|
+
- docs/class_list.html
|
|
100
|
+
- docs/css/common.css
|
|
101
|
+
- docs/css/full_list.css
|
|
102
|
+
- docs/css/style.css
|
|
103
|
+
- docs/file.Example1.html
|
|
104
|
+
- docs/file.README.html
|
|
105
|
+
- docs/file_list.html
|
|
106
|
+
- docs/frames.html
|
|
107
|
+
- docs/index.html
|
|
108
|
+
- docs/js/app.js
|
|
109
|
+
- docs/js/full_list.js
|
|
110
|
+
- docs/js/jquery.js
|
|
111
|
+
- docs/method_list.html
|
|
112
|
+
- docs/top-level-namespace.html
|
|
113
|
+
- examples/example1.rb
|
|
114
|
+
- kdict.gemspec
|
|
115
|
+
- lib/kdict.rb
|
|
116
|
+
- lib/kdict/kwargdict.rb
|
|
117
|
+
- lib/kdict/kwargdict/kwargtypes.rb
|
|
118
|
+
- lib/kdict/kwargdict/kwargtypes/bool.rb
|
|
119
|
+
- lib/kdict/kwargmodel.rb
|
|
120
|
+
- lib/kdict/version.rb
|
|
121
|
+
- ndoc/checksums
|
|
122
|
+
- ndoc/complete
|
|
123
|
+
- ndoc/object_types
|
|
124
|
+
- ndoc/objects/root.dat
|
|
125
|
+
- ndoc/proxy_types
|
|
126
|
+
homepage:
|
|
127
|
+
licenses:
|
|
128
|
+
- MIT
|
|
129
|
+
metadata: {}
|
|
130
|
+
post_install_message:
|
|
131
|
+
rdoc_options: []
|
|
132
|
+
require_paths:
|
|
133
|
+
- lib
|
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
|
+
requirements:
|
|
141
|
+
- - ">="
|
|
142
|
+
- !ruby/object:Gem::Version
|
|
143
|
+
version: '0'
|
|
144
|
+
requirements: []
|
|
145
|
+
rubygems_version: 3.0.3
|
|
146
|
+
signing_key:
|
|
147
|
+
specification_version: 4
|
|
148
|
+
summary: KDict allows you to quickly create powerful Keyword-Argument Dictionaries
|
|
149
|
+
to automate input validation.
|
|
150
|
+
test_files: []
|