Class: HashValidator::DSLProxy
- Inherits:
-
Object
- Object
- HashValidator::DSLProxy
- Defined in:
- lib/hash_validator/hash_validator.rb
Instance Attribute Summary collapse
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
- #allowed_values(ary) ⇒ Object
- #default(hsh) ⇒ Object
- #forbidden_values(ary) ⇒ Object
-
#initialize(subject) ⇒ DSLProxy
constructor
A new instance of DSLProxy.
- #key(k, opts = {}, &block) ⇒ Object
- #optional(*args, &block) ⇒ Object
- #required(*args, &block) ⇒ Object
- #type(klass) ⇒ Object
Constructor Details
#initialize(subject) ⇒ DSLProxy
Returns a new instance of DSLProxy.
113 114 115 |
# File 'lib/hash_validator/hash_validator.rb', line 113 def initialize(subject) @subject = subject end |
Instance Attribute Details
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
111 112 113 |
# File 'lib/hash_validator/hash_validator.rb', line 111 def subject @subject end |
Instance Method Details
#allowed_values(ary) ⇒ Object
144 145 146 |
# File 'lib/hash_validator/hash_validator.rb', line 144 def allowed_values(ary) @subject.add_option(:allowed_values, ary) end |
#default(hsh) ⇒ Object
152 153 154 155 |
# File 'lib/hash_validator/hash_validator.rb', line 152 def default(hsh) raise ArgumentError unless hsh.is_a?(Hash) @subject.default_value = hsh end |
#forbidden_values(ary) ⇒ Object
148 149 150 |
# File 'lib/hash_validator/hash_validator.rb', line 148 def forbidden_values(ary) @subject.add_option(:forbidden_values, ary) end |
#key(k, opts = {}, &block) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/hash_validator/hash_validator.rb', line 117 def key(k, opts = {}, &block) opts.reverse_merge!({ type: String }) @subject.key(k, opts, &block) end |
#optional(*args, &block) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/hash_validator/hash_validator.rb', line 125 def optional(*args, &block) opts = args.last.is_a?(Hash) ? args.pop : {} args.each do |key| key(key, opts.merge({required: false}), &block) end end |
#required(*args, &block) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/hash_validator/hash_validator.rb', line 132 def required(*args, &block) opts = args.last.is_a?(Hash) ? args.pop : {} args.each do |key| key(key, opts.merge({required: true}), &block) end end |
#type(klass) ⇒ Object
139 140 141 142 |
# File 'lib/hash_validator/hash_validator.rb', line 139 def type(klass) raise ArgumentError unless klass.is_a?(Class) @subject.add_option(:type, klass) end |