Module: ObjectAttorney::ClassMethods
- Defined in:
- lib/object_attorney/class_methods.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #defend(name, options = {}) ⇒ Object
-
#inherited(base) ⇒ Object
Copy allegations on inheritance.
- #validate(*args, &block) ⇒ Object
- #validates_with(*args, &block) ⇒ Object
Instance Attribute Details
#allegations ⇒ Object
15 16 17 |
# File 'lib/object_attorney/class_methods.rb', line 15 def allegations @allegations ||= Hash.new { |hash, key| hash[key] = [] } end |
#defendant_options ⇒ Object
7 8 9 |
# File 'lib/object_attorney/class_methods.rb', line 7 def ||= {} end |
Instance Method Details
#defend(name, options = {}) ⇒ Object
11 12 13 |
# File 'lib/object_attorney/class_methods.rb', line 11 def defend(name, = {}) .merge!(.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. = .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) = args. # certain ActiveModel::Validations::<Class> need this [:class] = self args.each do |validation_class| allegation = Allegation.new(validation_class, , &block) allegation.attributes.each do |attribute| allegations[attribute.to_sym].push allegation end end end |