datamapper 0.3.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/Manifest.txt +5 -0
- data/README.txt +11 -0
- data/Rakefile +70 -0
- data/lib/datamapper.rb +8 -0
- metadata +152 -319
- data/CHANGELOG +0 -145
- data/FAQ +0 -96
- data/MIT-LICENSE +0 -22
- data/QUICKLINKS +0 -12
- data/README +0 -105
- data/environment.rb +0 -62
- data/example.rb +0 -156
- data/lib/data_mapper.rb +0 -88
- data/lib/data_mapper/adapters/abstract_adapter.rb +0 -43
- data/lib/data_mapper/adapters/data_object_adapter.rb +0 -480
- data/lib/data_mapper/adapters/mysql_adapter.rb +0 -72
- data/lib/data_mapper/adapters/postgresql_adapter.rb +0 -258
- data/lib/data_mapper/adapters/sql/coersion.rb +0 -134
- data/lib/data_mapper/adapters/sql/commands/load_command.rb +0 -545
- data/lib/data_mapper/adapters/sql/mappings/associations_set.rb +0 -34
- data/lib/data_mapper/adapters/sql/mappings/column.rb +0 -279
- data/lib/data_mapper/adapters/sql/mappings/conditions.rb +0 -172
- data/lib/data_mapper/adapters/sql/mappings/schema.rb +0 -60
- data/lib/data_mapper/adapters/sql/mappings/table.rb +0 -459
- data/lib/data_mapper/adapters/sql/quoting.rb +0 -24
- data/lib/data_mapper/adapters/sqlite3_adapter.rb +0 -159
- data/lib/data_mapper/associations.rb +0 -106
- data/lib/data_mapper/associations/belongs_to_association.rb +0 -160
- data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +0 -437
- data/lib/data_mapper/associations/has_many_association.rb +0 -283
- data/lib/data_mapper/associations/has_n_association.rb +0 -143
- data/lib/data_mapper/associations/reference.rb +0 -47
- data/lib/data_mapper/attributes.rb +0 -73
- data/lib/data_mapper/auto_migrations.rb +0 -36
- data/lib/data_mapper/base.rb +0 -17
- data/lib/data_mapper/callbacks.rb +0 -107
- data/lib/data_mapper/context.rb +0 -112
- data/lib/data_mapper/database.rb +0 -234
- data/lib/data_mapper/dependency_queue.rb +0 -28
- data/lib/data_mapper/embedded_value.rb +0 -145
- data/lib/data_mapper/identity_map.rb +0 -47
- data/lib/data_mapper/is/tree.rb +0 -121
- data/lib/data_mapper/migration.rb +0 -155
- data/lib/data_mapper/persistence.rb +0 -852
- data/lib/data_mapper/property.rb +0 -310
- data/lib/data_mapper/query.rb +0 -164
- data/lib/data_mapper/support/blank.rb +0 -35
- data/lib/data_mapper/support/connection_pool.rb +0 -117
- data/lib/data_mapper/support/enumerable.rb +0 -35
- data/lib/data_mapper/support/errors.rb +0 -16
- data/lib/data_mapper/support/inflector.rb +0 -265
- data/lib/data_mapper/support/object.rb +0 -54
- data/lib/data_mapper/support/serialization.rb +0 -96
- data/lib/data_mapper/support/silence.rb +0 -10
- data/lib/data_mapper/support/string.rb +0 -72
- data/lib/data_mapper/support/struct.rb +0 -7
- data/lib/data_mapper/support/symbol.rb +0 -82
- data/lib/data_mapper/support/typed_set.rb +0 -65
- data/lib/data_mapper/types/base.rb +0 -44
- data/lib/data_mapper/types/string.rb +0 -34
- data/lib/data_mapper/validatable_extensions/errors.rb +0 -12
- data/lib/data_mapper/validatable_extensions/macros.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validatable_instance_methods.rb +0 -62
- data/lib/data_mapper/validatable_extensions/validation_base.rb +0 -18
- data/lib/data_mapper/validatable_extensions/validations/formats/email.rb +0 -43
- data/lib/data_mapper/validatable_extensions/validations/validates_acceptance_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_confirmation_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_each.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_format_of.rb +0 -28
- data/lib/data_mapper/validatable_extensions/validations/validates_length_of.rb +0 -15
- data/lib/data_mapper/validatable_extensions/validations/validates_numericality_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_presence_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_true_for.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_uniqueness_of.rb +0 -40
- data/lib/data_mapper/validations.rb +0 -20
- data/lib/data_mapper/validations/number_validator.rb +0 -40
- data/lib/data_mapper/validations/string_validator.rb +0 -20
- data/lib/data_mapper/validations/validator.rb +0 -13
- data/performance.rb +0 -307
- data/plugins/can_has_sphinx/LICENSE +0 -23
- data/plugins/can_has_sphinx/README +0 -4
- data/plugins/can_has_sphinx/REVISION +0 -1
- data/plugins/can_has_sphinx/Rakefile +0 -22
- data/plugins/can_has_sphinx/init.rb +0 -1
- data/plugins/can_has_sphinx/install.rb +0 -1
- data/plugins/can_has_sphinx/lib/acts_as_sphinx.rb +0 -123
- data/plugins/can_has_sphinx/lib/sphinx.rb +0 -460
- data/plugins/can_has_sphinx/scripts/sphinx.sh +0 -47
- data/plugins/can_has_sphinx/tasks/acts_as_sphinx_tasks.rake +0 -41
- data/profile_data_mapper.rb +0 -40
- data/rakefile.rb +0 -159
- data/spec/acts_as_tree_spec.rb +0 -67
- data/spec/adapters/data_object_adapter_spec.rb +0 -31
- data/spec/associations/belongs_to_association_spec.rb +0 -98
- data/spec/associations/has_and_belongs_to_many_association_spec.rb +0 -377
- data/spec/associations/has_many_association_spec.rb +0 -337
- data/spec/attributes_spec.rb +0 -52
- data/spec/auto_migrations_spec.rb +0 -101
- data/spec/callbacks_spec.rb +0 -186
- data/spec/can_has_sphinx.rb +0 -5
- data/spec/coersion_spec.rb +0 -41
- data/spec/column_spec.rb +0 -114
- data/spec/count_command_spec.rb +0 -45
- data/spec/database_spec.rb +0 -18
- data/spec/dataobjects_spec.rb +0 -27
- data/spec/delete_command_spec.rb +0 -11
- data/spec/dependency_spec.rb +0 -29
- data/spec/embedded_value_spec.rb +0 -161
- data/spec/fixtures/animals.yaml +0 -33
- data/spec/fixtures/animals_exhibits.yaml +0 -2
- data/spec/fixtures/careers.yaml +0 -5
- data/spec/fixtures/comments.yaml +0 -1
- data/spec/fixtures/exhibits.yaml +0 -90
- data/spec/fixtures/fruit.yaml +0 -6
- data/spec/fixtures/people.yaml +0 -37
- data/spec/fixtures/posts.yaml +0 -3
- data/spec/fixtures/projects.yaml +0 -13
- data/spec/fixtures/sections.yaml +0 -5
- data/spec/fixtures/serializers.yaml +0 -6
- data/spec/fixtures/tasks.yaml +0 -6
- data/spec/fixtures/tasks_tasks.yaml +0 -2
- data/spec/fixtures/tomatoes.yaml +0 -1
- data/spec/fixtures/users.yaml +0 -1
- data/spec/fixtures/zoos.yaml +0 -24
- data/spec/is_a_tree_spec.rb +0 -149
- data/spec/legacy_spec.rb +0 -16
- data/spec/load_command_spec.rb +0 -322
- data/spec/magic_columns_spec.rb +0 -26
- data/spec/migration_spec.rb +0 -267
- data/spec/mock_adapter.rb +0 -20
- data/spec/models/animal.rb +0 -12
- data/spec/models/candidate.rb +0 -8
- data/spec/models/career.rb +0 -7
- data/spec/models/chain.rb +0 -8
- data/spec/models/comment.rb +0 -6
- data/spec/models/exhibit.rb +0 -14
- data/spec/models/fence.rb +0 -7
- data/spec/models/fruit.rb +0 -8
- data/spec/models/job.rb +0 -8
- data/spec/models/person.rb +0 -30
- data/spec/models/post.rb +0 -14
- data/spec/models/project.rb +0 -41
- data/spec/models/sales_person.rb +0 -5
- data/spec/models/section.rb +0 -8
- data/spec/models/serializer.rb +0 -5
- data/spec/models/task.rb +0 -9
- data/spec/models/tomato.rb +0 -27
- data/spec/models/user.rb +0 -12
- data/spec/models/zoo.rb +0 -13
- data/spec/natural_key_spec.rb +0 -36
- data/spec/paranoia_spec.rb +0 -38
- data/spec/persistence_spec.rb +0 -479
- data/spec/postgres_spec.rb +0 -96
- data/spec/property_spec.rb +0 -151
- data/spec/query_spec.rb +0 -77
- data/spec/save_command_spec.rb +0 -94
- data/spec/schema_spec.rb +0 -8
- data/spec/serialize_spec.rb +0 -19
- data/spec/single_table_inheritance_spec.rb +0 -43
- data/spec/spec_helper.rb +0 -45
- data/spec/support/blank_spec.rb +0 -8
- data/spec/support/inflector_spec.rb +0 -41
- data/spec/support/object_spec.rb +0 -9
- data/spec/support/serialization_spec.rb +0 -61
- data/spec/support/silence_spec.rb +0 -15
- data/spec/support/string_spec.rb +0 -7
- data/spec/support/struct_spec.rb +0 -12
- data/spec/support/typed_set_spec.rb +0 -66
- data/spec/symbolic_operators_spec.rb +0 -27
- data/spec/table_spec.rb +0 -79
- data/spec/types/string.rb +0 -81
- data/spec/validates_confirmation_of_spec.rb +0 -55
- data/spec/validates_format_of_spec.rb +0 -78
- data/spec/validates_length_of_spec.rb +0 -117
- data/spec/validates_uniqueness_of_spec.rb +0 -92
- data/spec/validations/number_validator.rb +0 -59
- data/spec/validations/string_validator.rb +0 -14
- data/spec/validations_spec.rb +0 -141
- data/tasks/fixtures.rb +0 -53
@@ -1,143 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
|
3
|
-
class ForeignKeyNotFoundError < StandardError; end
|
4
|
-
class AssociationProtectedError < StandardError; end
|
5
|
-
|
6
|
-
module Associations
|
7
|
-
|
8
|
-
class HasNAssociation
|
9
|
-
|
10
|
-
attr_reader :options
|
11
|
-
|
12
|
-
OPTIONS = [
|
13
|
-
:class,
|
14
|
-
:class_name,
|
15
|
-
:foreign_key,
|
16
|
-
:dependent
|
17
|
-
]
|
18
|
-
|
19
|
-
def initialize(klass, association_name, options)
|
20
|
-
@constant = klass
|
21
|
-
@adapter = database.adapter
|
22
|
-
@table = @adapter.table(klass)
|
23
|
-
@association_name = association_name.to_sym
|
24
|
-
@options = options || Hash.new
|
25
|
-
|
26
|
-
define_accessor(klass)
|
27
|
-
|
28
|
-
Persistence::dependencies.add(associated_constant_name) do |klass|
|
29
|
-
@foreign_key_column = associated_table[foreign_key_name]
|
30
|
-
|
31
|
-
unless @foreign_key_column
|
32
|
-
associated_constant.property(foreign_key_name, foreign_key_type)
|
33
|
-
|
34
|
-
@foreign_key_column = associated_table[foreign_key_name]
|
35
|
-
|
36
|
-
if @foreign_key_column.nil?
|
37
|
-
raise ForeignKeyNotFoundError.new(<<-EOS.compress_lines)
|
38
|
-
key_table => #{key_table.inspect},
|
39
|
-
association_table => #{associated_table.inspect},
|
40
|
-
association_name => #{name},
|
41
|
-
foreign_key_name => #{foreign_key_name.inspect},
|
42
|
-
foreign_key_type => #{foreign_key_type.inspect},
|
43
|
-
constant => #{constant.inspect},
|
44
|
-
associated_constant => #{associated_constant.inspect}
|
45
|
-
EOS
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def name
|
52
|
-
@association_name
|
53
|
-
end
|
54
|
-
|
55
|
-
def constant
|
56
|
-
@constant
|
57
|
-
end
|
58
|
-
|
59
|
-
def associated_constant
|
60
|
-
@associated_constant || @associated_constant = Kernel.const_get(associated_constant_name)
|
61
|
-
end
|
62
|
-
|
63
|
-
def associated_constant_name
|
64
|
-
@associated_constant_name || begin
|
65
|
-
|
66
|
-
if @options.has_key?(:class) || @options.has_key?(:class_name)
|
67
|
-
@associated_constant_name = (@options[:class] || @options[:class_name])
|
68
|
-
|
69
|
-
if @associated_constant_name.kind_of?(String)
|
70
|
-
@associated_constant_name = Inflector.classify(@associated_constant_name)
|
71
|
-
elsif @associated_constant_name.kind_of?(Class)
|
72
|
-
@associated_constant_name = @associated_constant_name.name
|
73
|
-
end
|
74
|
-
else
|
75
|
-
@associated_constant_name = Inflector.classify(@association_name)
|
76
|
-
end
|
77
|
-
|
78
|
-
@associated_constant_name
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
def primary_key_column
|
84
|
-
@primary_key_column || @primary_key_column = key_table.key
|
85
|
-
end
|
86
|
-
|
87
|
-
def foreign_key_column
|
88
|
-
@foreign_key_column
|
89
|
-
end
|
90
|
-
|
91
|
-
def foreign_key_name
|
92
|
-
@foreign_key_name || @foreign_key_name = (@options[:foreign_key] || key_table.default_foreign_key)
|
93
|
-
end
|
94
|
-
|
95
|
-
def foreign_key_type
|
96
|
-
@foreign_key_type || @foreign_key_type = key_table.key.type
|
97
|
-
end
|
98
|
-
|
99
|
-
def key_table
|
100
|
-
@key_table || @key_table = @adapter.table(constant)
|
101
|
-
end
|
102
|
-
|
103
|
-
def associated_table
|
104
|
-
@association_table || @association_table = @adapter.table(associated_constant)
|
105
|
-
end
|
106
|
-
|
107
|
-
def associated_columns
|
108
|
-
associated_table.columns.reject { |column| column.lazy? }
|
109
|
-
end
|
110
|
-
|
111
|
-
def complementary_association
|
112
|
-
@complementary_association || begin
|
113
|
-
@complementary_association = associated_table.associations.find do |mapping|
|
114
|
-
mapping.is_a?(BelongsToAssociation) &&
|
115
|
-
mapping.foreign_key_column == foreign_key_column &&
|
116
|
-
mapping.key_table.name == key_table.name
|
117
|
-
end
|
118
|
-
|
119
|
-
if @complementary_association
|
120
|
-
class << self
|
121
|
-
attr_accessor :complementary_association
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
return @complementary_association
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def finder_options
|
130
|
-
@finder_options || @finder_options = @options.reject { |k,v| self.class::OPTIONS.include?(k) }
|
131
|
-
end
|
132
|
-
|
133
|
-
def to_sql
|
134
|
-
"JOIN #{associated_table.to_sql} ON #{foreign_key_column.to_sql(true)} = #{primary_key_column.to_sql(true)}"
|
135
|
-
end
|
136
|
-
|
137
|
-
def activate!(force = false)
|
138
|
-
foreign_key_column
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
end
|
143
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
|
3
|
-
module Associations
|
4
|
-
|
5
|
-
# Reference is an abstract-class providing the boiler-plate for
|
6
|
-
# the association proxies (ie: HasManyAssociation::Set, or
|
7
|
-
# HasOneAssociation::Instance)
|
8
|
-
# The proxies need to access the defining Association instances
|
9
|
-
# to obtain mapping information. This class provides convenient
|
10
|
-
# access to said Association.
|
11
|
-
#
|
12
|
-
# EXAMPLE:
|
13
|
-
# class Zoo
|
14
|
-
# has_many :exhibits
|
15
|
-
# end
|
16
|
-
# The +has_many+ declaration instantiates a
|
17
|
-
# DataMapper::Associations::HasManyAssociation and adds it to the
|
18
|
-
# DataMapper::Adapters::Sql::Mappings::Table#associations array for
|
19
|
-
# the Table representing Zoo.
|
20
|
-
#
|
21
|
-
# Zoo.new.exhibits
|
22
|
-
# +exhibits+ above returns an instance of
|
23
|
-
# DataMapper::Associations::HasManyAssociation::Set. This instance
|
24
|
-
# needs to access the actual HasManyAssociation instance in order
|
25
|
-
# to access the mapping information within. The DataMapper::Associations::Reference
|
26
|
-
# abstract-class for the Set provides the Reference#association method in order to
|
27
|
-
# provide easy access to this information.
|
28
|
-
class Reference
|
29
|
-
|
30
|
-
# +instance+ is a mapped object instance. ie: #<Zoo:0x123456 ...>
|
31
|
-
# +association_name+ is the Symbol used to look up the Association
|
32
|
-
# instance within the DataMapper::Adapters::Sql::Mappings::Table
|
33
|
-
def initialize(instance, association_name)
|
34
|
-
@instance, @association_name = instance, association_name.to_sym
|
35
|
-
@instance.loaded_associations << self
|
36
|
-
end
|
37
|
-
|
38
|
-
# #association provides lazily initialized access to the declared
|
39
|
-
# Association.
|
40
|
-
def association
|
41
|
-
@association || (@association = @instance.database_context.table(@instance.class).associations[@association_name])
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
|
3
|
-
module Attributes
|
4
|
-
|
5
|
-
def self.included(klass)
|
6
|
-
klass.const_set('ATTRIBUTES', Set.new) unless klass.const_defined?('ATTRIBUTES')
|
7
|
-
end
|
8
|
-
|
9
|
-
def attributes
|
10
|
-
__get_attributes(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
# Mass-assign mapped fields.
|
14
|
-
def attributes=(values_hash)
|
15
|
-
__set_attributes(values_hash, true)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def __method_defined?(name, public_only = true)
|
21
|
-
if public_only
|
22
|
-
self.class.public_method_defined?(name)
|
23
|
-
else
|
24
|
-
self.class.private_method_defined?(name) ||
|
25
|
-
self.class.protected_method_defined?(name) ||
|
26
|
-
self.class.public_method_defined?(name)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def __get_attributes(public_only)
|
31
|
-
pairs = {}
|
32
|
-
|
33
|
-
self.class::ATTRIBUTES.each do |name|
|
34
|
-
getter = if __method_defined?(name, public_only)
|
35
|
-
name
|
36
|
-
elsif __method_defined?(name.to_s.ensure_ends_with('?'), public_only)
|
37
|
-
name.to_s.ensure_ends_with('?')
|
38
|
-
else
|
39
|
-
nil
|
40
|
-
end
|
41
|
-
|
42
|
-
if getter
|
43
|
-
value = send(getter)
|
44
|
-
pairs[name] = value.is_a?(Class) ? value.to_s : value
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
pairs
|
49
|
-
end
|
50
|
-
|
51
|
-
def __set_attributes(values_hash, public_only)
|
52
|
-
values_hash.each_pair do |k,v|
|
53
|
-
setter_name = k.to_s.sub(/\?$/, '').ensure_ends_with('=')
|
54
|
-
if __method_defined?(setter_name, public_only)
|
55
|
-
send(setter_name, v)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
self
|
60
|
-
end
|
61
|
-
|
62
|
-
# return all attributes, regardless of their visibility
|
63
|
-
def private_attributes
|
64
|
-
__get_attributes(false)
|
65
|
-
end
|
66
|
-
|
67
|
-
# private method for setting any/all attribute values, regardless of visibility
|
68
|
-
def private_attributes=(values_hash)
|
69
|
-
__set_attributes(values_hash, false)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module AutoMigrations
|
3
|
-
def auto_migrate!
|
4
|
-
if self::subclasses.empty?
|
5
|
-
table = database.table(self)
|
6
|
-
table.activate_associations!
|
7
|
-
|
8
|
-
table.create!(true)
|
9
|
-
else
|
10
|
-
schema = database.schema
|
11
|
-
columns = self::subclasses.inject(schema[self].columns) do |span, subclass|
|
12
|
-
span + schema[subclass].columns
|
13
|
-
end
|
14
|
-
|
15
|
-
table_name = schema[self].name.to_s
|
16
|
-
table = schema[table_name]
|
17
|
-
columns.each do |column|
|
18
|
-
table.add_column(column.name, column.type, column.options)
|
19
|
-
end
|
20
|
-
|
21
|
-
table.activate_associations!
|
22
|
-
|
23
|
-
return table.create!(true)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
def create_table(table)
|
29
|
-
raise NotImplementedError.new
|
30
|
-
end
|
31
|
-
|
32
|
-
def modify_table(table, columns)
|
33
|
-
raise NotImplementedError.new
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/lib/data_mapper/base.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'data_mapper/persistence'
|
2
|
-
require 'data_mapper/is/tree'
|
3
|
-
|
4
|
-
module DataMapper
|
5
|
-
|
6
|
-
class Base
|
7
|
-
include DataMapper::Is::Tree
|
8
|
-
|
9
|
-
def self.inherited(klass)
|
10
|
-
klass.send(:include, DataMapper::Persistence)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.auto_migrate!
|
14
|
-
DataMapper::Persistence.auto_migrate!
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
|
3
|
-
# CallbacksHelper adds a class-method ClassMethods#callbacks
|
4
|
-
# when included in a class, and defines short-cut class-methods to
|
5
|
-
# add delegates to callbacks for the built-in Callbacks::EVENTS.
|
6
|
-
module CallbacksHelper
|
7
|
-
|
8
|
-
# The ::included callback extends the included class with a
|
9
|
-
# ::callbacks method, and sets up helper methods for the standard
|
10
|
-
# events declared in Callbacks::EVENTS.
|
11
|
-
def self.included(base)
|
12
|
-
base.extend(ClassMethods)
|
13
|
-
|
14
|
-
# Declare helpers for the standard EVENTS
|
15
|
-
Callbacks::EVENTS.each do |name|
|
16
|
-
base.class_eval <<-EOS
|
17
|
-
def self.#{name}(string = nil, &block)
|
18
|
-
if string.nil?
|
19
|
-
callbacks.add(:#{name}, block)
|
20
|
-
else
|
21
|
-
callbacks.add(:#{name}, string)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
EOS
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Defines class-methods for the class that CallbacksHelper is
|
29
|
-
# included in.
|
30
|
-
module ClassMethods
|
31
|
-
|
32
|
-
# Provides lazily initialized access to a Callbacks instance.
|
33
|
-
def callbacks
|
34
|
-
@callbacks || ( @callbacks = DataMapper::Callbacks.new )
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Callbacks is a collection to assign and execute blocks of code when
|
40
|
-
# hooks throughout the DataMapper call Callbacks#execute. A set of the
|
41
|
-
# standard callbacks is declared in the Callbacks::EVENTS array.
|
42
|
-
class Callbacks
|
43
|
-
|
44
|
-
# These are a collection of default callbacks that are hooked
|
45
|
-
# into the DataMapper. You're free to add your own events just by
|
46
|
-
# calling #add, but you'll have add the appropriate hooks into the
|
47
|
-
# system to actually execute them yourself.
|
48
|
-
EVENTS = [
|
49
|
-
:before_materialize, :after_materialize,
|
50
|
-
:before_save, :after_save,
|
51
|
-
:before_create, :after_create,
|
52
|
-
:before_update, :after_update,
|
53
|
-
:before_destroy, :after_destroy,
|
54
|
-
:before_validation, :after_validation
|
55
|
-
]
|
56
|
-
|
57
|
-
# Initializes an internal Hash that ensures callback names are always
|
58
|
-
# of type Symbol, and assigns an Array to store your delegating code
|
59
|
-
# when the callback is looked-up by name.
|
60
|
-
def initialize
|
61
|
-
@callbacks = Hash.new do |h,k|
|
62
|
-
raise 'Callback names must be Symbols' unless k.kind_of?(Symbol)
|
63
|
-
h[k] = Set.new
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# Executes a given callback and returns TRUE or FALSE depending on the
|
68
|
-
# return value of the callbacks. All callbacks must return successfully
|
69
|
-
# in order for the call to #execute to return TRUE. Callbacks always
|
70
|
-
# execute against an +instance+. You may pass additional arguments
|
71
|
-
# which will in turn be passed to any Proc objects assigned to a specific
|
72
|
-
# callback. Strings assigned to callbacks do not accept parameters.
|
73
|
-
# They are instance-eval'ed instead. When the callback is a Symbol,
|
74
|
-
# it is sent to the instance under the assumption it is a method call.
|
75
|
-
def execute(name, instance, *args)
|
76
|
-
@callbacks[name].all? do |callback|
|
77
|
-
case callback
|
78
|
-
when String then instance.instance_eval(callback)
|
79
|
-
when Proc then callback[instance, *args]
|
80
|
-
when Symbol then instance.send(callback, *args)
|
81
|
-
else raise ''
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
# Asign delegating code to a callback. The +block+ parameter
|
87
|
-
# can be a Proc object, a String which will be eval'ed when
|
88
|
-
# the callback is executed, or a Symbol, which will be sent to
|
89
|
-
# the instance executed against (as a method call).
|
90
|
-
def add(name, block)
|
91
|
-
callback = @callbacks[name]
|
92
|
-
raise ArgumentError.new("You didn't specify a callback in String, Symbol or Proc form.") unless [String, Proc, Symbol].detect { |type| block.is_a?(type) }
|
93
|
-
callback << block
|
94
|
-
end
|
95
|
-
|
96
|
-
def dup
|
97
|
-
copy = self.class.new
|
98
|
-
@callbacks.each_pair do |name, callbacks|
|
99
|
-
callbacks.each do |callback|
|
100
|
-
copy.add(name, callback)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
return copy
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
data/lib/data_mapper/context.rb
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
require 'data_mapper/identity_map'
|
2
|
-
|
3
|
-
module DataMapper
|
4
|
-
|
5
|
-
class Context
|
6
|
-
|
7
|
-
class MaterializationError < StandardError
|
8
|
-
end
|
9
|
-
|
10
|
-
attr_reader :adapter
|
11
|
-
|
12
|
-
def initialize(adapter)
|
13
|
-
@adapter = adapter
|
14
|
-
end
|
15
|
-
|
16
|
-
def identity_map
|
17
|
-
@identity_map || ( @identity_map = IdentityMap.new )
|
18
|
-
end
|
19
|
-
|
20
|
-
def first(klass, *args)
|
21
|
-
id = nil
|
22
|
-
options = nil
|
23
|
-
table = self.table(klass)
|
24
|
-
key = table.key
|
25
|
-
|
26
|
-
if args.empty? # No id, no options
|
27
|
-
options = { :limit => 1 }
|
28
|
-
elsif args.size == 2 && args.last.kind_of?(Hash) # id AND options
|
29
|
-
options = args.last.merge(key => args.first)
|
30
|
-
elsif args.size == 1 # id OR options
|
31
|
-
if args.first.kind_of?(Hash)
|
32
|
-
options = args.first.merge(:limit => 1) # no id, add limit
|
33
|
-
else
|
34
|
-
options = { key => args.first } # no options, set id
|
35
|
-
end
|
36
|
-
else
|
37
|
-
raise ArgumentError.new('Context#first takes a class, and optional type_or_id and/or options arguments')
|
38
|
-
end
|
39
|
-
|
40
|
-
# Account for undesired behaviour in MySQL that returns the
|
41
|
-
# last inserted row when the WHERE clause contains a "#{primary_key} IS NULL".
|
42
|
-
return nil if options.has_key?(key.name) && options[key.name] == nil
|
43
|
-
|
44
|
-
@adapter.load(self, klass, options).first
|
45
|
-
end
|
46
|
-
|
47
|
-
def get(klass, keys)
|
48
|
-
@adapter.get(self, klass, keys)
|
49
|
-
end
|
50
|
-
|
51
|
-
def all(klass, options = {})
|
52
|
-
@adapter.load(self, klass, options)
|
53
|
-
end
|
54
|
-
|
55
|
-
def count(klass, *args)
|
56
|
-
table(klass).count(*args)
|
57
|
-
end
|
58
|
-
|
59
|
-
def save(instance)
|
60
|
-
@adapter.save(self, instance)
|
61
|
-
end
|
62
|
-
|
63
|
-
def destroy(instance)
|
64
|
-
@adapter.delete(self, instance)
|
65
|
-
end
|
66
|
-
|
67
|
-
def delete_all(klass)
|
68
|
-
@adapter.delete(self, klass)
|
69
|
-
end
|
70
|
-
|
71
|
-
def truncate(klass)
|
72
|
-
table(klass).truncate!
|
73
|
-
end
|
74
|
-
|
75
|
-
def create_table(klass)
|
76
|
-
table(klass).create!
|
77
|
-
end
|
78
|
-
|
79
|
-
def drop_table(klass)
|
80
|
-
table(klass).drop!
|
81
|
-
end
|
82
|
-
|
83
|
-
def table_exists?(klass)
|
84
|
-
table(klass).exists?
|
85
|
-
end
|
86
|
-
|
87
|
-
def column_exists_for_table?(klass, column_name)
|
88
|
-
@adapter.column_exists_for_table?(klass, column_name)
|
89
|
-
end
|
90
|
-
|
91
|
-
def execute(*args)
|
92
|
-
@adapter.execute(*args)
|
93
|
-
end
|
94
|
-
|
95
|
-
def query(*args)
|
96
|
-
@adapter.query(*args)
|
97
|
-
end
|
98
|
-
|
99
|
-
def schema
|
100
|
-
@adapter.schema
|
101
|
-
end
|
102
|
-
|
103
|
-
def table(klass)
|
104
|
-
@adapter.table(klass)
|
105
|
-
end
|
106
|
-
|
107
|
-
def logger
|
108
|
-
@logger || @logger = @adapter.logger
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
end
|