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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/activefacts/api/role_values.rb', line 106

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
# File 'lib/activefacts/api/role_values.rb', line 35

def [](*a)
	if @sort
	  # REVISIT: Consider whether to return an array when a partial key is provided.
	  key = form_key(Array(a))
	  @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



81
82
83
84
85
86
87
88
89
90
# File 'lib/activefacts/api/role_values.rb', line 81

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



92
93
94
95
96
97
98
# File 'lib/activefacts/api/role_values.rb', line 92

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



62
63
64
65
66
67
68
# File 'lib/activefacts/api/role_values.rb', line 62

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)


122
123
124
125
126
127
128
# File 'lib/activefacts/api/role_values.rb', line 122

def include? v
	if @sort
	  @a.include?(form_key(v))
	else
	  @a.include?(v)
	end
end

#index_values(object) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/activefacts/api/role_values.rb', line 70

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



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

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



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

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

#to_aObject



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

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

#to_aryObject



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

def to_ary
	to_a
end

#verbaliseObject



100
101
102
103
# File 'lib/activefacts/api/role_values.rb', line 100

def verbalise
	a = @sort ? @a.values : @a
  "[#{a.map(&:verbalise).join(", ")}]"
end