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.
- checksums.yaml +4 -4
- data/lib/activefacts/api/constellation.rb +195 -195
- data/lib/activefacts/api/date.rb +8 -8
- data/lib/activefacts/api/entity.rb +228 -228
- data/lib/activefacts/api/exceptions.rb +13 -13
- data/lib/activefacts/api/fact_type.rb +5 -5
- data/lib/activefacts/api/guid.rb +9 -9
- data/lib/activefacts/api/instance.rb +93 -88
- data/lib/activefacts/api/instance_index.rb +52 -52
- data/lib/activefacts/api/numeric.rb +14 -14
- data/lib/activefacts/api/object_type.rb +231 -231
- data/lib/activefacts/api/role_values.rb +68 -68
- data/lib/activefacts/api/standard_types.rb +1 -1
- data/lib/activefacts/api/version.rb +1 -1
- data/lib/activefacts/api/vocabulary.rb +13 -7
- metadata +1 -1
@@ -12,48 +12,48 @@ module ActiveFacts
|
|
12
12
|
attr_accessor :sort
|
13
13
|
attr_accessor :index_roles
|
14
14
|
def object_type
|
15
|
-
|
15
|
+
@role.object_type
|
16
16
|
end
|
17
17
|
|
18
18
|
def initialize role, excluded_role = nil
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
48
|
+
@sort ? @a.values : @a
|
49
49
|
end
|
50
50
|
|
51
51
|
def to_ary
|
52
|
-
|
52
|
+
to_a
|
53
53
|
end
|
54
54
|
|
55
55
|
def keys
|
56
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
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
|
-
|
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
|
@@ -32,8 +32,14 @@ module ActiveFacts
|
|
32
32
|
__bind(camel)
|
33
33
|
c
|
34
34
|
else
|
35
|
-
|
36
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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:
|