Class: ActiveFacts::API::RoleValues
- Inherits:
-
Object
- Object
- ActiveFacts::API::RoleValues
- Defined in:
- lib/activefacts/api/role_values.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#index_roles ⇒ Object
Returns the value of attribute index_roles.
-
#role ⇒ Object
Returns the value of attribute role.
-
#sort ⇒ Object
Returns the value of attribute sort.
Class Method Summary collapse
Instance Method Summary collapse
- #+(a) ⇒ Object
- #[](*a) ⇒ Object
-
#add_instance(value, key) ⇒ Object
The key must include the excluded role, if any.
-
#delete_instance(value, key) ⇒ Object
The key must include the excluded role, if any.
- #form_key(a) ⇒ Object
- #include?(v) ⇒ Boolean
-
#index_values(object) ⇒ Object
Return the full key for the object according to the object_type of this role.
-
#initialize(role, excluded_role = nil) ⇒ RoleValues
constructor
A new instance of RoleValues.
- #keys ⇒ Object
- #object_type ⇒ Object
- #single ⇒ Object
- #to_a ⇒ Object
- #to_ary ⇒ Object
- #verbalise ⇒ Object
Constructor Details
#initialize(role, excluded_role = nil) ⇒ RoleValues
Returns a new instance of RoleValues.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/activefacts/api/role_values.rb', line 19 def initialize role, excluded_role = nil @role = role # Can't control sorting from the constructor API: @sort = sort == nil ? API::sorted : !!sort @sort = API::sorted @excluded_role = excluded_role @a = @sort ? RBTree.new : [] if @excluded_role @index_roles = role.object_type..dup @index_roles.delete_at(@excluded_role) @index_roles.freeze end end |
Instance Attribute Details
#index_roles ⇒ Object
Returns the value of attribute index_roles.
13 14 15 |
# File 'lib/activefacts/api/role_values.rb', line 13 def index_roles @index_roles end |
#role ⇒ Object
Returns the value of attribute role.
11 12 13 |
# File 'lib/activefacts/api/role_values.rb', line 11 def role @role end |
#sort ⇒ Object
Returns the value of attribute sort.
12 13 14 |
# File 'lib/activefacts/api/role_values.rb', line 12 def sort @sort end |
Class Method Details
.def_single_delegator(accessor, method, *expected_arities) ⇒ Object
Paranoia. Because of changes in the implementation, I need to catch old code that calls these delegates incorrectly
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/activefacts/api/role_values.rb', line 118 def self.def_single_delegator(accessor, method, *expected_arities) str = %{ def #{method}(*args, &block) if #{expected_arities.size == 0 ? "block" : "!block || !#{expected_arities.inspect}.include?(block.arity)" } raise ArgumentError.new("Arity mismatch on #{name}\##{method}, got \#{block ? block.arity : 'none'} want #{expected_arities.inspect} at \#{caller*"\n\t"})") end if @sort #{accessor}.values.__send__(:#{method}, *args, &block) else #{accessor}.__send__(:#{method}, *args, &block) end end } eval(str) end |
Instance Method Details
#+(a) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/activefacts/api/role_values.rb', line 32 def +(a) if @sort @a.values.+(a.is_a?(RoleValues) ? [a] : a) else @a.+(a.is_a?(RoleValues) ? [a] : a) end end |
#[](*a) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/activefacts/api/role_values.rb', line 40 def [](*a) if @sort #puts "Indexing #{object_type.name}.#{role.name} using #{a.inspect}:\n\t" + caller*"\n\t" + "\n\t---\n" key = form_key(a.map{|a| a.respond_to?(:identifying_role_values) ? a. : a}) # REVISIT: Consider whether to return an array when a partial key is provided. @a[key] else # Slow: Search the array for an element having the matching key: @a.detect{|e| index_values(e) == a} end end |
#add_instance(value, key) ⇒ Object
The key must include the excluded role, if any
89 90 91 92 93 94 95 96 97 |
# File 'lib/activefacts/api/role_values.rb', line 89 def add_instance(value, key) if @sort # Exclude the excluded role, if any: (key = key.dup).delete_at(@excluded_role) if @excluded_role @a[form_key(key)] = value else @a << value end end |
#delete_instance(value, key) ⇒ Object
The key must include the excluded role, if any
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/activefacts/api/role_values.rb', line 100 def delete_instance(value, key) if @sort if key.size != role.object_type..size raise "Internal error: incorrectly-sized key #{key.inspect} to delete_instance" end (key = key.dup).delete_at(@excluded_role) if @excluded_role deleted = @a.delete(form_key(key)) else deleted = @a.delete(value) # Slow: it has to search the array end end |
#form_key(a) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/activefacts/api/role_values.rb', line 68 def form_key a a = Array(a) if @index_roles && @index_roles.size != a.size raise "Incorrectly-sized key #{a.inspect}. Index roles are #{@index_roles.map(&:name).inspect}" end KeyArray.new(a) end |
#include?(v) ⇒ Boolean
134 135 136 137 138 139 140 |
# File 'lib/activefacts/api/role_values.rb', line 134 def include? v if @sort @a.include?(form_key(v)) else @a.include?(v) end end |
#index_values(object) ⇒ Object
Return the full key for the object according to the object_type of this role
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/activefacts/api/role_values.rb', line 77 def index_values object if @excluded_role @index_roles.map do |r| role_value = object.send(r.name) role_value.((c = r.counterpart) ? c.object_type : role_value.class) end else object.(role.object_type) end end |
#keys ⇒ Object
60 61 62 |
# File 'lib/activefacts/api/role_values.rb', line 60 def keys @sort ? @a.keys : @a end |
#object_type ⇒ Object
15 16 17 |
# File 'lib/activefacts/api/role_values.rb', line 15 def object_type @role.object_type end |
#single ⇒ Object
64 65 66 |
# File 'lib/activefacts/api/role_values.rb', line 64 def single size > 1 ? nil : to_a[0] end |
#to_a ⇒ Object
52 53 54 |
# File 'lib/activefacts/api/role_values.rb', line 52 def to_a @sort ? @a.values : @a end |
#to_ary ⇒ Object
56 57 58 |
# File 'lib/activefacts/api/role_values.rb', line 56 def to_ary to_a end |
#verbalise ⇒ Object
112 113 114 115 |
# File 'lib/activefacts/api/role_values.rb', line 112 def verbalise a = @sort ? @a.values : @a "[#{a.map(&:verbalise).join(", ")}]" end |