sbf-dm-core 1.3.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +29 -0
- data/.document +5 -0
- data/.gitignore +44 -0
- data/.rspec +1 -0
- data/.rubocop.yml +468 -0
- data/.travis.yml +57 -0
- data/.yardopts +1 -0
- data/Gemfile +70 -0
- data/LICENSE +20 -0
- data/README.md +269 -0
- data/Rakefile +4 -0
- data/dm-core.gemspec +21 -0
- data/lib/dm-core/adapters/abstract_adapter.rb +233 -0
- data/lib/dm-core/adapters/in_memory_adapter.rb +110 -0
- data/lib/dm-core/adapters.rb +249 -0
- data/lib/dm-core/associations/many_to_many.rb +477 -0
- data/lib/dm-core/associations/many_to_one.rb +282 -0
- data/lib/dm-core/associations/one_to_many.rb +332 -0
- data/lib/dm-core/associations/one_to_one.rb +84 -0
- data/lib/dm-core/associations/relationship.rb +650 -0
- data/lib/dm-core/backwards.rb +11 -0
- data/lib/dm-core/collection.rb +1486 -0
- data/lib/dm-core/core_ext/kernel.rb +21 -0
- data/lib/dm-core/core_ext/pathname.rb +4 -0
- data/lib/dm-core/core_ext/symbol.rb +10 -0
- data/lib/dm-core/identity_map.rb +6 -0
- data/lib/dm-core/model/hook.rb +99 -0
- data/lib/dm-core/model/is.rb +30 -0
- data/lib/dm-core/model/property.rb +244 -0
- data/lib/dm-core/model/relationship.rb +366 -0
- data/lib/dm-core/model/scope.rb +87 -0
- data/lib/dm-core/model.rb +876 -0
- data/lib/dm-core/property/binary.rb +19 -0
- data/lib/dm-core/property/boolean.rb +35 -0
- data/lib/dm-core/property/class.rb +23 -0
- data/lib/dm-core/property/date.rb +45 -0
- data/lib/dm-core/property/date_time.rb +44 -0
- data/lib/dm-core/property/decimal.rb +47 -0
- data/lib/dm-core/property/discriminator.rb +40 -0
- data/lib/dm-core/property/float.rb +27 -0
- data/lib/dm-core/property/integer.rb +32 -0
- data/lib/dm-core/property/invalid_value_error.rb +17 -0
- data/lib/dm-core/property/lookup.rb +26 -0
- data/lib/dm-core/property/numeric.rb +35 -0
- data/lib/dm-core/property/object.rb +33 -0
- data/lib/dm-core/property/serial.rb +13 -0
- data/lib/dm-core/property/string.rb +47 -0
- data/lib/dm-core/property/text.rb +12 -0
- data/lib/dm-core/property/time.rb +46 -0
- data/lib/dm-core/property/typecast/numeric.rb +32 -0
- data/lib/dm-core/property/typecast/time.rb +33 -0
- data/lib/dm-core/property.rb +856 -0
- data/lib/dm-core/property_set.rb +177 -0
- data/lib/dm-core/query/conditions/comparison.rb +886 -0
- data/lib/dm-core/query/conditions/operation.rb +710 -0
- data/lib/dm-core/query/direction.rb +33 -0
- data/lib/dm-core/query/operator.rb +34 -0
- data/lib/dm-core/query/path.rb +113 -0
- data/lib/dm-core/query/sort.rb +38 -0
- data/lib/dm-core/query.rb +1352 -0
- data/lib/dm-core/relationship_set.rb +69 -0
- data/lib/dm-core/repository.rb +226 -0
- data/lib/dm-core/resource/persistence_state/clean.rb +36 -0
- data/lib/dm-core/resource/persistence_state/deleted.rb +26 -0
- data/lib/dm-core/resource/persistence_state/dirty.rb +91 -0
- data/lib/dm-core/resource/persistence_state/immutable.rb +32 -0
- data/lib/dm-core/resource/persistence_state/persisted.rb +25 -0
- data/lib/dm-core/resource/persistence_state/transient.rb +87 -0
- data/lib/dm-core/resource/persistence_state.rb +70 -0
- data/lib/dm-core/resource.rb +1220 -0
- data/lib/dm-core/spec/lib/adapter_helpers.rb +63 -0
- data/lib/dm-core/spec/lib/collection_helpers.rb +21 -0
- data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
- data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
- data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
- data/lib/dm-core/spec/setup.rb +164 -0
- data/lib/dm-core/spec/shared/adapter_spec.rb +366 -0
- data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
- data/lib/dm-core/spec/shared/resource_spec.rb +1221 -0
- data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
- data/lib/dm-core/spec/shared/semipublic/property_spec.rb +184 -0
- data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
- data/lib/dm-core/support/assertions.rb +8 -0
- data/lib/dm-core/support/chainable.rb +18 -0
- data/lib/dm-core/support/deprecate.rb +12 -0
- data/lib/dm-core/support/descendant_set.rb +89 -0
- data/lib/dm-core/support/equalizer.rb +48 -0
- data/lib/dm-core/support/ext/array.rb +22 -0
- data/lib/dm-core/support/ext/blank.rb +25 -0
- data/lib/dm-core/support/ext/hash.rb +67 -0
- data/lib/dm-core/support/ext/module.rb +47 -0
- data/lib/dm-core/support/ext/object.rb +57 -0
- data/lib/dm-core/support/ext/string.rb +24 -0
- data/lib/dm-core/support/ext/try_dup.rb +12 -0
- data/lib/dm-core/support/hook.rb +388 -0
- data/lib/dm-core/support/inflections.rb +60 -0
- data/lib/dm-core/support/inflector/inflections.rb +211 -0
- data/lib/dm-core/support/inflector/methods.rb +151 -0
- data/lib/dm-core/support/lazy_array.rb +451 -0
- data/lib/dm-core/support/local_object_space.rb +13 -0
- data/lib/dm-core/support/logger.rb +201 -0
- data/lib/dm-core/support/mash.rb +176 -0
- data/lib/dm-core/support/naming_conventions.rb +109 -0
- data/lib/dm-core/support/ordered_set.rb +381 -0
- data/lib/dm-core/support/subject.rb +33 -0
- data/lib/dm-core/support/subject_set.rb +251 -0
- data/lib/dm-core/version.rb +3 -0
- data/lib/dm-core.rb +274 -0
- data/script/performance.rb +275 -0
- data/script/profile.rb +218 -0
- data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
- data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +69 -0
- data/spec/public/associations/many_to_many_spec.rb +197 -0
- data/spec/public/associations/many_to_one_spec.rb +83 -0
- data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
- data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
- data/spec/public/associations/one_to_many_spec.rb +81 -0
- data/spec/public/associations/one_to_one_spec.rb +176 -0
- data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
- data/spec/public/collection_spec.rb +69 -0
- data/spec/public/finalize_spec.rb +77 -0
- data/spec/public/model/hook_spec.rb +245 -0
- data/spec/public/model/property_spec.rb +91 -0
- data/spec/public/model/relationship_spec.rb +1040 -0
- data/spec/public/model_spec.rb +456 -0
- data/spec/public/property/binary_spec.rb +43 -0
- data/spec/public/property/boolean_spec.rb +21 -0
- data/spec/public/property/class_spec.rb +27 -0
- data/spec/public/property/date_spec.rb +21 -0
- data/spec/public/property/date_time_spec.rb +21 -0
- data/spec/public/property/decimal_spec.rb +23 -0
- data/spec/public/property/discriminator_spec.rb +134 -0
- data/spec/public/property/float_spec.rb +22 -0
- data/spec/public/property/integer_spec.rb +22 -0
- data/spec/public/property/object_spec.rb +117 -0
- data/spec/public/property/serial_spec.rb +22 -0
- data/spec/public/property/string_spec.rb +21 -0
- data/spec/public/property/text_spec.rb +62 -0
- data/spec/public/property/time_spec.rb +21 -0
- data/spec/public/property_spec.rb +333 -0
- data/spec/public/resource/state_spec.rb +72 -0
- data/spec/public/resource_spec.rb +289 -0
- data/spec/public/sel_spec.rb +53 -0
- data/spec/public/setup_spec.rb +145 -0
- data/spec/public/shared/association_collection_shared_spec.rb +309 -0
- data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
- data/spec/public/shared/collection_shared_spec.rb +1637 -0
- data/spec/public/shared/finder_shared_spec.rb +1647 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
- data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
- data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
- data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
- data/spec/semipublic/associations/relationship_spec.rb +200 -0
- data/spec/semipublic/associations_spec.rb +177 -0
- data/spec/semipublic/collection_spec.rb +110 -0
- data/spec/semipublic/model_spec.rb +96 -0
- data/spec/semipublic/property/binary_spec.rb +13 -0
- data/spec/semipublic/property/boolean_spec.rb +47 -0
- data/spec/semipublic/property/class_spec.rb +33 -0
- data/spec/semipublic/property/date_spec.rb +43 -0
- data/spec/semipublic/property/date_time_spec.rb +46 -0
- data/spec/semipublic/property/decimal_spec.rb +83 -0
- data/spec/semipublic/property/discriminator_spec.rb +19 -0
- data/spec/semipublic/property/float_spec.rb +82 -0
- data/spec/semipublic/property/integer_spec.rb +82 -0
- data/spec/semipublic/property/lookup_spec.rb +29 -0
- data/spec/semipublic/property/serial_spec.rb +13 -0
- data/spec/semipublic/property/string_spec.rb +13 -0
- data/spec/semipublic/property/text_spec.rb +31 -0
- data/spec/semipublic/property/time_spec.rb +50 -0
- data/spec/semipublic/property_spec.rb +114 -0
- data/spec/semipublic/query/conditions/comparison_spec.rb +1502 -0
- data/spec/semipublic/query/conditions/operation_spec.rb +1296 -0
- data/spec/semipublic/query/path_spec.rb +471 -0
- data/spec/semipublic/query_spec.rb +3665 -0
- data/spec/semipublic/resource/state/clean_spec.rb +89 -0
- data/spec/semipublic/resource/state/deleted_spec.rb +79 -0
- data/spec/semipublic/resource/state/dirty_spec.rb +163 -0
- data/spec/semipublic/resource/state/immutable_spec.rb +107 -0
- data/spec/semipublic/resource/state/transient_spec.rb +163 -0
- data/spec/semipublic/resource/state_spec.rb +230 -0
- data/spec/semipublic/resource_spec.rb +23 -0
- data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +198 -0
- data/spec/semipublic/shared/resource_state_shared_spec.rb +91 -0
- data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/core_ext/hash.rb +10 -0
- data/spec/support/core_ext/inheritable_attributes.rb +46 -0
- data/spec/support/properties/huge_integer.rb +17 -0
- data/spec/unit/array_spec.rb +23 -0
- data/spec/unit/blank_spec.rb +73 -0
- data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
- data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
- data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
- data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
- data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
- data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
- data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
- data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
- data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
- data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
- data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
- data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
- data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
- data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
- data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
- data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
- data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
- data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
- data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
- data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
- data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
- data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
- data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
- data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
- data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
- data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
- data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
- data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
- data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
- data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
- data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
- data/spec/unit/hash_spec.rb +27 -0
- data/spec/unit/hook_spec.rb +1216 -0
- data/spec/unit/inflections_spec.rb +14 -0
- data/spec/unit/lazy_array_spec.rb +1949 -0
- data/spec/unit/mash_spec.rb +289 -0
- data/spec/unit/module_spec.rb +70 -0
- data/spec/unit/object_spec.rb +38 -0
- data/spec/unit/try_dup_spec.rb +46 -0
- data/tasks/ci.rake +1 -0
- data/tasks/spec.rake +18 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +323 -0
@@ -0,0 +1,366 @@
|
|
1
|
+
# TODO: update Model#respond_to? to return true if method_method missing
|
2
|
+
# would handle the message
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
module Model
|
6
|
+
module Relationship
|
7
|
+
Model.append_extensions self
|
8
|
+
|
9
|
+
include DataMapper::Assertions
|
10
|
+
|
11
|
+
# Initializes relationships hash for extended model
|
12
|
+
# class.
|
13
|
+
#
|
14
|
+
# When model calls has n, has 1 or belongs_to, relationships
|
15
|
+
# are stored in that hash: keys are repository names and
|
16
|
+
# values are relationship sets.
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
def self.extended(model)
|
20
|
+
model.instance_variable_set(:@relationships, {})
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
# When DataMapper model is inherited, relationships
|
25
|
+
# of parent are duplicated and copied to subclass model
|
26
|
+
#
|
27
|
+
# @api private
|
28
|
+
def inherited(model)
|
29
|
+
model.instance_variable_set(:@relationships, {})
|
30
|
+
|
31
|
+
@relationships.each do |repository_name, relationships|
|
32
|
+
model_relationships = model.relationships(repository_name)
|
33
|
+
relationships.each { |relationship| model_relationships << relationship }
|
34
|
+
end
|
35
|
+
|
36
|
+
super
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns copy of relationships set in given repository.
|
40
|
+
#
|
41
|
+
# @param [Symbol] repository_name
|
42
|
+
# Name of the repository for which relationships set is returned
|
43
|
+
# @return [RelationshipSet] relationships set for given repository
|
44
|
+
#
|
45
|
+
# @api semipublic
|
46
|
+
def relationships(repository_name = default_repository_name)
|
47
|
+
# TODO: create RelationshipSet#copy that will copy the relationships, but assign the
|
48
|
+
# new Relationship objects to a supplied repository and model. dup does not really
|
49
|
+
# do what is needed
|
50
|
+
|
51
|
+
default_repository_name = self.default_repository_name
|
52
|
+
|
53
|
+
@relationships[repository_name] ||= if repository_name == default_repository_name
|
54
|
+
RelationshipSet.new
|
55
|
+
else
|
56
|
+
relationships(default_repository_name).dup
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Used to express unlimited cardinality of association,
|
61
|
+
# see +has+
|
62
|
+
#
|
63
|
+
# @api public
|
64
|
+
def n
|
65
|
+
Infinity
|
66
|
+
end
|
67
|
+
|
68
|
+
# A shorthand, clear syntax for defining one-to-one, one-to-many and
|
69
|
+
# many-to-many resource relationships.
|
70
|
+
#
|
71
|
+
# * has 1, :friend # one friend
|
72
|
+
# * has n, :friends # many friends
|
73
|
+
# * has 1..3, :friends # many friends (at least 1, at most 3)
|
74
|
+
# * has 3, :friends # many friends (exactly 3)
|
75
|
+
# * has 1, :friend, 'User' # one friend with the class User
|
76
|
+
# * has 3, :friends, :through => :friendships # many friends through the friendships relationship
|
77
|
+
#
|
78
|
+
# @param cardinality [Integer, Range, Infinity]
|
79
|
+
# cardinality that defines the association type and constraints
|
80
|
+
# @param name [Symbol]
|
81
|
+
# the name that the association will be referenced by
|
82
|
+
# @param *args [Model, Hash] model and/or options hash
|
83
|
+
#
|
84
|
+
# @option *args :through[Symbol] A association that this join should go through to form
|
85
|
+
# a many-to-many association
|
86
|
+
# @option *args :model[Model, String] The name of the class to associate with, if omitted
|
87
|
+
# then the association name is assumed to match the class name
|
88
|
+
# @option *args :repository[Symbol] name of child model repository
|
89
|
+
#
|
90
|
+
# @return [Association::Relationship] the relationship that was
|
91
|
+
# created to reflect either a one-to-one, one-to-many or many-to-many
|
92
|
+
# relationship
|
93
|
+
# @raise [ArgumentError] if the cardinality was not understood. Should be a
|
94
|
+
# Integer, Range or Infinity(n)
|
95
|
+
#
|
96
|
+
# @api public
|
97
|
+
def has(cardinality, name, *args)
|
98
|
+
name = name.to_sym
|
99
|
+
model = extract_model(args)
|
100
|
+
options = extract_options(args)
|
101
|
+
|
102
|
+
min, max = extract_min_max(cardinality)
|
103
|
+
options.update(min: min, max: max)
|
104
|
+
|
105
|
+
assert_valid_options(options)
|
106
|
+
|
107
|
+
raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument' if options.key?(:model) && model
|
108
|
+
|
109
|
+
model ||= options.delete(:model)
|
110
|
+
|
111
|
+
repository_name = repository.name
|
112
|
+
|
113
|
+
# TODO: change to :target_respository_name and :source_repository_name
|
114
|
+
options[:child_repository_name] = options.delete(:repository)
|
115
|
+
options[:parent_repository_name] = repository_name
|
116
|
+
|
117
|
+
klass = if max > 1
|
118
|
+
options.key?(:through) ? Associations::ManyToMany::Relationship : Associations::OneToMany::Relationship
|
119
|
+
else
|
120
|
+
Associations::OneToOne::Relationship
|
121
|
+
end
|
122
|
+
|
123
|
+
relationship = klass.new(name, model, self, options)
|
124
|
+
|
125
|
+
relationships(repository_name) << relationship
|
126
|
+
|
127
|
+
descendants.each do |descendant|
|
128
|
+
descendant.relationships(repository_name) << relationship
|
129
|
+
end
|
130
|
+
|
131
|
+
create_relationship_reader(relationship)
|
132
|
+
create_relationship_writer(relationship)
|
133
|
+
|
134
|
+
relationship
|
135
|
+
end
|
136
|
+
|
137
|
+
# A shorthand, clear syntax for defining many-to-one resource relationships.
|
138
|
+
#
|
139
|
+
# * belongs_to :user # many to one user
|
140
|
+
# * belongs_to :friend, :model => 'User' # many to one friend
|
141
|
+
# * belongs_to :reference, :repository => :pubmed # association for repository other than default
|
142
|
+
#
|
143
|
+
# @param name [Symbol]
|
144
|
+
# the name that the association will be referenced by
|
145
|
+
# @param *args [Model, Hash] model and/or options hash
|
146
|
+
#
|
147
|
+
# @option *args :model[Model, String] The name of the class to associate with, if omitted
|
148
|
+
# then the association name is assumed to match the class name
|
149
|
+
# @option *args :repository[Symbol] name of child model repository
|
150
|
+
#
|
151
|
+
# @return [Association::Relationship] The association created
|
152
|
+
# should not be accessed directly
|
153
|
+
#
|
154
|
+
# @api public
|
155
|
+
def belongs_to(name, *args)
|
156
|
+
name = name.to_sym
|
157
|
+
model_name = self.name
|
158
|
+
model = extract_model(args)
|
159
|
+
options = extract_options(args)
|
160
|
+
|
161
|
+
if options.key?(:through)
|
162
|
+
raise "#{model_name}#belongs_to with :through is deprecated, use 'has 1, :#{name}, " \
|
163
|
+
"#{options.inspect}' in #{model_name} instead (#{caller.first})"
|
164
|
+
elsif options.key?(:model) && model
|
165
|
+
raise ArgumentError, 'should not specify options[:model] if passing the model in the third argument'
|
166
|
+
end
|
167
|
+
|
168
|
+
assert_valid_options(options)
|
169
|
+
|
170
|
+
model ||= options.delete(:model)
|
171
|
+
|
172
|
+
repository_name = repository.name
|
173
|
+
|
174
|
+
# TODO: change to source_repository_name and target_respository_name
|
175
|
+
options[:child_repository_name] = repository_name
|
176
|
+
options[:parent_repository_name] = options.delete(:repository)
|
177
|
+
|
178
|
+
relationship = Associations::ManyToOne::Relationship.new(name, self, model, options)
|
179
|
+
|
180
|
+
relationships(repository_name) << relationship
|
181
|
+
|
182
|
+
descendants.each do |descendant|
|
183
|
+
descendant.relationships(repository_name) << relationship
|
184
|
+
end
|
185
|
+
|
186
|
+
create_relationship_reader(relationship)
|
187
|
+
create_relationship_writer(relationship)
|
188
|
+
|
189
|
+
relationship
|
190
|
+
end
|
191
|
+
|
192
|
+
# Extract the model from an Array of arguments
|
193
|
+
#
|
194
|
+
# @param [Array(Model, String, Hash)]
|
195
|
+
# The arguments passed to an relationship declaration
|
196
|
+
#
|
197
|
+
# @return [Model, #to_str]
|
198
|
+
# target model for the association
|
199
|
+
#
|
200
|
+
# @api private
|
201
|
+
private def extract_model(args)
|
202
|
+
model = args.first
|
203
|
+
|
204
|
+
if model.is_a?(Model)
|
205
|
+
model
|
206
|
+
elsif model.respond_to?(:to_str)
|
207
|
+
model.to_str
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Extract the model from an Array of arguments
|
212
|
+
#
|
213
|
+
# @param [Array(Model, String, Hash)]
|
214
|
+
# The arguments passed to an relationship declaration
|
215
|
+
#
|
216
|
+
# @return [Hash]
|
217
|
+
# options for the association
|
218
|
+
#
|
219
|
+
# @api private
|
220
|
+
private def extract_options(args)
|
221
|
+
options = args.last
|
222
|
+
options.respond_to?(:to_hash) ? options.to_hash.dup : {}
|
223
|
+
end
|
224
|
+
|
225
|
+
# A support method for converting Integer, Range or Infinity values into two
|
226
|
+
# values representing the minimum and maximum cardinality of the association
|
227
|
+
#
|
228
|
+
# @return [Array] A pair of integers, min and max
|
229
|
+
#
|
230
|
+
# @api private
|
231
|
+
private def extract_min_max(cardinality)
|
232
|
+
case cardinality
|
233
|
+
when Integer then [cardinality, cardinality]
|
234
|
+
when Range then [cardinality.first, cardinality.last]
|
235
|
+
when Infinity then [0, Infinity]
|
236
|
+
else
|
237
|
+
assert_kind_of 'options', options, Integer, Range, Infinity.class
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
# Validates options of association method like belongs_to or has:
|
242
|
+
# verifies types of cardinality bounds, repository, association class,
|
243
|
+
# keys and possible values of :through option.
|
244
|
+
#
|
245
|
+
# @api private
|
246
|
+
private def assert_valid_options(options)
|
247
|
+
# TODO: update to match Query#assert_valid_options
|
248
|
+
# - perform options normalization elsewhere
|
249
|
+
|
250
|
+
if options.key?(:min) && options.key?(:max)
|
251
|
+
min = options[:min]
|
252
|
+
max = options[:max]
|
253
|
+
|
254
|
+
min = min.to_int unless min == Infinity
|
255
|
+
max = max.to_int unless max == Infinity
|
256
|
+
|
257
|
+
if min == Infinity && max == Infinity
|
258
|
+
raise ArgumentError, 'Cardinality may not be n..n. The cardinality specifies the min/max number of results from the association'
|
259
|
+
elsif min > max
|
260
|
+
raise ArgumentError, "Cardinality min (#{min}) cannot be larger than the max (#{max})"
|
261
|
+
elsif min < 0
|
262
|
+
raise ArgumentError, "Cardinality min much be greater than or equal to 0, but was #{min}"
|
263
|
+
elsif max < 1
|
264
|
+
raise ArgumentError, "Cardinality max much be greater than or equal to 1, but was #{max}"
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
options[:repository] = options[:repository].to_sym if options.key?(:repository)
|
269
|
+
|
270
|
+
if options.key?(:class_name)
|
271
|
+
raise "+options[:class_name]+ is deprecated, use :model instead (#{caller[1]})"
|
272
|
+
elsif options.key?(:remote_name)
|
273
|
+
raise "+options[:remote_name]+ is deprecated, use :via instead (#{caller[1]})"
|
274
|
+
end
|
275
|
+
|
276
|
+
assert_kind_of 'options[:through]', options[:through], Symbol, Module if options.key?(:through)
|
277
|
+
|
278
|
+
%i(via inverse).each do |key|
|
279
|
+
assert_kind_of "options[#{key.inspect}]", options[key], Symbol, Associations::Relationship if options.key?(key)
|
280
|
+
end
|
281
|
+
|
282
|
+
# TODO: deprecate :child_key and :parent_key in favor of :source_key and
|
283
|
+
# :target_key (will mean something different for each relationship)
|
284
|
+
|
285
|
+
%i(child_key parent_key).each do |key|
|
286
|
+
options[key] = Array(options[key]) if options.key?(key)
|
287
|
+
end
|
288
|
+
|
289
|
+
raise ArgumentError, '+options[:limit]+ should not be specified on a relationship' if options.key?(:limit)
|
290
|
+
end
|
291
|
+
|
292
|
+
# Defines the anonymous module that is used to add relationships.
|
293
|
+
# Using a single module here prevents having a very large number
|
294
|
+
# of anonymous modules, where each property has their own module.
|
295
|
+
# @api private
|
296
|
+
private def relationship_module
|
297
|
+
@relationship_module ||= begin
|
298
|
+
mod = Module.new
|
299
|
+
class_eval do
|
300
|
+
include mod
|
301
|
+
end
|
302
|
+
mod
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# Dynamically defines reader method
|
307
|
+
#
|
308
|
+
# @api private
|
309
|
+
private def create_relationship_reader(relationship)
|
310
|
+
name = relationship.name
|
311
|
+
reader_name = name.to_s
|
312
|
+
|
313
|
+
return if method_defined?(reader_name)
|
314
|
+
|
315
|
+
reader_visibility = relationship.reader_visibility
|
316
|
+
|
317
|
+
relationship_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
318
|
+
#{reader_visibility}
|
319
|
+
def #{reader_name}(query = nil)
|
320
|
+
# TODO: when no query is passed in, return the results from
|
321
|
+
# the ivar directly. This will require that the ivar
|
322
|
+
# actually hold the resource/collection, and in the case
|
323
|
+
# of 1:1, the underlying collection is hidden in a
|
324
|
+
# private ivar, and the resource is in a known ivar
|
325
|
+
|
326
|
+
persistence_state.get(relationships[#{name.inspect}], query)
|
327
|
+
end
|
328
|
+
RUBY
|
329
|
+
end
|
330
|
+
|
331
|
+
# Dynamically defines writer method
|
332
|
+
#
|
333
|
+
# @api private
|
334
|
+
private def create_relationship_writer(relationship)
|
335
|
+
name = relationship.name
|
336
|
+
writer_name = "#{name}="
|
337
|
+
|
338
|
+
return if method_defined?(writer_name)
|
339
|
+
|
340
|
+
writer_visibility = relationship.writer_visibility
|
341
|
+
|
342
|
+
relationship_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
343
|
+
#{writer_visibility}
|
344
|
+
def #{writer_name}(target)
|
345
|
+
relationship = relationships[#{name.inspect}]
|
346
|
+
self.persistence_state = persistence_state.set(relationship, target)
|
347
|
+
persistence_state.get(relationship)
|
348
|
+
end
|
349
|
+
RUBY
|
350
|
+
end
|
351
|
+
|
352
|
+
# @api public
|
353
|
+
private def respond_to_missing?(method, include_private)
|
354
|
+
relationships(repository_name)[method] || super
|
355
|
+
end
|
356
|
+
|
357
|
+
private def method_missing(method, *args, &block)
|
358
|
+
if (relationship = relationships(repository_name)[method])
|
359
|
+
return Query::Path.new([relationship])
|
360
|
+
end
|
361
|
+
|
362
|
+
super
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
366
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Model
|
3
|
+
# Module with query scoping functionality.
|
4
|
+
#
|
5
|
+
# Scopes are implemented using simple array based
|
6
|
+
# stack that is thread local. Default scope can be set
|
7
|
+
# on a per repository basis.
|
8
|
+
#
|
9
|
+
# Scopes are merged as new queries are nested.
|
10
|
+
# It is also possible to get exclusive scope access
|
11
|
+
# using +with_exclusive_scope+
|
12
|
+
module Scope
|
13
|
+
# @api private
|
14
|
+
def default_scope(repository_name = default_repository_name)
|
15
|
+
@default_scope ||= {}
|
16
|
+
|
17
|
+
default_repository_name = self.default_repository_name
|
18
|
+
|
19
|
+
@default_scope[repository_name] ||= if repository_name == default_repository_name
|
20
|
+
{}
|
21
|
+
else
|
22
|
+
default_scope(default_repository_name).dup
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns query on top of scope stack
|
27
|
+
#
|
28
|
+
# @api private
|
29
|
+
def query
|
30
|
+
repository.new_query(self, current_scope).freeze
|
31
|
+
end
|
32
|
+
|
33
|
+
# @api private
|
34
|
+
def current_scope
|
35
|
+
scope_stack.last || default_scope(repository.name)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Pushes given query on top of the stack
|
39
|
+
#
|
40
|
+
# @param [Hash, Query] query to add to current scope nesting
|
41
|
+
#
|
42
|
+
# @api private
|
43
|
+
protected def with_scope(query)
|
44
|
+
options = if query.is_a?(Hash)
|
45
|
+
query
|
46
|
+
else
|
47
|
+
query.options
|
48
|
+
end
|
49
|
+
|
50
|
+
# merge the current scope with the passed in query
|
51
|
+
with_exclusive_scope(self.query.merge(options)) { |*block_args| yield(*block_args) }
|
52
|
+
end
|
53
|
+
|
54
|
+
# Pushes given query on top of scope stack and yields
|
55
|
+
# given block, then pops the stack. During block execution
|
56
|
+
# queries previously pushed onto the stack
|
57
|
+
# have no effect.
|
58
|
+
#
|
59
|
+
# @api private
|
60
|
+
protected def with_exclusive_scope(query)
|
61
|
+
query = if query.is_a?(Hash)
|
62
|
+
repository.new_query(self, query)
|
63
|
+
else
|
64
|
+
query.dup
|
65
|
+
end
|
66
|
+
|
67
|
+
scope_stack = self.scope_stack
|
68
|
+
scope_stack << query.options
|
69
|
+
|
70
|
+
begin
|
71
|
+
yield query.freeze
|
72
|
+
ensure
|
73
|
+
scope_stack.pop
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Initializes (if necessary) and returns current scope stack
|
78
|
+
# @api private
|
79
|
+
protected def scope_stack
|
80
|
+
scope_stack_for = Thread.current[:dm_scope_stack] ||= {}
|
81
|
+
scope_stack_for[object_id] ||= []
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
include Scope
|
86
|
+
end
|
87
|
+
end
|