Module: FirstResponder::ClassMethods

Defined in:
lib/first_responder.rb

Instance Method Summary collapse

Instance Method Details

#add_to_nested(attr, type) ⇒ Object



129
130
131
132
# File 'lib/first_responder.rb', line 129

def add_to_nested(attr, type)
  return if type.is_a? Array
  nested_validations << attr if type.ancestors.include?(FirstResponder)
end

#add_to_required(attr, opts) ⇒ Object



124
125
126
127
# File 'lib/first_responder.rb', line 124

def add_to_required(attr, opts)
  first_responder_opts = opts.extract!(:at)[:at]
  required_attributes << Hash[attr, first_responder_opts]
end

#default_validationsObject



113
114
115
# File 'lib/first_responder.rb', line 113

def default_validations
  { presence: true }
end

#first_responder_rootObject



97
98
99
# File 'lib/first_responder.rb', line 97

def first_responder_root
  @first_responder_root ||= ""
end

#nested_validationsObject



93
94
95
# File 'lib/first_responder.rb', line 93

def nested_validations
  @nested_validations ||= []
end

#proc_on_invalidObject



105
106
107
# File 'lib/first_responder.rb', line 105

def proc_on_invalid
  @proc_on_invalid
end

#required_attributesObject



89
90
91
# File 'lib/first_responder.rb', line 89

def required_attributes
  @required_attributes ||= []
end

#requires(attr, type, opts = {}) ⇒ Object



117
118
119
120
121
122
# File 'lib/first_responder.rb', line 117

def requires(attr, type, opts={})
  add_to_required(attr, opts)
  add_to_nested(attr, type)
  validates attr, default_validations.merge(opts)
  attribute attr, type, opts
end

#root(node) ⇒ Object



101
102
103
# File 'lib/first_responder.rb', line 101

def root(node)
  @first_responder_root = node
end

#when_invalid(&blk) ⇒ Object



109
110
111
# File 'lib/first_responder.rb', line 109

def when_invalid(&blk)
  @proc_on_invalid = blk
end