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(object_type, index_roles = nil, sort = nil) ⇒ RoleValues

Returns a new instance of RoleValues.



15
16
17
18
19
20
# File 'lib/activefacts/api/role_values.rb', line 15

def initialize object_type, index_roles = nil, sort = nil
  @object_type = object_type
  @sort = sort == nil ? API::sorted : !!sort
  @a = @sort ? RBTree.new : []
  @index_roles = index_roles
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

#object_typeObject

Returns the value of attribute object_type.



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

def object_type
  @object_type
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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/activefacts/api/role_values.rb', line 91

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



22
23
24
25
26
27
28
# File 'lib/activefacts/api/role_values.rb', line 22

def +(a)
  if @sort
    @a.values.+(a.is_a?(RoleValues) ? [a] : a)
  else
    @a.+(a.is_a?(RoleValues) ? [a] : a)
  end
end

#[](*a) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/activefacts/api/role_values.rb', line 30

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



69
70
71
72
73
74
75
# File 'lib/activefacts/api/role_values.rb', line 69

def add_instance(value, key)
  if @sort
    @a[form_key(index_values(value))] = value
  else
    @a << value
  end
end

#delete_instance(value, key) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/activefacts/api/role_values.rb', line 77

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



53
54
55
56
57
58
59
# File 'lib/activefacts/api/role_values.rb', line 53

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)


107
108
109
110
111
112
113
# File 'lib/activefacts/api/role_values.rb', line 107

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

#index_values(object) ⇒ Object



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

def index_values object
  if @index_roles
    @index_roles.map{|r| object.send(r.name).identifying_role_values}
  else
    object.identifying_role_values
  end
end

#keysObject



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

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

#singleObject



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

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

#to_aObject



41
42
43
# File 'lib/activefacts/api/role_values.rb', line 41

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

#verbaliseObject



85
86
87
88
# File 'lib/activefacts/api/role_values.rb', line 85

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