dm-core 0.9.11 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +17 -14
- data/.gitignore +3 -1
- data/FAQ +6 -5
- data/History.txt +5 -50
- data/Manifest.txt +66 -76
- data/QUICKLINKS +1 -1
- data/README.txt +21 -15
- data/Rakefile +6 -7
- data/SPECS +2 -29
- data/TODO +1 -1
- data/deps.rip +2 -0
- data/dm-core.gemspec +11 -15
- data/lib/dm-core.rb +105 -110
- data/lib/dm-core/adapters.rb +135 -16
- data/lib/dm-core/adapters/abstract_adapter.rb +251 -181
- data/lib/dm-core/adapters/data_objects_adapter.rb +482 -534
- data/lib/dm-core/adapters/in_memory_adapter.rb +90 -69
- data/lib/dm-core/adapters/mysql_adapter.rb +22 -115
- data/lib/dm-core/adapters/oracle_adapter.rb +249 -0
- data/lib/dm-core/adapters/postgres_adapter.rb +7 -173
- data/lib/dm-core/adapters/sqlite3_adapter.rb +4 -97
- data/lib/dm-core/adapters/yaml_adapter.rb +116 -0
- data/lib/dm-core/associations/many_to_many.rb +372 -90
- data/lib/dm-core/associations/many_to_one.rb +220 -73
- data/lib/dm-core/associations/one_to_many.rb +319 -255
- data/lib/dm-core/associations/one_to_one.rb +66 -53
- data/lib/dm-core/associations/relationship.rb +561 -156
- data/lib/dm-core/collection.rb +1101 -379
- data/lib/dm-core/core_ext/kernel.rb +12 -0
- data/lib/dm-core/core_ext/symbol.rb +10 -0
- data/lib/dm-core/identity_map.rb +4 -34
- data/lib/dm-core/migrations.rb +1283 -0
- data/lib/dm-core/model.rb +570 -369
- data/lib/dm-core/model/descendant_set.rb +81 -0
- data/lib/dm-core/model/hook.rb +45 -0
- data/lib/dm-core/model/is.rb +32 -0
- data/lib/dm-core/model/property.rb +247 -0
- data/lib/dm-core/model/relationship.rb +335 -0
- data/lib/dm-core/model/scope.rb +90 -0
- data/lib/dm-core/property.rb +808 -273
- data/lib/dm-core/property_set.rb +141 -98
- data/lib/dm-core/query.rb +1037 -483
- data/lib/dm-core/query/conditions/comparison.rb +872 -0
- data/lib/dm-core/query/conditions/operation.rb +221 -0
- data/lib/dm-core/query/direction.rb +43 -0
- data/lib/dm-core/query/operator.rb +84 -0
- data/lib/dm-core/query/path.rb +138 -0
- data/lib/dm-core/query/sort.rb +45 -0
- data/lib/dm-core/repository.rb +210 -94
- data/lib/dm-core/resource.rb +641 -421
- data/lib/dm-core/spec/adapter_shared_spec.rb +294 -0
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +106 -0
- data/lib/dm-core/support/chainable.rb +22 -0
- data/lib/dm-core/support/deprecate.rb +12 -0
- data/lib/dm-core/support/logger.rb +13 -0
- data/lib/dm-core/{naming_conventions.rb → support/naming_conventions.rb} +6 -6
- data/lib/dm-core/transaction.rb +333 -92
- data/lib/dm-core/type.rb +98 -60
- data/lib/dm-core/types/boolean.rb +1 -1
- data/lib/dm-core/types/discriminator.rb +34 -20
- data/lib/dm-core/types/object.rb +7 -4
- data/lib/dm-core/types/paranoid_boolean.rb +11 -9
- data/lib/dm-core/types/paranoid_datetime.rb +11 -9
- data/lib/dm-core/types/serial.rb +3 -3
- data/lib/dm-core/types/text.rb +3 -4
- data/lib/dm-core/version.rb +1 -1
- data/script/performance.rb +102 -109
- data/script/profile.rb +169 -38
- data/spec/lib/adapter_helpers.rb +105 -0
- data/spec/lib/collection_helpers.rb +18 -0
- data/spec/lib/counter_adapter.rb +34 -0
- data/spec/lib/pending_helpers.rb +27 -0
- data/spec/lib/rspec_immediate_feedback_formatter.rb +53 -0
- data/spec/public/associations/many_to_many_spec.rb +193 -0
- data/spec/public/associations/many_to_one_spec.rb +73 -0
- data/spec/public/associations/one_to_many_spec.rb +77 -0
- data/spec/public/associations/one_to_one_spec.rb +156 -0
- data/spec/public/collection_spec.rb +65 -0
- data/spec/public/migrations_spec.rb +359 -0
- data/spec/public/model/relationship_spec.rb +924 -0
- data/spec/public/model_spec.rb +159 -0
- data/spec/public/property_spec.rb +829 -0
- data/spec/public/resource_spec.rb +71 -0
- data/spec/public/sel_spec.rb +44 -0
- data/spec/public/setup_spec.rb +145 -0
- data/spec/public/shared/association_collection_shared_spec.rb +317 -0
- data/spec/public/shared/collection_shared_spec.rb +1670 -0
- data/spec/public/shared/finder_shared_spec.rb +1619 -0
- data/spec/public/shared/resource_shared_spec.rb +924 -0
- data/spec/public/shared/sel_shared_spec.rb +112 -0
- data/spec/public/transaction_spec.rb +129 -0
- data/spec/public/types/discriminator_spec.rb +130 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
- data/spec/semipublic/adapters/mysql_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/oracle_adapter_spec.rb +194 -0
- data/spec/semipublic/adapters/postgres_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/yaml_adapter_spec.rb +12 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +53 -0
- data/spec/semipublic/associations/relationship_spec.rb +194 -0
- data/spec/semipublic/associations_spec.rb +177 -0
- data/spec/semipublic/collection_spec.rb +142 -0
- data/spec/semipublic/property_spec.rb +61 -0
- data/spec/semipublic/query/conditions_spec.rb +528 -0
- data/spec/semipublic/query/path_spec.rb +443 -0
- data/spec/semipublic/query_spec.rb +2626 -0
- data/spec/semipublic/resource_spec.rb +47 -0
- data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +126 -0
- data/spec/spec.opts +3 -1
- data/spec/spec_helper.rb +80 -57
- data/tasks/ci.rb +19 -31
- data/tasks/dm.rb +43 -48
- data/tasks/doc.rb +8 -11
- data/tasks/gemspec.rb +5 -5
- data/tasks/hoe.rb +15 -16
- data/tasks/install.rb +8 -10
- metadata +74 -111
- data/lib/dm-core/associations.rb +0 -207
- data/lib/dm-core/associations/relationship_chain.rb +0 -81
- data/lib/dm-core/auto_migrations.rb +0 -105
- data/lib/dm-core/dependency_queue.rb +0 -32
- data/lib/dm-core/hook.rb +0 -11
- data/lib/dm-core/is.rb +0 -16
- data/lib/dm-core/logger.rb +0 -232
- data/lib/dm-core/migrations/destructive_migrations.rb +0 -17
- data/lib/dm-core/migrator.rb +0 -29
- data/lib/dm-core/scope.rb +0 -58
- data/lib/dm-core/support.rb +0 -7
- data/lib/dm-core/support/array.rb +0 -13
- data/lib/dm-core/support/assertions.rb +0 -8
- data/lib/dm-core/support/errors.rb +0 -23
- data/lib/dm-core/support/kernel.rb +0 -11
- data/lib/dm-core/support/symbol.rb +0 -41
- data/lib/dm-core/type_map.rb +0 -80
- data/lib/dm-core/types.rb +0 -19
- data/script/all +0 -4
- data/spec/integration/association_spec.rb +0 -1382
- data/spec/integration/association_through_spec.rb +0 -203
- data/spec/integration/associations/many_to_many_spec.rb +0 -449
- data/spec/integration/associations/many_to_one_spec.rb +0 -163
- data/spec/integration/associations/one_to_many_spec.rb +0 -188
- data/spec/integration/auto_migrations_spec.rb +0 -413
- data/spec/integration/collection_spec.rb +0 -1073
- data/spec/integration/data_objects_adapter_spec.rb +0 -32
- data/spec/integration/dependency_queue_spec.rb +0 -46
- data/spec/integration/model_spec.rb +0 -197
- data/spec/integration/mysql_adapter_spec.rb +0 -85
- data/spec/integration/postgres_adapter_spec.rb +0 -731
- data/spec/integration/property_spec.rb +0 -253
- data/spec/integration/query_spec.rb +0 -514
- data/spec/integration/repository_spec.rb +0 -61
- data/spec/integration/resource_spec.rb +0 -513
- data/spec/integration/sqlite3_adapter_spec.rb +0 -352
- data/spec/integration/sti_spec.rb +0 -273
- data/spec/integration/strategic_eager_loading_spec.rb +0 -156
- data/spec/integration/transaction_spec.rb +0 -75
- data/spec/integration/type_spec.rb +0 -275
- data/spec/lib/logging_helper.rb +0 -18
- data/spec/lib/mock_adapter.rb +0 -27
- data/spec/lib/model_loader.rb +0 -100
- data/spec/lib/publicize_methods.rb +0 -28
- data/spec/models/content.rb +0 -16
- data/spec/models/vehicles.rb +0 -34
- data/spec/models/zoo.rb +0 -48
- data/spec/unit/adapters/abstract_adapter_spec.rb +0 -133
- data/spec/unit/adapters/adapter_shared_spec.rb +0 -15
- data/spec/unit/adapters/data_objects_adapter_spec.rb +0 -632
- data/spec/unit/adapters/in_memory_adapter_spec.rb +0 -98
- data/spec/unit/adapters/postgres_adapter_spec.rb +0 -133
- data/spec/unit/associations/many_to_many_spec.rb +0 -32
- data/spec/unit/associations/many_to_one_spec.rb +0 -159
- data/spec/unit/associations/one_to_many_spec.rb +0 -393
- data/spec/unit/associations/one_to_one_spec.rb +0 -7
- data/spec/unit/associations/relationship_spec.rb +0 -71
- data/spec/unit/associations_spec.rb +0 -242
- data/spec/unit/auto_migrations_spec.rb +0 -111
- data/spec/unit/collection_spec.rb +0 -182
- data/spec/unit/data_mapper_spec.rb +0 -35
- data/spec/unit/identity_map_spec.rb +0 -126
- data/spec/unit/is_spec.rb +0 -80
- data/spec/unit/migrator_spec.rb +0 -33
- data/spec/unit/model_spec.rb +0 -321
- data/spec/unit/naming_conventions_spec.rb +0 -36
- data/spec/unit/property_set_spec.rb +0 -90
- data/spec/unit/property_spec.rb +0 -753
- data/spec/unit/query_spec.rb +0 -571
- data/spec/unit/repository_spec.rb +0 -93
- data/spec/unit/resource_spec.rb +0 -649
- data/spec/unit/scope_spec.rb +0 -142
- data/spec/unit/transaction_spec.rb +0 -493
- data/spec/unit/type_map_spec.rb +0 -114
- data/spec/unit/type_spec.rb +0 -119
@@ -1,81 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Associations
|
3
|
-
class RelationshipChain < Relationship
|
4
|
-
OPTIONS = [
|
5
|
-
:repository_name, :near_relationship_name, :remote_relationship_name,
|
6
|
-
:child_model, :parent_model, :parent_key, :child_key,
|
7
|
-
:min, :max
|
8
|
-
]
|
9
|
-
|
10
|
-
undef_method :get_parent
|
11
|
-
undef_method :attach_parent
|
12
|
-
|
13
|
-
# @api private
|
14
|
-
def child_model
|
15
|
-
near_relationship.child_model
|
16
|
-
end
|
17
|
-
|
18
|
-
# @api private
|
19
|
-
def get_children(parent, options = {}, finder = :all, *args)
|
20
|
-
query = @query.merge(options).merge(child_key.to_query(parent_key.get(parent)))
|
21
|
-
|
22
|
-
query[:links] = links
|
23
|
-
query[:unique] = true
|
24
|
-
|
25
|
-
with_repository(parent) do
|
26
|
-
results = grandchild_model.send(finder, *(args << query))
|
27
|
-
# FIXME: remove the need for the uniq.freeze
|
28
|
-
finder == :all ? (@mutable ? results : results.freeze) : results
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
# @api private
|
35
|
-
def initialize(options)
|
36
|
-
if (missing_options = OPTIONS - [ :min, :max ] - options.keys ).any?
|
37
|
-
raise ArgumentError, "The options #{missing_options * ', '} are required", caller
|
38
|
-
end
|
39
|
-
|
40
|
-
@repository_name = options.fetch(:repository_name)
|
41
|
-
@near_relationship_name = options.fetch(:near_relationship_name)
|
42
|
-
@remote_relationship_name = options.fetch(:remote_relationship_name)
|
43
|
-
@child_model = options.fetch(:child_model)
|
44
|
-
@parent_model = options.fetch(:parent_model)
|
45
|
-
@parent_properties = options.fetch(:parent_key)
|
46
|
-
@child_properties = options.fetch(:child_key)
|
47
|
-
@mutable = options.delete(:mutable) || false
|
48
|
-
|
49
|
-
@name = near_relationship.name
|
50
|
-
@query = options.reject{ |key,val| OPTIONS.include?(key) }
|
51
|
-
@extra_links = []
|
52
|
-
@options = options
|
53
|
-
end
|
54
|
-
|
55
|
-
# @api private
|
56
|
-
def near_relationship
|
57
|
-
parent_model.relationships[@near_relationship_name]
|
58
|
-
end
|
59
|
-
|
60
|
-
# @api private
|
61
|
-
def links
|
62
|
-
if remote_relationship.kind_of?(RelationshipChain)
|
63
|
-
remote_relationship.send(:links) + [ remote_relationship.send(:near_relationship) ]
|
64
|
-
else
|
65
|
-
[ remote_relationship ]
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# @api private
|
70
|
-
def remote_relationship
|
71
|
-
near_relationship.child_model.relationships[@remote_relationship_name] ||
|
72
|
-
near_relationship.child_model.relationships[@remote_relationship_name.to_s.singularize.to_sym]
|
73
|
-
end
|
74
|
-
|
75
|
-
# @api private
|
76
|
-
def grandchild_model
|
77
|
-
Class === @child_model ? @child_model : (Class === @parent_model ? @parent_model.find_const(@child_model) : Object.find_const(@child_model))
|
78
|
-
end
|
79
|
-
end # class Relationship
|
80
|
-
end # module Associations
|
81
|
-
end # module DataMapper
|
@@ -1,105 +0,0 @@
|
|
1
|
-
# TODO: move to dm-more/dm-migrations
|
2
|
-
|
3
|
-
module DataMapper
|
4
|
-
class AutoMigrator
|
5
|
-
##
|
6
|
-
# Destructively automigrates the data-store to match the model.
|
7
|
-
# First migrates all models down and then up.
|
8
|
-
# REPEAT: THIS IS DESTRUCTIVE
|
9
|
-
#
|
10
|
-
# @param Symbol repository_name the repository to be migrated
|
11
|
-
def self.auto_migrate(repository_name = nil, *descendants)
|
12
|
-
auto_migrate_down(repository_name, *descendants)
|
13
|
-
auto_migrate_up(repository_name, *descendants)
|
14
|
-
end
|
15
|
-
|
16
|
-
##
|
17
|
-
# Destructively automigrates the data-store down
|
18
|
-
# REPEAT: THIS IS DESTRUCTIVE
|
19
|
-
#
|
20
|
-
# @param Symbol repository_name the repository to be migrated
|
21
|
-
# @calls DataMapper::Resource#auto_migrate_down!
|
22
|
-
# @api private
|
23
|
-
def self.auto_migrate_down(repository_name = nil, *descendants)
|
24
|
-
descendants = DataMapper::Resource.descendants.to_a if descendants.empty?
|
25
|
-
descendants.reverse.each do |model|
|
26
|
-
model.auto_migrate_down!(repository_name)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
##
|
31
|
-
# Automigrates the data-store up
|
32
|
-
#
|
33
|
-
# @param Symbol repository_name the repository to be migrated
|
34
|
-
# @calls DataMapper::Resource#auto_migrate_up!
|
35
|
-
# @api private
|
36
|
-
def self.auto_migrate_up(repository_name = nil, *descendants)
|
37
|
-
descendants = DataMapper::Resource.descendants.to_a if descendants.empty?
|
38
|
-
descendants.each do |model|
|
39
|
-
model.auto_migrate_up!(repository_name)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
##
|
44
|
-
# Safely migrates the data-store to match the model
|
45
|
-
# preserving data already in the data-store
|
46
|
-
#
|
47
|
-
# @param Symbol repository_name the repository to be migrated
|
48
|
-
# @calls DataMapper::Resource#auto_upgrade!
|
49
|
-
def self.auto_upgrade(repository_name = nil)
|
50
|
-
DataMapper::Resource.descendants.each do |model|
|
51
|
-
model.auto_upgrade!(repository_name)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end # class AutoMigrator
|
55
|
-
|
56
|
-
module AutoMigrations
|
57
|
-
##
|
58
|
-
# Destructively automigrates the data-store to match the model
|
59
|
-
# REPEAT: THIS IS DESTRUCTIVE
|
60
|
-
#
|
61
|
-
# @param Symbol repository_name the repository to be migrated
|
62
|
-
def auto_migrate!(repository_name = self.repository_name)
|
63
|
-
auto_migrate_down!(repository_name)
|
64
|
-
auto_migrate_up!(repository_name)
|
65
|
-
end
|
66
|
-
|
67
|
-
##
|
68
|
-
# Destructively migrates the data-store down, which basically
|
69
|
-
# deletes all the models.
|
70
|
-
# REPEAT: THIS IS DESTRUCTIVE
|
71
|
-
#
|
72
|
-
# @param Symbol repository_name the repository to be migrated
|
73
|
-
# @api private
|
74
|
-
def auto_migrate_down!(repository_name = self.repository_name)
|
75
|
-
# repository_name ||= default_repository_name
|
76
|
-
repository(repository_name) do |r|
|
77
|
-
r.adapter.destroy_model_storage(r, self.base_model)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
##
|
82
|
-
# Auto migrates the data-store to match the model
|
83
|
-
#
|
84
|
-
# @param Symbol repository_name the repository to be migrated
|
85
|
-
# @api private
|
86
|
-
def auto_migrate_up!(repository_name = self.repository_name)
|
87
|
-
repository(repository_name) do |r|
|
88
|
-
r.adapter.create_model_storage(r, self.base_model)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
##
|
93
|
-
# Safely migrates the data-store to match the model
|
94
|
-
# preserving data already in the data-store
|
95
|
-
#
|
96
|
-
# @param Symbol repository_name the repository to be migrated
|
97
|
-
def auto_upgrade!(repository_name = self.repository_name)
|
98
|
-
repository(repository_name) do |r|
|
99
|
-
r.adapter.upgrade_model_storage(r, self)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
Model.send(:include, self)
|
104
|
-
end # module AutoMigrations
|
105
|
-
end # module DataMapper
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
##
|
3
|
-
#
|
4
|
-
# DataMapper's DependencyQueue is used to store callbacks for classes which
|
5
|
-
# may or may not be loaded already.
|
6
|
-
#
|
7
|
-
class DependencyQueue
|
8
|
-
def initialize
|
9
|
-
@dependencies = {}
|
10
|
-
end
|
11
|
-
|
12
|
-
def add(class_name, &callback)
|
13
|
-
@dependencies[class_name] ||= []
|
14
|
-
@dependencies[class_name] << callback
|
15
|
-
resolve!
|
16
|
-
end
|
17
|
-
|
18
|
-
def resolve!
|
19
|
-
@dependencies.each do |class_name, callbacks|
|
20
|
-
begin
|
21
|
-
klass = Object.find_const(class_name)
|
22
|
-
callbacks.each do |callback|
|
23
|
-
callback.call(klass)
|
24
|
-
end
|
25
|
-
callbacks.clear
|
26
|
-
rescue NameError
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end # class DependencyQueue
|
32
|
-
end # module DataMapper
|
data/lib/dm-core/hook.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Hook
|
3
|
-
def self.included(model)
|
4
|
-
model.class_eval <<-EOS, __FILE__, __LINE__
|
5
|
-
include Extlib::Hook
|
6
|
-
register_instance_hooks :save, :create, :update, :destroy
|
7
|
-
EOS
|
8
|
-
end
|
9
|
-
end
|
10
|
-
DataMapper::Resource.append_inclusions Hook
|
11
|
-
end # module DataMapper
|
data/lib/dm-core/is.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
module Is
|
3
|
-
|
4
|
-
def is(plugin, *pars, &block)
|
5
|
-
generator_method = "is_#{plugin}".to_sym
|
6
|
-
|
7
|
-
if self.respond_to?(generator_method)
|
8
|
-
self.send(generator_method, *pars, &block)
|
9
|
-
else
|
10
|
-
raise PluginNotFoundError, "could not find plugin named #{plugin}"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
Model.send(:include, self)
|
15
|
-
end # module Is
|
16
|
-
end # module DataMapper
|
data/lib/dm-core/logger.rb
DELETED
@@ -1,232 +0,0 @@
|
|
1
|
-
require "time" # httpdate
|
2
|
-
# ==== Public DataMapper Logger API
|
3
|
-
#
|
4
|
-
# Logger taken from Merb :)
|
5
|
-
#
|
6
|
-
# To replace an existing logger with a new one:
|
7
|
-
# DataMapper.logger.set_log(log{String, IO},level{Symbol, String})
|
8
|
-
#
|
9
|
-
# Available logging levels are:
|
10
|
-
# :off, :fatal, :error, :warn, :info, :debug
|
11
|
-
#
|
12
|
-
# Logging via:
|
13
|
-
# DataMapper.logger.fatal(message<String>)
|
14
|
-
# DataMapper.logger.error(message<String>)
|
15
|
-
# DataMapper.logger.warn(message<String>)
|
16
|
-
# DataMapper.logger.info(message<String>)
|
17
|
-
# DataMapper.logger.debug(message<String>)
|
18
|
-
#
|
19
|
-
# Flush the buffer to
|
20
|
-
# DataMapper.logger.flush
|
21
|
-
#
|
22
|
-
# Remove the current log object
|
23
|
-
# DataMapper.logger.close
|
24
|
-
#
|
25
|
-
# ==== Private DataMapper Logger API
|
26
|
-
#
|
27
|
-
# To initialize the logger you create a new object, proxies to set_log.
|
28
|
-
# DataMapper::Logger.new(log{String, IO}, level{Symbol, String})
|
29
|
-
#
|
30
|
-
# Logger will not create the file until something is actually logged
|
31
|
-
# This avoids file creation on DataMapper init when it creates the
|
32
|
-
# default logger.
|
33
|
-
module DataMapper
|
34
|
-
|
35
|
-
class << self #:nodoc:
|
36
|
-
attr_accessor :logger
|
37
|
-
end
|
38
|
-
|
39
|
-
class Logger
|
40
|
-
|
41
|
-
attr_accessor :aio
|
42
|
-
attr_accessor :delimiter
|
43
|
-
attr_reader :level
|
44
|
-
attr_reader :buffer
|
45
|
-
attr_reader :log
|
46
|
-
|
47
|
-
# @note
|
48
|
-
# Ruby (standard) logger levels:
|
49
|
-
# off: absolutely nothing
|
50
|
-
# fatal: an unhandleable error that results in a program crash
|
51
|
-
# error: a handleable error condition
|
52
|
-
# warn: a warning
|
53
|
-
# info: generic (useful) information about system operation
|
54
|
-
# debug: low-level information for developers
|
55
|
-
#
|
56
|
-
# DataMapper::Logger::LEVELS[:off, :fatal, :error, :warn, :info, :debug]
|
57
|
-
LEVELS =
|
58
|
-
{
|
59
|
-
:off => 99999,
|
60
|
-
:fatal => 7,
|
61
|
-
:error => 6,
|
62
|
-
:warn => 4,
|
63
|
-
:info => 3,
|
64
|
-
:debug => 0
|
65
|
-
}
|
66
|
-
|
67
|
-
def level=(new_level)
|
68
|
-
@level = LEVELS[new_level.to_sym]
|
69
|
-
reset_methods(:close)
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
# The idea here is that instead of performing an 'if' conditional check on
|
75
|
-
# each logging we do it once when the log object is setup
|
76
|
-
def set_write_method
|
77
|
-
@log.instance_eval do
|
78
|
-
|
79
|
-
# Determine if asynchronous IO can be used
|
80
|
-
def aio?
|
81
|
-
@aio = !RUBY_PLATFORM.match(/java|mswin/) &&
|
82
|
-
!(@log == STDOUT) &&
|
83
|
-
@log.respond_to?(:write_nonblock)
|
84
|
-
end
|
85
|
-
|
86
|
-
# Define the write method based on if aio an be used
|
87
|
-
undef write_method if defined? write_method
|
88
|
-
if aio?
|
89
|
-
alias :write_method :write_nonblock
|
90
|
-
else
|
91
|
-
alias :write_method :write
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def initialize_log(log)
|
97
|
-
close if @log # be sure that we don't leave open files laying around.
|
98
|
-
@log = log || "log/dm.log"
|
99
|
-
end
|
100
|
-
|
101
|
-
def reset_methods(o_or_c)
|
102
|
-
if o_or_c == :open
|
103
|
-
alias internal_push push_opened
|
104
|
-
elsif o_or_c == :close
|
105
|
-
alias internal_push push_closed
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def push_opened(string)
|
110
|
-
message = Time.now.httpdate
|
111
|
-
message << delimiter
|
112
|
-
message << string
|
113
|
-
message << "\n" unless message[-1] == ?\n
|
114
|
-
@buffer << message
|
115
|
-
flush # Force a flush for now until we figure out where we want to use the buffering.
|
116
|
-
end
|
117
|
-
|
118
|
-
def push_closed(string)
|
119
|
-
unless @log.respond_to?(:write)
|
120
|
-
log = Pathname(@log)
|
121
|
-
log.dirname.mkpath
|
122
|
-
@log = log.open('a')
|
123
|
-
@log.sync = true
|
124
|
-
end
|
125
|
-
set_write_method
|
126
|
-
reset_methods(:open)
|
127
|
-
push(string)
|
128
|
-
end
|
129
|
-
|
130
|
-
alias internal_push push_closed
|
131
|
-
|
132
|
-
def prep_msg(message, level)
|
133
|
-
level << delimiter << message
|
134
|
-
end
|
135
|
-
|
136
|
-
public
|
137
|
-
|
138
|
-
# To initialize the logger you create a new object, proxies to set_log.
|
139
|
-
# DataMapper::Logger.new(log{String, IO},level{Symbol, String})
|
140
|
-
#
|
141
|
-
# @param log<IO,String> either an IO object or a name of a logfile.
|
142
|
-
# @param log_level<String> the message string to be logged
|
143
|
-
# @param delimiter<String> delimiter to use between message sections
|
144
|
-
# @param log_creation<Boolean> log that the file is being created
|
145
|
-
def initialize(*args)
|
146
|
-
set_log(*args)
|
147
|
-
end
|
148
|
-
|
149
|
-
# To replace an existing logger with a new one:
|
150
|
-
# DataMapper.logger.set_log(log{String, IO},level{Symbol, String})
|
151
|
-
#
|
152
|
-
# @param log<IO,String> either an IO object or a name of a logfile.
|
153
|
-
# @param log_level<Symbol> a symbol representing the log level from
|
154
|
-
# {:off, :fatal, :error, :warn, :info, :debug}
|
155
|
-
# @param delimiter<String> delimiter to use between message sections
|
156
|
-
# @param log_creation<Boolean> log that the file is being created
|
157
|
-
def set_log(log, log_level = :off, delimiter = " ~ ", log_creation = false)
|
158
|
-
delimiter ||= " ~ "
|
159
|
-
|
160
|
-
if log_level && LEVELS[log_level.to_sym]
|
161
|
-
self.level = log_level.to_sym
|
162
|
-
else
|
163
|
-
self.level = :debug
|
164
|
-
end
|
165
|
-
|
166
|
-
@buffer = []
|
167
|
-
@delimiter = delimiter
|
168
|
-
|
169
|
-
initialize_log(log)
|
170
|
-
|
171
|
-
DataMapper.logger = self
|
172
|
-
|
173
|
-
self.info("Logfile created") if log_creation
|
174
|
-
end
|
175
|
-
|
176
|
-
# Flush the entire buffer to the log object.
|
177
|
-
# DataMapper.logger.flush
|
178
|
-
#
|
179
|
-
def flush
|
180
|
-
return unless @buffer.size > 0
|
181
|
-
@log.write_method(@buffer.slice!(0..-1).join)
|
182
|
-
end
|
183
|
-
|
184
|
-
# Close and remove the current log object.
|
185
|
-
# DataMapper.logger.close
|
186
|
-
#
|
187
|
-
def close
|
188
|
-
flush
|
189
|
-
@log.close if @log.respond_to?(:close)
|
190
|
-
@log = nil
|
191
|
-
end
|
192
|
-
|
193
|
-
# Appends a string and log level to logger's buffer.
|
194
|
-
|
195
|
-
# @note
|
196
|
-
# Note that the string is discarded if the string's log level less than the
|
197
|
-
# logger's log level.
|
198
|
-
# @note
|
199
|
-
# Note that if the logger is aio capable then the logger will use
|
200
|
-
# non-blocking asynchronous writes.
|
201
|
-
#
|
202
|
-
# @param level<Fixnum> the logging level as an integer
|
203
|
-
# @param string<String> the message string to be logged
|
204
|
-
def push(string)
|
205
|
-
internal_push(string)
|
206
|
-
end
|
207
|
-
alias << push
|
208
|
-
|
209
|
-
# Generate the following logging methods for DataMapper.logger as described
|
210
|
-
# in the API:
|
211
|
-
# :fatal, :error, :warn, :info, :debug
|
212
|
-
# :off only gets a off? method
|
213
|
-
LEVELS.each_pair do |name, number|
|
214
|
-
unless name.to_s == 'off'
|
215
|
-
class_eval <<-EOS, __FILE__, __LINE__
|
216
|
-
# DOC
|
217
|
-
def #{name}(message)
|
218
|
-
self.<<( prep_msg(message, "#{name}") ) if #{name}?
|
219
|
-
end
|
220
|
-
EOS
|
221
|
-
end
|
222
|
-
|
223
|
-
class_eval <<-EOS, __FILE__, __LINE__
|
224
|
-
# DOC
|
225
|
-
def #{name}?
|
226
|
-
#{number} >= level
|
227
|
-
end
|
228
|
-
EOS
|
229
|
-
end
|
230
|
-
|
231
|
-
end # class Logger
|
232
|
-
end # module DataMapper
|