Module: GL::Contract::ClassMethods
- Defined in:
- lib/gl/command/contract.rb
Instance Method Summary collapse
- #allows(*attributes, **strong_attributes) ⇒ Object
- #enforce_attribute_presence(*attributes) ⇒ Object
- #enforce_attribute_types(**attributes) ⇒ Object
- #requires(*attributes, **strong_attributes) ⇒ Object
- #requires_attributes(*attributes) ⇒ Object
- #requires_strong_attributes(**attributes) ⇒ Object
- #returns(*attributes, **strong_attributes) ⇒ Object
Instance Method Details
#allows(*attributes, **strong_attributes) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/gl/command/contract.rb', line 16 def allows(*attributes, **strong_attributes) delegate(*attributes, to: :context) return if strong_attributes.blank? delegate(*strong_attributes.keys, to: :context) enforce_attribute_types(**strong_attributes) end |
#enforce_attribute_presence(*attributes) ⇒ Object
55 56 57 |
# File 'lib/gl/command/contract.rb', line 55 def enforce_attribute_presence(*attributes) validates(*attributes, presence: true) if attributes.present? end |
#enforce_attribute_types(**attributes) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gl/command/contract.rb', line 59 def enforce_attribute_types(**attributes) return if attributes.blank? validates_each attributes.keys do |record, attr_name, value| next if value.blank? type = attributes[attr_name] record.errors.add attr_name, "does is not a #{type}" unless type_applies?(value, type) end end |
#requires(*attributes, **strong_attributes) ⇒ Object
24 25 26 27 |
# File 'lib/gl/command/contract.rb', line 24 def requires(*attributes, **strong_attributes) requires_attributes(*attributes) requires_strong_attributes(**strong_attributes) end |
#requires_attributes(*attributes) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/gl/command/contract.rb', line 29 def requires_attributes(*attributes) return if attributes.blank? delegate(*attributes, to: :context) enforce_attribute_presence(*attributes) end |
#requires_strong_attributes(**attributes) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/gl/command/contract.rb', line 36 def requires_strong_attributes(**attributes) return if attributes.blank? attribute_keys = attributes.keys requires_attributes(*attribute_keys) enforce_attribute_types(**attributes) end |
#returns(*attributes, **strong_attributes) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gl/command/contract.rb', line 44 def returns(*attributes, **strong_attributes) delegate(*attributes, to: :context) @return_attributes = attributes return if strong_attributes.blank? strong_attribute_keys = strong_attributes.keys delegate(*strong_attribute_keys, to: :context) @return_attributes.concat(strong_attribute_keys) @return_strong_attributes = strong_attributes end |