Examples:
class Widget
include Stannum::Entity
attribute :name, String
attribute :description, String, optional: true
attribute :quantity, Integer, default: 0
end
widget = Widget.new(name: 'Self-sealing Stem Bolt')
widget.name widget.description widget.quantity widget.attributes
widget.description = 'A stem bolt, but self sealing.'
widget.attributes #=>
# {
# name: 'Self-sealing Stem Bolt',
# description: 'A stem bolt, but self sealing.',
# quantity: 0
# }
widget.assign_attributes(quantity: 50)
widget.attributes #=>
# {
# name: 'Self-sealing Stem Bolt',
# description: 'A stem bolt, but self sealing.',
# quantity: 50
# }
widget.attributes = (name: 'Inverse Chronoton Emitter')
# {
# name: 'Inverse Chronoton Emitter',
# description: nil,
# quantity: 0
# }
Defining Attribute Constraints
Widget::Contract.matches?(quantity: -5) Widget::Contract.matches?(name: 'Capacitor', quantity: -5)
class Widget
constraint(:quantity) { |qty| qty >= 0 }
end
Widget::Contract.matches?(name: 'Capacitor', quantity: -5) Widget::Contract.matches?(name: 'Capacitor', quantity: 10)
Defining Struct Constraints
Widget::Contract.matches?(name: 'Diode')
class Widget
constraint { |struct| struct.description&.include?(struct.name) }
end
Widget::Contract.matches?(name: 'Diode') Widget::Contract.matches?(
name: 'Diode',
description: 'A low budget Diode',
)