activefacts-api 1.9.5 → 1.9.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,48 +12,48 @@ module ActiveFacts
12
12
  attr_accessor :sort
13
13
  attr_accessor :index_roles
14
14
  def object_type
15
- @role.object_type
15
+ @role.object_type
16
16
  end
17
17
 
18
18
  def initialize role, excluded_role = nil
19
- @role = role
20
- # Can't control sorting from the constructor API: @sort = sort == nil ? API::sorted : !!sort
21
- @sort = API::sorted
22
- @excluded_role = excluded_role
19
+ @role = role
20
+ # Can't control sorting from the constructor API: @sort = sort == nil ? API::sorted : !!sort
21
+ @sort = API::sorted
22
+ @excluded_role = excluded_role
23
23
  @a = @sort ? RBTree.new : []
24
- (@index_roles = role.object_type.identifying_roles.dup).delete_at(@excluded_role) if @excluded_role
24
+ (@index_roles = role.object_type.identifying_roles.dup).delete_at(@excluded_role) if @excluded_role
25
25
  end
26
26
 
27
27
  def +(a)
28
- if @sort
29
- @a.values.+(a.is_a?(RoleValues) ? [a] : a)
30
- else
31
- @a.+(a.is_a?(RoleValues) ? [a] : a)
32
- end
28
+ if @sort
29
+ @a.values.+(a.is_a?(RoleValues) ? [a] : a)
30
+ else
31
+ @a.+(a.is_a?(RoleValues) ? [a] : a)
32
+ end
33
33
  end
34
34
 
35
35
  def [](*a)
36
- if @sort
37
- #puts "Indexing #{object_type.name}.#{role.name} using #{a.inspect}:\n\t" + caller*"\n\t" + "\n\t---\n"
38
- key = form_key(a.map{|a| a.respond_to?(:identifying_role_values) ? a.identifying_role_values : a})
39
- # REVISIT: Consider whether to return an array when a partial key is provided.
40
- @a[key]
41
- else
42
- # Slow: Search the array for an element having the matching key:
43
- @a.detect{|e| index_values(e) == a}
44
- end
36
+ if @sort
37
+ #puts "Indexing #{object_type.name}.#{role.name} using #{a.inspect}:\n\t" + caller*"\n\t" + "\n\t---\n"
38
+ key = form_key(a.map{|a| a.respond_to?(:identifying_role_values) ? a.identifying_role_values : a})
39
+ # REVISIT: Consider whether to return an array when a partial key is provided.
40
+ @a[key]
41
+ else
42
+ # Slow: Search the array for an element having the matching key:
43
+ @a.detect{|e| index_values(e) == a}
44
+ end
45
45
  end
46
46
 
47
47
  def to_a
48
- @sort ? @a.values : @a
48
+ @sort ? @a.values : @a
49
49
  end
50
50
 
51
51
  def to_ary
52
- to_a
52
+ to_a
53
53
  end
54
54
 
55
55
  def keys
56
- @sort ? @a.keys : @a
56
+ @sort ? @a.keys : @a
57
57
  end
58
58
 
59
59
  def single
@@ -61,71 +61,71 @@ module ActiveFacts
61
61
  end
62
62
 
63
63
  def form_key a
64
- a = Array(a)
65
- if @index_roles && @index_roles.size != a.size
66
- raise "Incorrectly-sized key #{a.inspect}. Index roles are #{@index_roles.map(&:name).inspect}"
67
- end
68
- KeyArray.new(a)
64
+ a = Array(a)
65
+ if @index_roles && @index_roles.size != a.size
66
+ raise "Incorrectly-sized key #{a.inspect}. Index roles are #{@index_roles.map(&:name).inspect}"
67
+ end
68
+ KeyArray.new(a)
69
69
  end
70
70
 
71
71
  def index_values object
72
- if @index_roles
73
- @index_roles.map{|r|
74
- role_value = object.send(r.name)
75
- role_value.identifying_role_values((c = r.counterpart) ? c.object_type : role_value.class)
76
- }
77
- else
78
- object.identifying_role_values
79
- end
72
+ if @index_roles
73
+ @index_roles.map{|r|
74
+ role_value = object.send(r.name)
75
+ role_value.identifying_role_values((c = r.counterpart) ? c.object_type : role_value.class)
76
+ }
77
+ else
78
+ object.identifying_role_values
79
+ end
80
80
  end
81
81
 
82
82
  def add_instance(value, key)
83
- if @sort
84
- # Exclude the excluded role, if any:
85
- (key = key.dup).delete_at(@excluded_role) if @excluded_role
86
- @a[form_key(key)] = value
87
- # Old slow way: @a[form_key(index_values(value))] = value
88
- else
89
- @a << value
90
- end
83
+ if @sort
84
+ # Exclude the excluded role, if any:
85
+ (key = key.dup).delete_at(@excluded_role) if @excluded_role
86
+ @a[form_key(key)] = value
87
+ # Old slow way: @a[form_key(index_values(value))] = value
88
+ else
89
+ @a << value
90
+ end
91
91
  end
92
92
 
93
93
  def delete_instance(value, key)
94
- if @sort
95
- deleted = @a.delete(form_key(key))
96
- else
97
- deleted = @a.delete(value) # Slow: it has to search the array
98
- end
94
+ if @sort
95
+ deleted = @a.delete(form_key(key))
96
+ else
97
+ deleted = @a.delete(value) # Slow: it has to search the array
98
+ end
99
99
  end
100
100
 
101
101
  def verbalise
102
- a = @sort ? @a.values : @a
102
+ a = @sort ? @a.values : @a
103
103
  "[#{a.map(&:verbalise).join(", ")}]"
104
104
  end
105
105
 
106
106
  # Paranoia. Because of changes in the implementation, I need to catch old code that calls these delegates incorrectly
107
107
  def self.def_single_delegator(accessor, method, *expected_arities)
108
- str = %{
109
- def #{method}(*args, &block)
110
- if #{expected_arities.size == 0 ? "block" : "!block || !#{expected_arities.inspect}.include?(block.arity)" }
111
- raise ArgumentError.new("Arity mismatch on #{name}\##{method}, got \#{block ? block.arity : 'none'} want #{expected_arities.inspect} at \#{caller*"\n\t"})")
112
- end
113
- if @sort
114
- #{accessor}.values.__send__(:#{method}, *args, &block)
115
- else
116
- #{accessor}.__send__(:#{method}, *args, &block)
117
- end
118
- end
119
- }
120
- eval(str)
108
+ str = %{
109
+ def #{method}(*args, &block)
110
+ if #{expected_arities.size == 0 ? "block" : "!block || !#{expected_arities.inspect}.include?(block.arity)" }
111
+ raise ArgumentError.new("Arity mismatch on #{name}\##{method}, got \#{block ? block.arity : 'none'} want #{expected_arities.inspect} at \#{caller*"\n\t"})")
112
+ end
113
+ if @sort
114
+ #{accessor}.values.__send__(:#{method}, *args, &block)
115
+ else
116
+ #{accessor}.__send__(:#{method}, *args, &block)
117
+ end
118
+ end
119
+ }
120
+ eval(str)
121
121
  end
122
122
 
123
123
  def include? v
124
- if @sort
125
- @a.include?(form_key(v))
126
- else
127
- @a.include?(v)
128
- end
124
+ if @sort
125
+ @a.include?(form_key(v))
126
+ else
127
+ @a.include?(v)
128
+ end
129
129
  end
130
130
 
131
131
  # Arity of -1 is needed when a block is passed using to_proc, e.g. map(&:some_method).
@@ -16,7 +16,7 @@ module ActiveFacts
16
16
  # Adapter module to add value_type to all potential value classes
17
17
  module ValueClass #:nodoc:
18
18
  def value_type *args, &block #:nodoc:
19
- # The inclusion of instance methods triggers ClassMethods to be included in the class too
19
+ # The inclusion of instance methods triggers ClassMethods to be included in the class too
20
20
  include ActiveFacts::API::Value
21
21
  value_type(*args, &block)
22
22
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveFacts
2
2
  module API
3
- VERSION = "1.9.5"
3
+ VERSION = "1.9.6"
4
4
  end
5
5
  end
@@ -32,8 +32,14 @@ module ActiveFacts
32
32
  __bind(camel)
33
33
  c
34
34
  else
35
- klass = const_get(camel) rescue nil # NameError if undefined
36
- klass && klass.modspace == self ? klass : nil
35
+ if constants.include?(camel.to_sym)
36
+ if klass = const_get(camel) and !klass.respond_to?(:vocabulary)
37
+ raise CrossVocabularyRoleException.new(name, self)
38
+ end
39
+ klass
40
+ else
41
+ nil
42
+ end
37
43
  end
38
44
  end
39
45
 
@@ -49,11 +55,11 @@ module ActiveFacts
49
55
  def verbalise
50
56
  "Vocabulary #{name}:\n\t" +
51
57
  @object_type.keys.sort.map do |object_type|
52
- c = @object_type[object_type]
53
- __bind(c.basename)
54
- c.verbalise + "\n\t\t// Roles played: " + c.all_role.verbalise
55
- end.
56
- join("\n\t")
58
+ c = @object_type[object_type]
59
+ __bind(c.basename)
60
+ c.verbalise + "\n\t\t// Roles played: " + c.all_role.verbalise
61
+ end.
62
+ join("\n\t")
57
63
  end
58
64
 
59
65
  def __add_object_type(klass) #:nodoc:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activefacts-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.5
4
+ version: 1.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clifford Heath