Class: Troupe::Contract::PropertyTable
- Inherits:
-
Object
- Object
- Troupe::Contract::PropertyTable
- Defined in:
- lib/troupe/contract/property_table.rb
Instance Method Summary collapse
- #all_properties ⇒ Object
- #default_for(property_name) ⇒ Object
- #each_property ⇒ Object
- #expected ⇒ Object
- #expected_and_permitted_properties ⇒ Object
- #expected_properties ⇒ Object
- #get(property_name) ⇒ Object
-
#initialize ⇒ PropertyTable
constructor
A new instance of PropertyTable.
- #missing_properties(context) ⇒ Object
- #permitted ⇒ Object
- #permitted_properties ⇒ Object
- #provided ⇒ Object
- #provided_properties ⇒ Object
- #select(args) ⇒ Object
- #set(property_name, opts = {}) ⇒ Object
- #undeclared_properties(context) ⇒ Object
Constructor Details
#initialize ⇒ PropertyTable
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_properties ⇒ Object
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_property ⇒ Object
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 |
#expected ⇒ Object
31 |
# File 'lib/troupe/contract/property_table.rb', line 31 def expected; select(presence: :expected); end |
#expected_and_permitted_properties ⇒ Object
45 46 47 |
# File 'lib/troupe/contract/property_table.rb', line 45 def expected_and_permitted_properties expected_properties + permitted_properties end |
#expected_properties ⇒ Object
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 |
#permitted ⇒ Object
32 |
# File 'lib/troupe/contract/property_table.rb', line 32 def permitted; select(presence: :permitted); end |
#permitted_properties ⇒ Object
42 |
# File 'lib/troupe/contract/property_table.rb', line 42 def permitted_properties; permitted.keys; end |
#provided ⇒ Object
33 |
# File 'lib/troupe/contract/property_table.rb', line 33 def provided; select(presence: :provided); end |
#provided_properties ⇒ Object
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 |