Class: Troupe::Contract::PropertyTable

Inherits:
Object
  • Object
show all
Defined in:
lib/troupe/contract/property_table.rb

Instance Method Summary collapse

Constructor Details

#initializePropertyTable

Returns a new instance of PropertyTable.



4
5
6
# File 'lib/troupe/contract/property_table.rb', line 4

def initialize
  @table ||= {}
end

Instance Method Details

#all_propertiesObject



35
36
37
38
39
# File 'lib/troupe/contract/property_table.rb', line 35

def all_properties
  expected_properties +
    permitted_properties +
    provided_properties
end

#default_for(property_name) ⇒ Object



61
62
63
# File 'lib/troupe/contract/property_table.rb', line 61

def default_for(property_name)
  @table[property_name].default
end

#each_propertyObject



17
18
19
20
21
# File 'lib/troupe/contract/property_table.rb', line 17

def each_property
  @table.each do |property_name, property|
    yield property_name, property
  end
end

#expectedObject



31
# File 'lib/troupe/contract/property_table.rb', line 31

def expected;  select(presence: :expected); end

#expected_and_permitted_propertiesObject



45
46
47
# File 'lib/troupe/contract/property_table.rb', line 45

def expected_and_permitted_properties
  expected_properties + permitted_properties
end

#expected_propertiesObject



41
# File 'lib/troupe/contract/property_table.rb', line 41

def expected_properties; expected.keys; end

#get(property_name) ⇒ Object



8
9
10
# File 'lib/troupe/contract/property_table.rb', line 8

def get(property_name)
  @table[property_name]
end

#missing_properties(context) ⇒ Object



55
56
57
58
59
# File 'lib/troupe/contract/property_table.rb', line 55

def missing_properties(context)
  expected_properties.select do |attr|
    !context.members.include?(attr)
  end
end

#permittedObject



32
# File 'lib/troupe/contract/property_table.rb', line 32

def permitted; select(presence: :permitted); end

#permitted_propertiesObject



42
# File 'lib/troupe/contract/property_table.rb', line 42

def permitted_properties; permitted.keys; end

#providedObject



33
# File 'lib/troupe/contract/property_table.rb', line 33

def provided;  select(presence: :provided); end

#provided_propertiesObject



43
# File 'lib/troupe/contract/property_table.rb', line 43

def provided_properties; provided.keys; end

#select(args) ⇒ Object



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

def select(args)
  @table.select do |_, property|
    args == args.select do |k, v|
      property.send(k) == v
    end
  end
end

#set(property_name, opts = {}) ⇒ Object



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

def set(property_name, opts={})
  @table[property_name] ||= Property.new(opts)
  @table[property_name].merge!(opts)
end

#undeclared_properties(context) ⇒ Object



49
50
51
52
53
# File 'lib/troupe/contract/property_table.rb', line 49

def undeclared_properties(context)
  context.members.select do |attr|
    !expected_and_permitted_properties.include?(attr)
  end
end