Class: ActiveFacts::API::RoleValues

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/api/role_values.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.identifying_roles.dup).delete_at(@excluded_role) if @excluded_role
end

Instance Attribute Details

#index_rolesObject

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

#roleObject

Returns the value of attribute role.



11
12
13
# File 'lib/activefacts/api/role_values.rb', line 11

def role
  @role
end

#sortObject

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.identifying_role_values : 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

Returns:

  • (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.identifying_role_values((c = r.counterpart) ? c.object_type : role_value.class)
    }
  else
    object.identifying_role_values
  end
end

#keysObject



55
56
57
# File 'lib/activefacts/api/role_values.rb', line 55

def keys
  @sort ? @a.keys : @a
end

#object_typeObject



14
15
16
# File 'lib/activefacts/api/role_values.rb', line 14

def object_type
  @role.object_type
end

#singleObject



59
60
61
# File 'lib/activefacts/api/role_values.rb', line 59

def single
  size > 1 ? nil : to_a[0]
end

#to_aObject



47
48
49
# File 'lib/activefacts/api/role_values.rb', line 47

def to_a
  @sort ? @a.values : @a
end

#to_aryObject



51
52
53
# File 'lib/activefacts/api/role_values.rb', line 51

def to_ary
  to_a
end

#verbaliseObject



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