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
- #delete_instance(value, key) ⇒ Object
- #form_key(a) ⇒ Object
- #include?(v) ⇒ Boolean
- #index_values(object) ⇒ Object
-
#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.
18 19 20 21 22 23 24 25 |
# File 'lib/activefacts/api/role_values.rb', line 18 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 : [] (@index_roles = role.object_type..dup).delete_at(@excluded_role) if @excluded_role 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/activefacts/api/role_values.rb', line 107 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
27 28 29 30 31 32 33 |
# File 'lib/activefacts/api/role_values.rb', line 27 def +(a) if @sort @a.values.+(a.is_a?(RoleValues) ? [a] : a) else @a.+(a.is_a?(RoleValues) ? [a] : a) end end |
#[](*a) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/activefacts/api/role_values.rb', line 35 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
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/activefacts/api/role_values.rb', line 82 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 # Old slow way: @a[form_key(index_values(value))] = value else @a << value end end |
#delete_instance(value, key) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/activefacts/api/role_values.rb', line 93 def delete_instance(value, key) if @sort deleted = @a.delete(form_key(key)) else deleted = @a.delete(value) # Slow: it has to search the array end end |
#form_key(a) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/activefacts/api/role_values.rb', line 63 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
123 124 125 126 127 128 129 |
# File 'lib/activefacts/api/role_values.rb', line 123 def include? v if @sort @a.include?(form_key(v)) else @a.include?(v) end end |
#index_values(object) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/activefacts/api/role_values.rb', line 71 def index_values object if @index_roles @index_roles.map{|r| role_value = object.send(r.name) role_value.((c = r.counterpart) ? c.object_type : role_value.class) } else object. end end |
#keys ⇒ Object
55 56 57 |
# File 'lib/activefacts/api/role_values.rb', line 55 def keys @sort ? @a.keys : @a end |
#object_type ⇒ Object
14 15 16 |
# File 'lib/activefacts/api/role_values.rb', line 14 def object_type @role.object_type end |
#single ⇒ Object
59 60 61 |
# File 'lib/activefacts/api/role_values.rb', line 59 def single size > 1 ? nil : to_a[0] end |
#to_a ⇒ Object
47 48 49 |
# File 'lib/activefacts/api/role_values.rb', line 47 def to_a @sort ? @a.values : @a end |
#to_ary ⇒ Object
51 52 53 |
# File 'lib/activefacts/api/role_values.rb', line 51 def to_ary to_a end |
#verbalise ⇒ Object
101 102 103 104 |
# File 'lib/activefacts/api/role_values.rb', line 101 def verbalise a = @sort ? @a.values : @a "[#{a.map(&:verbalise).join(", ")}]" end |