module Troupe::Contract::ClassMethods

Public Instance Methods

all_properties() click to toggle source
# File lib/troupe/contract.rb, line 100
def all_properties;       property_table.all_properties; end
default_for(attr) click to toggle source
# File lib/troupe/contract.rb, line 101
def default_for(attr);    property_table.default_for(attr); end
expected_and_permitted_properties() click to toggle source
# File lib/troupe/contract.rb, line 105
def expected_and_permitted_properties
  property_table.expected_and_permitted_properties
end
expected_properties() click to toggle source
# File lib/troupe/contract.rb, line 97
def expected_properties;  property_table.expected_properties; end
expects(*args, &block) click to toggle source

Sugar for core DSL

# File lib/troupe/contract.rb, line 81
def expects(*args, &block)
  presence_is(:expected, args, block)
end
missing_properties(context) click to toggle source
# File lib/troupe/contract.rb, line 103
def missing_properties(context);   property_table.missing_properties(context); end
on_violation(&block) click to toggle source
# File lib/troupe/contract.rb, line 75
def on_violation(&block)
  @on_violation_block = block
end
on_violation_block() click to toggle source
# File lib/troupe/contract.rb, line 93
def on_violation_block
  @on_violation_block
end
on_violation_for(*args, &block) click to toggle source
# File lib/troupe/contract.rb, line 68
def on_violation_for(*args, &block)
  args.each do |arg|
    next unless property = property_table.get(arg)
    property.on_violation = block
  end
end
permits(*args, &block) click to toggle source
# File lib/troupe/contract.rb, line 85
def permits(*args, &block)
  presence_is(:permitted, args, block)
end
permitted_properties() click to toggle source
# File lib/troupe/contract.rb, line 98
def permitted_properties; property_table.permitted_properties; end
property(attr, opts={}, &block) click to toggle source

Core DSL

# File lib/troupe/contract.rb, line 61
def property(attr, opts={}, &block)
  opts.merge!(default: block) if block
  property_table.set(attr, opts)

  delegate_properties
end
provided_properties() click to toggle source
# File lib/troupe/contract.rb, line 99
def provided_properties;  property_table.provided_properties; end
provides(*args, &block) click to toggle source
# File lib/troupe/contract.rb, line 89
def provides(*args, &block)
  presence_is(:provided, args, block)
end
violation_block_for(attr) click to toggle source
# File lib/troupe/contract.rb, line 102
def violation_block_for(attr);     property_table.get(attr).on_violation; end

Private Instance Methods

delegate_properties() click to toggle source
# File lib/troupe/contract.rb, line 116
def delegate_properties
  all_properties.each do |attr|
    define_method attr do
      next context[attr] if context.members.include?(attr)
      default = self.class.default_for(attr)
      context[attr] = if default.is_a?(Proc)
                        instance_exec(&default)
                      elsif default.is_a?(Symbol)
                        send(default)
                      end
    end

    define_method "#{attr}=" do |value|
      context[attr] = value
    end
  end
end
presence_is(presence, args, block) click to toggle source
# File lib/troupe/contract.rb, line 134
def presence_is(presence, args, block)
  opts = args.detect { |arg| arg.is_a?(Hash) } || {}
  opts.merge!(presence: presence)
  args.reject! { |arg| arg.is_a?(Hash) }

  args.each do |arg|
    property(arg, opts, &block)
  end
end
property_table() click to toggle source
# File lib/troupe/contract.rb, line 111
def property_table
  @property_table ||= PropertyTable.new
end