Class: DelegateValidation::Validator

Inherits:
Object
  • Object
show all
Includes:
Validateable
Defined in:
app/models/delegate_validation.rb

Defined Under Namespace

Classes: DelegateError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validateable

append_features, #method_missing, #validate!

Constructor Details

#initialize(target) ⇒ Validator

Returns a new instance of Validator


42
43
44
# File 'app/models/delegate_validation.rb', line 42

def initialize(target)
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Validateable

Class Method Details

.delegate_attribute(*args) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/delegate_validation.rb', line 46

def self.delegate_attribute(*args)
  options   = args.extract_options!
  type_cast = ".#{options[:type_cast]}" if options.key?(:type_cast) && options[:type_cast].present?
  default   = " || #{options[:default].inspect}" if options.key?(:default)

  args.each do |attribute|
    line = __LINE__ + 1
    class_eval("
      def #{attribute}_before_type_cast
        #{options[:to]}.#{attribute} #{default}
      end

      def #{attribute}
        #{attribute}_before_type_cast#{type_cast}
      end

      def #{attribute}_needs_checking?
        #{attribute}_before_type_cast.present? or include_unset_values?
      end
    ", __FILE__, line)
  end
end

.nameObject


38
39
40
# File 'app/models/delegate_validation.rb', line 38

def self.name
  'Nothing'
end