Class: DelegateValidation::Validator

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

Overview

rubocop:enable Metrics/MethodLength

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.


47
48
49
# File 'app/models/delegate_validation.rb', line 47

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

rubocop:todo Metrics/MethodLength


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/delegate_validation.rb', line 51

def self.delegate_attribute(*args) # rubocop:todo Metrics/MethodLength
  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


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

def self.name
  'Nothing'
end