Module: Eye::Dsl::Validation::ClassMethods
- Defined in:
- lib/eye/dsl/validation.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#should_bes ⇒ Object
Returns the value of attribute should_bes.
-
#validates ⇒ Object
Returns the value of attribute validates.
-
#variants ⇒ Object
Returns the value of attribute variants.
Instance Method Summary collapse
- #del_param(param) ⇒ Object
- #inherited(subclass) ⇒ Object
- #param(param, types = [], should_be = false, default = nil, _variants = nil) ⇒ Object
- #param_default(param, default) ⇒ Object
- #validate(options = {}) ⇒ Object
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
16 17 18 |
# File 'lib/eye/dsl/validation.rb', line 16 def defaults @defaults end |
#should_bes ⇒ Object
Returns the value of attribute should_bes.
16 17 18 |
# File 'lib/eye/dsl/validation.rb', line 16 def should_bes @should_bes end |
#validates ⇒ Object
Returns the value of attribute validates.
16 17 18 |
# File 'lib/eye/dsl/validation.rb', line 16 def validates @validates end |
#variants ⇒ Object
Returns the value of attribute variants.
16 17 18 |
# File 'lib/eye/dsl/validation.rb', line 16 def variants @variants end |
Instance Method Details
#del_param(param) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/eye/dsl/validation.rb', line 44 def del_param(param) param = param.to_sym validates.delete(param) should_bes.delete(param) defaults.delete(param) variants.delete(param) remove_method(param) end |
#inherited(subclass) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/eye/dsl/validation.rb', line 9 def inherited(subclass) subclass.validates = self.validates.clone subclass.should_bes = self.should_bes.clone subclass.defaults = self.defaults.clone subclass.variants = self.variants.clone end |
#param(param, types = [], should_be = false, default = nil, _variants = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/eye/dsl/validation.rb', line 23 def param(param, types = [], should_be = false, default = nil, _variants = nil) param = param.to_sym validates[param] = types should_bes << param if should_be param_default(param, default) variants[param] = _variants return if param == :do define_method "#{param}" do value = @options[param] value.nil? ? default : value end end |
#param_default(param, default) ⇒ Object
39 40 41 42 |
# File 'lib/eye/dsl/validation.rb', line 39 def param_default(param, default) param = param.to_sym defaults[param] = default end |
#validate(options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/eye/dsl/validation.rb', line 53 def validate( = {}) .each do |param, value| param = param.to_sym types = validates[param] unless types if param != :type raise Error, "#{self.name} unknown param :#{param} value #{value.inspect}" end end if self.variants[param] if value && !value.is_a?(Proc) if value.is_a?(Array) if (value - self.variants[param]).present? raise Error, "#{value.inspect} should be within #{self.variants[param].inspect}" end elsif !self.variants[param].include?(value) raise Error, "#{value.inspect} should be within #{self.variants[param].inspect}" end end end next if types.blank? types = Array(types) good = types.any?{|type| value.is_a?(type) } raise Error, "#{self.name} bad param :#{param} value #{value.inspect}, type #{types.inspect}" unless good end should_bes.each do |param| raise Error, "#{self.name} for param :#{param} value should be" unless [param.to_sym] || defaults[param.to_sym] end end |