Module: Eye::Dsl::Validation::ClassMethods

Defined in:
lib/eye/dsl/validation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultsObject

Returns the value of attribute defaults.



18
19
20
# File 'lib/eye/dsl/validation.rb', line 18

def defaults
  @defaults
end

#should_besObject

Returns the value of attribute should_bes.



18
19
20
# File 'lib/eye/dsl/validation.rb', line 18

def should_bes
  @should_bes
end

#validatesObject

Returns the value of attribute validates.



18
19
20
# File 'lib/eye/dsl/validation.rb', line 18

def validates
  @validates
end

#variantsObject

Returns the value of attribute variants.



18
19
20
# File 'lib/eye/dsl/validation.rb', line 18

def variants
  @variants
end

Instance Method Details

#del_param(param) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/eye/dsl/validation.rb', line 61

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



11
12
13
14
15
16
# File 'lib/eye/dsl/validation.rb', line 11

def inherited(subclass)
  subclass.validates = validates.clone
  subclass.should_bes = should_bes.clone
  subclass.defaults = defaults.clone
  subclass.variants = variants.clone
end

#param(param, types = [], should_be = false, default = nil, variants = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/eye/dsl/validation.rb', line 36

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)
  self.variants[param] = variants

  return if param == :do

  define_method param do
    value = @options[param]
    value.nil? ? self.class.defaults[param] : value
  end

  define_method "#{param}=" do |value|
    @options[param] = value
  end
end

#param_default(param, default) ⇒ Object



56
57
58
59
# File 'lib/eye/dsl/validation.rb', line 56

def param_default(param, default)
  param = param.to_sym
  defaults[param] = default
end

#validate(options = {}) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/eye/dsl/validation.rb', line 70

def validate(options = {})
  options.each { |param, value| validate_param(param, value) }

  should_bes.each do |param|
    raise Error, "#{name} for param :#{param} value should be" unless options[param.to_sym] || defaults[param.to_sym]
  end
end

#validate_param(param, value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eye/dsl/validation.rb', line 78

def validate_param(param, value)
  param = param.to_sym
  types = validates[param]
  if !types && param != :type
    raise Error, "#{name} unknown param :#{param} value #{value.inspect}"
  end

  if variants[param] && value && !value.is_a?(Proc)
    if value.is_a?(Array)
      value = value.reject { |v| v.is_a?(Proc) }
      if (value - variants[param]).present?
        raise Error, "#{value.inspect} should be within #{variants[param].inspect}"
      end
    elsif !variants[param].include?(value)
      raise Error, "#{value.inspect} should be within #{variants[param].inspect}"
    end
  end

  if types.present?
    types = Array(types)
    good = types.any? { |type| value.is_a?(type) }
    raise Error, "#{name} bad param :#{param} value #{value.inspect}, type #{types.inspect}" unless good
  end
end