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,62 +0,0 @@
|
|
1
|
-
# = Validations
|
2
|
-
# DataMapper uses the 'Validatable' gem to validate models.
|
3
|
-
#
|
4
|
-
# Example:
|
5
|
-
# class Person < DataMapper::Base
|
6
|
-
# property :name, :string
|
7
|
-
# property :email, :string
|
8
|
-
# property :password, :string
|
9
|
-
#
|
10
|
-
# validates_presence_of :name, :email
|
11
|
-
# validates_length_of :password, :minimum => 6, :on => :create
|
12
|
-
# validates_format_of :email, :with => :email_address, :message => 'Please provide a valid email address.'
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# p = Person.new
|
16
|
-
# p.valid? #=> false
|
17
|
-
# p.errors.full_messages #=> ["Email must not be blank", "Please provide a valid email address.", "Name must not be blank"]
|
18
|
-
#
|
19
|
-
# p.save #=> false
|
20
|
-
# p.errors.full_messages #=> ["Password must be more than 5 characters long", "Email must not be blank",
|
21
|
-
# "Please provide a valid email address.", "Name must not be blank"]
|
22
|
-
#
|
23
|
-
#
|
24
|
-
|
25
|
-
module Validatable
|
26
|
-
alias_method :valid_in_all_cases?, :valid? #:nodoc:
|
27
|
-
|
28
|
-
# Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
|
29
|
-
def valid?(event = :validate)
|
30
|
-
validate_recursively(event, Set.new)
|
31
|
-
end
|
32
|
-
|
33
|
-
#TODO should callbacks really affect the flow? shouldn't validations themselves do that?
|
34
|
-
def validate_recursively(event, cleared) #:nodoc:
|
35
|
-
return true if cleared.include?(self)
|
36
|
-
cleared << self
|
37
|
-
|
38
|
-
self.class.callbacks.execute(:before_validation, self)
|
39
|
-
|
40
|
-
# Validatable clears the errors list when running valid_for_some_group?, so we save general errors
|
41
|
-
# and then merge them back in after we've validated for events
|
42
|
-
generally_valid = valid_for_group?(nil)
|
43
|
-
general_errors = self.errors.errors.dup
|
44
|
-
|
45
|
-
if respond_to?(:"valid_for_#{event}?")
|
46
|
-
valid_for_events = send(:"valid_for_#{event}?")
|
47
|
-
self.errors.errors.merge!(general_errors) {|key, val1, val2| val1+val2 }
|
48
|
-
return false unless generally_valid && valid_for_events
|
49
|
-
end
|
50
|
-
|
51
|
-
return false unless generally_valid
|
52
|
-
|
53
|
-
if self.respond_to?(:loaded_associations)
|
54
|
-
return false unless self.loaded_associations.all? do |association|
|
55
|
-
association.validate_recursively(event, cleared)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
self.class.callbacks.execute(:after_validation, self)
|
60
|
-
return true
|
61
|
-
end
|
62
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Validatable
|
2
|
-
class ValidationBase
|
3
|
-
alias_method :old_init, :initialize
|
4
|
-
|
5
|
-
DEFAULT_EVENTS = [:validate, :create, :save, :update]
|
6
|
-
|
7
|
-
def initialize(klass, attribute, options={})
|
8
|
-
events = [options.delete(:on)].flatten.compact + [options.delete(:event)].flatten.compact
|
9
|
-
raise ArgumentError.new("Events must be one of #{DEFAULT_EVENTS.inspect}") unless (events & DEFAULT_EVENTS).size == events.size
|
10
|
-
options[:groups] ||= events unless events.empty? ### <- Danger will robinson
|
11
|
-
old_init(klass, attribute, options)
|
12
|
-
end
|
13
|
-
|
14
|
-
def humanized_attribute
|
15
|
-
@humanized_attribute ||= Inflector.humanize(self.attribute.to_s)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Validatable
|
2
|
-
module Helpers #:nodoc:
|
3
|
-
module Formats #:nodoc:
|
4
|
-
|
5
|
-
# Allows you to validate fields as email addresses
|
6
|
-
# validates_format_of :email_field, :with => :email_address
|
7
|
-
|
8
|
-
module Email
|
9
|
-
|
10
|
-
def self.included(base)
|
11
|
-
base::FORMATS.merge!( :email_address => [ EmailAddress,
|
12
|
-
lambda { |attribute| lambda { "%s is not a valid email address".t(self.send(attribute)) } }] )
|
13
|
-
end
|
14
|
-
|
15
|
-
EmailAddress = begin
|
16
|
-
alpha = "a-zA-Z"
|
17
|
-
digit = "0-9"
|
18
|
-
atext = "[#{alpha}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
|
19
|
-
dot_atom_text = "#{atext}+([.]#{atext}*)*"
|
20
|
-
dot_atom = "#{dot_atom_text}"
|
21
|
-
qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
|
22
|
-
text = "[\\x01-\\x09\\x11\\x12\\x14-\\x7f]"
|
23
|
-
quoted_pair = "(\\x5c#{text})"
|
24
|
-
qcontent = "(?:#{qtext}|#{quoted_pair})"
|
25
|
-
quoted_string = "[\"]#{qcontent}+[\"]"
|
26
|
-
atom = "#{atext}+"
|
27
|
-
word = "(?:#{atom}|#{quoted_string})"
|
28
|
-
obs_local_part = "#{word}([.]#{word})*"
|
29
|
-
local_part = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
|
30
|
-
no_ws_ctl = "\\x01-\\x08\\x11\\x12\\x14-\\x1f\\x7f"
|
31
|
-
dtext = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
|
32
|
-
dcontent = "(?:#{dtext}|#{quoted_pair})"
|
33
|
-
domain_literal = "\\[#{dcontent}+\\]"
|
34
|
-
obs_domain = "#{atom}([.]#{atom})*"
|
35
|
-
domain = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
|
36
|
-
addr_spec = "#{local_part}\@#{domain}"
|
37
|
-
pattern = /^#{addr_spec}$/
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/formats/email.rb'
|
2
|
-
|
3
|
-
module Validatable
|
4
|
-
|
5
|
-
class ValidatesFormatOf < ValidationBase
|
6
|
-
FORMATS = {}
|
7
|
-
|
8
|
-
include Validatable::Helpers::Formats::Email
|
9
|
-
|
10
|
-
def initialize(klass, attribute, options={})
|
11
|
-
super
|
12
|
-
if with.is_a? Symbol
|
13
|
-
self.with = if FORMATS[with].is_a? Array
|
14
|
-
@message = (FORMATS[with][1].respond_to?(:call) ? FORMATS[with][1].call(attribute) : FORMATS[with][1]) unless @message
|
15
|
-
FORMATS[with][0]
|
16
|
-
else
|
17
|
-
FORMATS[with]
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def message(instance)
|
23
|
-
super || '%s is invalid'.t(humanized_attribute)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Validatable
|
2
|
-
class ValidatesLengthOf < ValidationBase
|
3
|
-
def message(instance)
|
4
|
-
super || unless minimum.nil?
|
5
|
-
'%s must be more than %d characters long'.t(humanized_attribute, minimum-1)
|
6
|
-
else unless maximum.nil?
|
7
|
-
'%s must be less than %d characters long'.t(humanized_attribute, maximum+1)
|
8
|
-
else unless is.nil?
|
9
|
-
'%s must be %d characters long'.t(humanized_attribute, is)
|
10
|
-
else unless within.nil?
|
11
|
-
'%s must be between %d and %d characters long'.t(humanized_attribute, within.first, within.last)
|
12
|
-
end;end;end;end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Validatable
|
2
|
-
class ValidatesUniquenessOf < ValidationBase
|
3
|
-
option :scope, :allow_nil, :case_sensitive
|
4
|
-
|
5
|
-
def message(instance)
|
6
|
-
super || '%s has already been taken'.t(humanized_attribute)
|
7
|
-
end
|
8
|
-
|
9
|
-
def case_sensitive?
|
10
|
-
case_sensitive
|
11
|
-
end
|
12
|
-
|
13
|
-
def valid?(instance)
|
14
|
-
value = instance.send(self.attribute)
|
15
|
-
return true if allow_nil && value.nil?
|
16
|
-
|
17
|
-
finder_options = if case_sensitive?
|
18
|
-
{ self.attribute => value }
|
19
|
-
else
|
20
|
-
{ self.attribute.like => value }
|
21
|
-
end
|
22
|
-
|
23
|
-
if scope
|
24
|
-
if scope.kind_of?(Array) # if scope is larger than just one property, check them all
|
25
|
-
scope.each do |scope_property|
|
26
|
-
scope_value = instance.send(scope_property)
|
27
|
-
finder_options.merge! scope_property => scope_value
|
28
|
-
end
|
29
|
-
else
|
30
|
-
scope_value = instance.send(scope)
|
31
|
-
finder_options.merge! scope => scope_value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
finder_options.merge!({ instance.database_context.table(instance.class).key.name.not => instance.key }) unless instance.new_record?
|
36
|
-
instance.database_context.first(instance.class, finder_options).nil?
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
Dir[File.dirname(__FILE__) + "/validatable_extensions/**/*.rb"].each do |path|
|
2
|
-
require path
|
3
|
-
end
|
4
|
-
|
5
|
-
#--
|
6
|
-
# TODO:
|
7
|
-
# implement alias_option method for all validations (so we can set :with == :as)
|
8
|
-
# make validations orthogonal with allow_nil default to true
|
9
|
-
#
|
10
|
-
module DataMapper
|
11
|
-
module Validations
|
12
|
-
|
13
|
-
def self.included(base) #:nodoc:
|
14
|
-
base.class_eval do
|
15
|
-
include Validatable
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'data_mapper/validations/validator'
|
2
|
-
|
3
|
-
module DataMapper
|
4
|
-
module Validations
|
5
|
-
class NumberValidator < Validator
|
6
|
-
def <(max)
|
7
|
-
@max_excl = max
|
8
|
-
end
|
9
|
-
|
10
|
-
def <=(max)
|
11
|
-
@max_incl = max
|
12
|
-
end
|
13
|
-
|
14
|
-
def >(min)
|
15
|
-
@min_excl = min
|
16
|
-
end
|
17
|
-
|
18
|
-
def >=(min)
|
19
|
-
@min_incl = min
|
20
|
-
end
|
21
|
-
|
22
|
-
def between(range)
|
23
|
-
@range = range
|
24
|
-
end
|
25
|
-
|
26
|
-
def errors_for(target)
|
27
|
-
errors = []
|
28
|
-
error = nil
|
29
|
-
|
30
|
-
errors << Validator::Error.new(@max_excl, target) if @max_excl && target >= @max_excl
|
31
|
-
errors << Validator::Error.new(@max_incl, target) if @max_incl && target > @max_incl
|
32
|
-
errors << Validator::Error.new(@min_excl, target) if @min_excl && target <= @min_excl
|
33
|
-
errors << Validator::Error.new(@min_incl, target) if @min_incl && target < @min_incl
|
34
|
-
errors << Validator::Error.new(@range, target) if @range && ! @range.include?(target)
|
35
|
-
|
36
|
-
errors
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'data_mapper/validations/validator'
|
2
|
-
|
3
|
-
module DataMapper
|
4
|
-
module Validations
|
5
|
-
class StringValidator < Validator
|
6
|
-
def matches(regexp)
|
7
|
-
@regexp = regexp
|
8
|
-
end
|
9
|
-
|
10
|
-
def errors_for(target)
|
11
|
-
errors = []
|
12
|
-
|
13
|
-
errors << Validator::Error.new(@regexp, target) if @regexp && @regexp.match(target)
|
14
|
-
|
15
|
-
errors
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
data/performance.rb
DELETED
@@ -1,307 +0,0 @@
|
|
1
|
-
require 'benchmark'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
socket_file = [
|
5
|
-
"/opt/local/var/run/mysql5/mysqld.sock",
|
6
|
-
"tmp/mysqld.sock",
|
7
|
-
"tmp/mysql.sock"
|
8
|
-
].find { |path| File.exists?(path) }
|
9
|
-
|
10
|
-
configuration_options = {
|
11
|
-
:adapter => 'mysql',
|
12
|
-
:username => 'root',
|
13
|
-
:password => '',
|
14
|
-
:database => 'data_mapper_1'
|
15
|
-
}
|
16
|
-
|
17
|
-
configuration_options[:socket] = socket_file unless socket_file.nil?
|
18
|
-
|
19
|
-
ActiveRecord::Base.establish_connection(configuration_options)
|
20
|
-
|
21
|
-
ActiveRecord::Base.find_by_sql('SELECT 1')
|
22
|
-
|
23
|
-
class ARAnimal < ActiveRecord::Base #:nodoc:
|
24
|
-
set_table_name 'animals'
|
25
|
-
end
|
26
|
-
|
27
|
-
class ARPerson < ActiveRecord::Base #:nodoc:
|
28
|
-
set_table_name 'people'
|
29
|
-
end
|
30
|
-
|
31
|
-
require 'lib/data_mapper'
|
32
|
-
|
33
|
-
DataMapper::Database.setup(configuration_options)
|
34
|
-
|
35
|
-
class DMAnimal < DataMapper::Base #:nodoc:
|
36
|
-
set_table_name 'animals'
|
37
|
-
property :name, :string
|
38
|
-
property :notes, :text
|
39
|
-
end
|
40
|
-
|
41
|
-
class DMPerson < DataMapper::Base #:nodoc:
|
42
|
-
set_table_name 'people'
|
43
|
-
property :name, :string
|
44
|
-
property :age, :integer
|
45
|
-
property :occupation, :string
|
46
|
-
property :type, :class
|
47
|
-
property :notes, :text
|
48
|
-
property :date_of_birth, :date
|
49
|
-
|
50
|
-
embed :address, :prefix => true do
|
51
|
-
property :street, :string
|
52
|
-
property :city, :string
|
53
|
-
property :state, :string, :size => 2
|
54
|
-
property :zip_code, :string, :size => 10
|
55
|
-
|
56
|
-
def city_state_zip_code
|
57
|
-
"#{city}, #{state} #{zip_code}"
|
58
|
-
end
|
59
|
-
|
60
|
-
def to_s
|
61
|
-
"#{street}\n#{city_state_zip_code}"
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
class Exhibit < DataMapper::Base #:nodoc:
|
68
|
-
property :name, :string
|
69
|
-
belongs_to :zoo
|
70
|
-
end
|
71
|
-
|
72
|
-
class Zoo < DataMapper::Base #:nodoc:
|
73
|
-
property :name, :string
|
74
|
-
property :notes, :text
|
75
|
-
has_many :exhibits
|
76
|
-
end
|
77
|
-
|
78
|
-
class ARZoo < ActiveRecord::Base #:nodoc:
|
79
|
-
set_table_name 'zoos'
|
80
|
-
has_many :exhibits, :class_name => 'ARExhibit', :foreign_key => 'zoo_id'
|
81
|
-
end
|
82
|
-
|
83
|
-
class ARExhibit < ActiveRecord::Base #:nodoc:
|
84
|
-
set_table_name 'exhibits'
|
85
|
-
belongs_to :zoo, :class_name => 'ARZoo', :foreign_key => 'zoo_id'
|
86
|
-
end
|
87
|
-
|
88
|
-
N = (ENV['N'] || 1000).to_i
|
89
|
-
|
90
|
-
# 4.times do
|
91
|
-
# [DMAnimal, Zoo, Exhibit].each do |klass|
|
92
|
-
# klass.all.each do |instance|
|
93
|
-
# klass.create(instance.attributes.reject { |k,v| k == :id })
|
94
|
-
# end
|
95
|
-
# end
|
96
|
-
# end
|
97
|
-
|
98
|
-
Benchmark::send(ENV['BM'] || :bmbm, 40) do |x|
|
99
|
-
|
100
|
-
x.report('ActiveRecord:id') do
|
101
|
-
N.times { ARAnimal.find(1).name }
|
102
|
-
end
|
103
|
-
|
104
|
-
x.report('ActiveRecord:id:raw_result-set') do
|
105
|
-
connection = ARAnimal.connection
|
106
|
-
|
107
|
-
N.times do
|
108
|
-
result = connection.execute("SELECT name FROM animals WHERE id = 1")
|
109
|
-
result.each do |row|
|
110
|
-
nil
|
111
|
-
end
|
112
|
-
result.free if result.respond_to?(:free)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
x.report('DataMapper:id') do
|
117
|
-
N.times { DMAnimal[1].name }
|
118
|
-
end
|
119
|
-
|
120
|
-
x.report('DataMapper:id:in-session') do
|
121
|
-
database do
|
122
|
-
N.times { DMAnimal[1].name }
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
x.report('DataMapper:id:raw-result-set') do
|
127
|
-
database.adapter.connection do |db|
|
128
|
-
N.times do
|
129
|
-
command = db.create_command("SELECT name FROM animals WHERE id = 1")
|
130
|
-
|
131
|
-
command.execute_reader do |reader|
|
132
|
-
reader.each { reader.current_row }
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
x.report('ActiveRecord:all') do
|
139
|
-
N.times { ARZoo.find(:all).each { |a| a.name } }
|
140
|
-
end
|
141
|
-
|
142
|
-
x.report('DataMapper:all') do
|
143
|
-
N.times { Zoo.all.each { |a| a.name } }
|
144
|
-
end
|
145
|
-
|
146
|
-
x.report('DataMapper:all:in-session') do
|
147
|
-
database do
|
148
|
-
N.times { Zoo.all.each { |a| a.name } }
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
x.report('ActiveRecord:conditions') do
|
153
|
-
N.times { ARZoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
|
154
|
-
end
|
155
|
-
|
156
|
-
x.report('DataMapper:conditions:short') do
|
157
|
-
N.times { Zoo.first(:name => 'Galveston').name }
|
158
|
-
end
|
159
|
-
|
160
|
-
x.report('DataMapper:conditions:short:in-session') do
|
161
|
-
database do
|
162
|
-
N.times { Zoo.first(:name => 'Galveston').name }
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
x.report('DataMapper:conditions:long') do
|
167
|
-
N.times { Zoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
|
168
|
-
end
|
169
|
-
|
170
|
-
x.report('DataMapper:conditions:long:in-session') do
|
171
|
-
database do
|
172
|
-
N.times { Zoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
people = [
|
177
|
-
['Sam', 29, 'Programmer', 'A slow text field'],
|
178
|
-
['Amy', 28, 'Business Analyst Manager', 'A slow text field'],
|
179
|
-
['Scott', 25, 'Programmer', 'A slow text field'],
|
180
|
-
['Josh', 23, 'Supervisor', 'A slow text field'],
|
181
|
-
['Bob', 40, 'Peon', 'A slow text field']
|
182
|
-
]
|
183
|
-
|
184
|
-
DMPerson.truncate!
|
185
|
-
|
186
|
-
x.report('ActiveRecord:insert') do
|
187
|
-
N.times do
|
188
|
-
people.each do |a|
|
189
|
-
ARPerson::create(:name => a[0], :age => a[1], :occupation => a[2], :notes => a[3])
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
DMPerson.truncate!
|
195
|
-
|
196
|
-
x.report('DataMapper:insert') do
|
197
|
-
N.times do
|
198
|
-
people.each do |a|
|
199
|
-
DMPerson::create(:name => a[0], :age => a[1], :occupation => a[2], :notes => a[3])
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
x.report('ActiveRecord:update') do
|
205
|
-
N.times do
|
206
|
-
bob = ARAnimal.find(:first, :conditions => ["name = ?", 'Elephant'])
|
207
|
-
bob.name
|
208
|
-
bob.notes = 'Updated by ActiveRecord'
|
209
|
-
bob.save
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
x.report('DataMapper:update') do
|
214
|
-
N.times do
|
215
|
-
bob = DMAnimal.first(:name => 'Elephant')
|
216
|
-
bob.name
|
217
|
-
bob.notes = 'Updated by DataMapper'
|
218
|
-
bob.save
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
x.report('ActiveRecord:associations:lazy') do
|
223
|
-
N.times do
|
224
|
-
zoos = ARZoo.find(:all)
|
225
|
-
zoos.each { |zoo| zoo.name; zoo.exhibits.entries }
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
x.report('ActiveRecord:associations:included') do
|
230
|
-
N.times do
|
231
|
-
zoos = ARZoo.find(:all, :include => [:exhibits])
|
232
|
-
zoos.each { |zoo| zoo.name; zoo.exhibits.entries }
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
x.report('DataMapper:associations') do
|
237
|
-
N.times do
|
238
|
-
Zoo.all.each { |zoo| zoo.name; zoo.exhibits.entries }
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
x.report('DataMapper:associations:in-session') do
|
243
|
-
database do
|
244
|
-
N.times do
|
245
|
-
Zoo.all.each { |zoo| zoo.name; zoo.exhibits.entries }
|
246
|
-
end
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
x.report('ActiveRecord:find_by_sql') do
|
251
|
-
N.times do
|
252
|
-
ARZoo.find_by_sql("SELECT * FROM zoos").each { |z| z.name }
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
x.report('DataMapper:find_by_sql') do
|
257
|
-
N.times do
|
258
|
-
database.query("SELECT * FROM zoos").each { |z| z.name }
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
x.report('DataMapper:raw-query') do
|
263
|
-
N.times do
|
264
|
-
database.adapter.connection do |db|
|
265
|
-
|
266
|
-
command = db.create_command("SELECT * FROM zoos")
|
267
|
-
|
268
|
-
command.execute_reader do |reader|
|
269
|
-
reader.each { reader.current_row }
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
x.report('ActiveRecord:accessors') do
|
276
|
-
person = ARPerson.find(:first)
|
277
|
-
|
278
|
-
N.times do
|
279
|
-
<<-VCARD
|
280
|
-
#{person.name} (#{person.age})
|
281
|
-
#{person.occupation}
|
282
|
-
|
283
|
-
#{person.address_street}
|
284
|
-
#{person.address_city}, #{person.address_state} #{person.address_zip_code}
|
285
|
-
|
286
|
-
#{person.notes}
|
287
|
-
VCARD
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
x.report('DataMapper:accessors') do
|
292
|
-
person = DMPerson.first
|
293
|
-
|
294
|
-
N.times do
|
295
|
-
<<-VCARD
|
296
|
-
#{person.name} (#{person.age})
|
297
|
-
#{person.occupation}
|
298
|
-
|
299
|
-
#{person.address}
|
300
|
-
|
301
|
-
#{person.notes}
|
302
|
-
VCARD
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
|
307
|
-
end
|