Module: Troupe::Contract

Defined in:
lib/troupe/contract.rb,
lib/troupe/contract/property.rb,
lib/troupe/contract/property_table.rb

Defined Under Namespace

Modules: ClassMethods Classes: Property, PropertyTable

Constant Summary collapse

VALID_TYPES =
%i(open closed)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/troupe/contract.rb', line 5

def self.included(base)
  base.class_eval do
    extend ClassMethods
  end

  private

  def violation_table
    @violation_table ||= {}
  end

  def validate_contract_expectations
    populate_violation_table
    check_each_violation
  end

  def missing_properties
    @missing_properties ||= self.class.missing_properties(context)
  end

  def ensure_contract_defaults
    self.class.all_properties.each do |attr|
      send(attr)
    end
  end

  def populate_violation_table
    missing_properties.each do |property_name|
      violation_table[property_name] = ContractViolation.new(
        self,
        property: property_name,
        message: "Expected context to include property '#{property_name}'."
      )
    end
  end

  def check_each_violation
    return if violation_table.empty?
    violation_table.each do |property_name, violation|
      if block = violation_block_for(property_name)
        instance_exec(violation, &block)
      else
        raise violation
      end
    end
  end

  def violation_block_for(property_name)
    self.class.violation_block_for(property_name) ||
      self.class.on_violation_block
  end
end

Instance Method Details

#check_each_violationObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/troupe/contract.rb', line 41

def check_each_violation
  return if violation_table.empty?
  violation_table.each do |property_name, violation|
    if block = violation_block_for(property_name)
      instance_exec(violation, &block)
    else
      raise violation
    end
  end
end

#ensure_contract_defaultsObject



25
26
27
28
29
# File 'lib/troupe/contract.rb', line 25

def ensure_contract_defaults
  self.class.all_properties.each do |attr|
    send(attr)
  end
end

#missing_propertiesObject



21
22
23
# File 'lib/troupe/contract.rb', line 21

def missing_properties
  @missing_properties ||= self.class.missing_properties(context)
end

#populate_violation_tableObject



31
32
33
34
35
36
37
38
39
# File 'lib/troupe/contract.rb', line 31

def populate_violation_table
  missing_properties.each do |property_name|
    violation_table[property_name] = ContractViolation.new(
      self,
      property: property_name,
      message: "Expected context to include property '#{property_name}'."
    )
  end
end

#validate_contract_expectationsObject



16
17
18
19
# File 'lib/troupe/contract.rb', line 16

def validate_contract_expectations
  populate_violation_table
  check_each_violation
end

#violation_block_for(property_name) ⇒ Object



52
53
54
55
# File 'lib/troupe/contract.rb', line 52

def violation_block_for(property_name)
  self.class.violation_block_for(property_name) ||
    self.class.on_violation_block
end

#violation_tableObject



12
13
14
# File 'lib/troupe/contract.rb', line 12

def violation_table
  @violation_table ||= {}
end