Module: ObjectAttorney::ClassMethods

Defined in:
lib/object_attorney/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allegationsObject



15
16
17
# File 'lib/object_attorney/class_methods.rb', line 15

def allegations
  @allegations ||= Hash.new { |hash, key| hash[key] = [] }
end

#defendant_optionsObject



7
8
9
# File 'lib/object_attorney/class_methods.rb', line 7

def defendant_options
  @defendant_options ||= {}
end

Instance Method Details

#defend(name, options = {}) ⇒ Object



11
12
13
# File 'lib/object_attorney/class_methods.rb', line 11

def defend(name, options = {})
  defendant_options.merge!(options.merge(name: name))
end

#inherited(base) ⇒ Object

Copy allegations on inheritance.



39
40
41
42
43
44
# File 'lib/object_attorney/class_methods.rb', line 39

def inherited(base)
  base.allegations = allegations.clone
  base.defendant_options = defendant_options.clone

  super
end

#validate(*args, &block) ⇒ Object



19
20
21
# File 'lib/object_attorney/class_methods.rb', line 19

def validate(*args, &block)
  allegations[nil] << Allegation.new(:custom, args, &block)
end

#validates_with(*args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/object_attorney/class_methods.rb', line 23

def validates_with(*args, &block)
  options = args.extract_options!

  # certain ActiveModel::Validations::<Class> need this
  options[:class] = self

  args.each do |validation_class|
    allegation = Allegation.new(validation_class, options, &block)

    allegation.attributes.each do |attribute|
      allegations[attribute.to_sym].push allegation
    end
  end
end