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,35 +0,0 @@
|
|
1
|
-
class Object
|
2
|
-
def blank?
|
3
|
-
nil? || (respond_to?(:empty?) && empty?)
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class Numeric
|
8
|
-
def blank?
|
9
|
-
false
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class NilClass
|
14
|
-
def blank?
|
15
|
-
true
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class TrueClass
|
20
|
-
def blank?
|
21
|
-
false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class FalseClass
|
26
|
-
def blank?
|
27
|
-
true
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class String
|
32
|
-
def blank?
|
33
|
-
empty? || self =~ /\A\s*\Z/
|
34
|
-
end
|
35
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# See 'fastthread' dependency in data_mapper.rb
|
2
|
-
# require 'thread'
|
3
|
-
|
4
|
-
module DataMapper
|
5
|
-
module Support
|
6
|
-
|
7
|
-
# A ConnectionPool manages access to database connections by keeping
|
8
|
-
# multiple connections and giving threads exclusive access to each
|
9
|
-
# connection.
|
10
|
-
#
|
11
|
-
# CREDIT: Sharon Rosner, maintainer of the Sequel (http://sequel.rubyforge.org)
|
12
|
-
# project an "ORM framework for Ruby" contributed this class.
|
13
|
-
class ConnectionPool
|
14
|
-
attr_reader :mutex
|
15
|
-
|
16
|
-
# The maximum number of connections.
|
17
|
-
attr_reader :max_size
|
18
|
-
|
19
|
-
# Returns the number of created connections.
|
20
|
-
attr_reader :created_count
|
21
|
-
alias :size :created_count
|
22
|
-
|
23
|
-
# The proc used to create a new connection.
|
24
|
-
attr_accessor :connection_proc
|
25
|
-
|
26
|
-
attr_reader :available_connections, :allocated
|
27
|
-
|
28
|
-
# Constructs a new pool with a maximum size. If a block is supplied, it
|
29
|
-
# is used to create new connections as they are needed.
|
30
|
-
#
|
31
|
-
# pool = ConnectionPool.new(10) {MyConnection.new(opts)}
|
32
|
-
#
|
33
|
-
# The connection creation proc can be changed at any time by assigning a
|
34
|
-
# Proc to pool#connection_proc.
|
35
|
-
#
|
36
|
-
# pool = ConnectionPool.new(10)
|
37
|
-
# pool.connection_proc = proc {MyConnection.new(opts)}
|
38
|
-
def initialize(max_size = 4, &block)
|
39
|
-
@max_size = max_size
|
40
|
-
@mutex = Mutex.new
|
41
|
-
@connection_proc = block
|
42
|
-
|
43
|
-
@available_connections = []
|
44
|
-
@allocated = {}
|
45
|
-
@created_count = 0
|
46
|
-
end
|
47
|
-
|
48
|
-
# Assigns a connection to the current thread, yielding the connection
|
49
|
-
# to the supplied block.
|
50
|
-
#
|
51
|
-
# pool.hold {|conn| conn.execute('DROP TABLE posts;')}
|
52
|
-
#
|
53
|
-
# Pool#hold is re-entrant, meaning it can be called recursively in
|
54
|
-
# the same thread without blocking.
|
55
|
-
#
|
56
|
-
# If no connection is available, Pool#hold will block until a connection
|
57
|
-
# is available.
|
58
|
-
def hold
|
59
|
-
t = Thread.current
|
60
|
-
if (conn = owned_connection(t))
|
61
|
-
return yield(conn)
|
62
|
-
end
|
63
|
-
while !(conn = acquire(t))
|
64
|
-
sleep 0.001
|
65
|
-
end
|
66
|
-
begin
|
67
|
-
yield conn
|
68
|
-
ensure
|
69
|
-
release(t)
|
70
|
-
end
|
71
|
-
rescue Exception => e
|
72
|
-
# if the error is not a StandardError it is converted into RuntimeError.
|
73
|
-
raise e.is_a?(StandardError) ? e : e.message
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
# Returns the connection owned by the supplied thread, if any.
|
78
|
-
def owned_connection(thread)
|
79
|
-
@mutex.synchronize {@allocated[thread]}
|
80
|
-
end
|
81
|
-
|
82
|
-
# Assigns a connection to the supplied thread, if one is available.
|
83
|
-
def acquire(thread)
|
84
|
-
@mutex.synchronize do
|
85
|
-
if conn = available
|
86
|
-
@allocated[thread] = conn
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# Returns an available connection. If no connection is available,
|
92
|
-
# tries to create a new connection.
|
93
|
-
def available
|
94
|
-
@available_connections.pop || make_new
|
95
|
-
end
|
96
|
-
|
97
|
-
# Creates a new connection if the size of the pool is less than the
|
98
|
-
# maximum size.
|
99
|
-
def make_new
|
100
|
-
if @created_count < @max_size
|
101
|
-
@created_count += 1
|
102
|
-
@connection_proc.call
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
# Releases the connection assigned to the supplied thread.
|
107
|
-
def release(thread)
|
108
|
-
@mutex.synchronize do
|
109
|
-
@available_connections << @allocated[thread]
|
110
|
-
@allocated.delete(thread)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
end # class ConnectionPool
|
115
|
-
|
116
|
-
end # module Support
|
117
|
-
end # module DataMapper
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
# The Support module adds functionality to Make Things Easier(tm):
|
3
|
-
# * grouping by attributes of objects in an array (returns a hash, see #DataMapper::Support::Enumerable)
|
4
|
-
# * adds symbols for operators like <= (lte), like, in, select, etc (see #DataMapper::Support::Symbol)
|
5
|
-
# * adds methods for strings, allowing us to ensure strings are wrapped with content (see #DataMapper::Support::String)
|
6
|
-
# * pulls in ActiveSupport's Inflector module
|
7
|
-
# * loads #DataMapper::Database and #DataMapper::Base
|
8
|
-
module Support
|
9
|
-
|
10
|
-
# Extends Array to include an instance method for grouping objects
|
11
|
-
module EnumerableExtensions
|
12
|
-
|
13
|
-
# Group a collection of elements into groups within a
|
14
|
-
# Hash. The value returned by the block passed to group_by
|
15
|
-
# is the key, and the value is an Array of items matching
|
16
|
-
# that key.
|
17
|
-
#
|
18
|
-
# === Example
|
19
|
-
# names = %w{ sam scott amy robert betsy }
|
20
|
-
# names.group_by { |name| name.size }
|
21
|
-
# => { 3 => [ "sam", "amy" ], 5 => [ "scott", "betsy" ], 6 => [ "robert" ]}
|
22
|
-
def group_by
|
23
|
-
inject(Hash.new { |h,k| h[k] = [] }) do |memo,item|
|
24
|
-
memo[yield(item)] << item; memo
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
end # module Enumerable
|
29
|
-
end # module Support
|
30
|
-
end # module DataMapper
|
31
|
-
|
32
|
-
# Extend Array with DataMapper::Support::EnumerableExtensions
|
33
|
-
class Array #:nodoc:
|
34
|
-
include DataMapper::Support::EnumerableExtensions
|
35
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module DataMapper
|
2
|
-
|
3
|
-
class ValidationError < StandardError; end
|
4
|
-
|
5
|
-
class ObjectNotFoundError < StandardError; end
|
6
|
-
|
7
|
-
class MaterializationError < StandardError; end
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
class StandardError
|
12
|
-
|
13
|
-
def display
|
14
|
-
"#{message}\n\t#{backtrace.join("\n\t")}"
|
15
|
-
end
|
16
|
-
end
|
@@ -1,265 +0,0 @@
|
|
1
|
-
# This file was copied from the ActiveSupport project, which
|
2
|
-
# is a part of the Ruby On Rails web-framework (http://rubyonrails.org).
|
3
|
-
# Some methods have been modified or removed.
|
4
|
-
|
5
|
-
require 'singleton'
|
6
|
-
|
7
|
-
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
|
8
|
-
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
|
9
|
-
# in inflections.rb.
|
10
|
-
unless defined?(Inflector)
|
11
|
-
module Inflector
|
12
|
-
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
|
13
|
-
# inflection rules. Examples:
|
14
|
-
#
|
15
|
-
# Inflector.inflections do |inflect|
|
16
|
-
# inflect.plural /^(ox)$/i, '\1\2en'
|
17
|
-
# inflect.singular /^(ox)en/i, '\1'
|
18
|
-
#
|
19
|
-
# inflect.irregular 'octopus', 'octopi'
|
20
|
-
#
|
21
|
-
# inflect.uncountable "equipment"
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
25
|
-
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
26
|
-
# already have been loaded.
|
27
|
-
class Inflections
|
28
|
-
include Singleton
|
29
|
-
|
30
|
-
attr_reader :plurals, :singulars, :uncountables
|
31
|
-
|
32
|
-
def initialize
|
33
|
-
@plurals, @singulars, @uncountables = [], [], []
|
34
|
-
end
|
35
|
-
|
36
|
-
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
37
|
-
# The replacement should always be a string that may include references to the matched data from the rule.
|
38
|
-
def plural(rule, replacement)
|
39
|
-
@plurals.insert(0, [rule, replacement])
|
40
|
-
end
|
41
|
-
|
42
|
-
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
43
|
-
# The replacement should always be a string that may include references to the matched data from the rule.
|
44
|
-
def singular(rule, replacement)
|
45
|
-
@singulars.insert(0, [rule, replacement])
|
46
|
-
end
|
47
|
-
|
48
|
-
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
49
|
-
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
50
|
-
#
|
51
|
-
# Examples:
|
52
|
-
# irregular 'octopus', 'octopi'
|
53
|
-
# irregular 'person', 'people'
|
54
|
-
def irregular(singular, plural)
|
55
|
-
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
56
|
-
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
57
|
-
end
|
58
|
-
|
59
|
-
# Add uncountable words that shouldn't be attempted inflected.
|
60
|
-
#
|
61
|
-
# Examples:
|
62
|
-
# uncountable "money"
|
63
|
-
# uncountable "money", "information"
|
64
|
-
# uncountable %w( money information rice )
|
65
|
-
def uncountable(*words)
|
66
|
-
(@uncountables << words).flatten!
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
extend self
|
71
|
-
|
72
|
-
def inflections
|
73
|
-
if block_given?
|
74
|
-
yield Inflections.instance
|
75
|
-
else
|
76
|
-
Inflections.instance
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Returns the plural form of the word in the string.
|
81
|
-
#
|
82
|
-
# Examples
|
83
|
-
# "post".pluralize #=> "posts"
|
84
|
-
# "octopus".pluralize #=> "octopi"
|
85
|
-
# "sheep".pluralize #=> "sheep"
|
86
|
-
# "words".pluralize #=> "words"
|
87
|
-
# "the blue mailman".pluralize #=> "the blue mailmen"
|
88
|
-
# "CamelOctopus".pluralize #=> "CamelOctopi"
|
89
|
-
def pluralize(word)
|
90
|
-
result = word.to_s.dup
|
91
|
-
|
92
|
-
if inflections.uncountables.include?(result.downcase)
|
93
|
-
result
|
94
|
-
else
|
95
|
-
inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
96
|
-
result
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# The reverse of pluralize, returns the singular form of a word in a string.
|
101
|
-
#
|
102
|
-
# Examples
|
103
|
-
# "posts".singularize #=> "post"
|
104
|
-
# "octopi".singularize #=> "octopus"
|
105
|
-
# "sheep".singluarize #=> "sheep"
|
106
|
-
# "word".singluarize #=> "word"
|
107
|
-
# "the blue mailmen".singularize #=> "the blue mailman"
|
108
|
-
# "CamelOctopi".singularize #=> "CamelOctopus"
|
109
|
-
def singularize(word)
|
110
|
-
result = word.to_s.dup
|
111
|
-
|
112
|
-
if inflections.uncountables.include?(result.downcase)
|
113
|
-
result
|
114
|
-
else
|
115
|
-
inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
116
|
-
result
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
# By default, camelize converts strings to UpperCamelCase.
|
121
|
-
#
|
122
|
-
# camelize will also convert '/' to '::' which is useful for converting paths to namespaces
|
123
|
-
#
|
124
|
-
# Examples
|
125
|
-
# "active_record".camelize #=> "ActiveRecord"
|
126
|
-
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
|
127
|
-
def camelize(lower_case_and_underscored_word, *args)
|
128
|
-
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
129
|
-
end
|
130
|
-
|
131
|
-
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
|
132
|
-
#
|
133
|
-
# Changes '::' to '/' to convert namespaces to paths.
|
134
|
-
#
|
135
|
-
# Examples
|
136
|
-
# "ActiveRecord".underscore #=> "active_record"
|
137
|
-
# "ActiveRecord::Errors".underscore #=> active_record/errors
|
138
|
-
def underscore(camel_cased_word)
|
139
|
-
camel_cased_word.to_s.gsub(/::/, '/').
|
140
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
141
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
142
|
-
tr("-", "_").
|
143
|
-
downcase
|
144
|
-
end
|
145
|
-
|
146
|
-
# Capitalizes the first word and turns underscores into spaces and strips _id.
|
147
|
-
# Like titleize, this is meant for creating pretty output.
|
148
|
-
#
|
149
|
-
# Examples
|
150
|
-
# "employee_salary" #=> "Employee salary"
|
151
|
-
# "author_id" #=> "Author"
|
152
|
-
def humanize(lower_case_and_underscored_word)
|
153
|
-
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
154
|
-
end
|
155
|
-
|
156
|
-
# Removes the module part from the expression in the string
|
157
|
-
#
|
158
|
-
# Examples
|
159
|
-
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
|
160
|
-
# "Inflections".demodulize #=> "Inflections"
|
161
|
-
def demodulize(class_name_in_module)
|
162
|
-
class_name_in_module.to_s.gsub(/^.*::/, '')
|
163
|
-
end
|
164
|
-
|
165
|
-
# Create the name of a table like Rails does for models to table names. This method
|
166
|
-
# uses the pluralize method on the last word in the string.
|
167
|
-
#
|
168
|
-
# Examples
|
169
|
-
# "RawScaledScorer".tableize #=> "raw_scaled_scorers"
|
170
|
-
# "egg_and_ham".tableize #=> "egg_and_hams"
|
171
|
-
# "fancyCategory".tableize #=> "fancy_categories"
|
172
|
-
def tableize(class_name)
|
173
|
-
pluralize(underscore(class_name))
|
174
|
-
end
|
175
|
-
|
176
|
-
# Create a class name from a table name like Rails does for table names to models.
|
177
|
-
# Note that this returns a string and not a Class. (To convert to an actual class
|
178
|
-
# follow classify with constantize.)
|
179
|
-
#
|
180
|
-
# Examples
|
181
|
-
# "egg_and_hams".classify #=> "EggAndHam"
|
182
|
-
# "post".classify #=> "Post"
|
183
|
-
def classify(table_name)
|
184
|
-
# strip out any leading schema name
|
185
|
-
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
186
|
-
end
|
187
|
-
|
188
|
-
# Creates a foreign key name from a class name.
|
189
|
-
#
|
190
|
-
# Examples
|
191
|
-
# "Message".foreign_key #=> "message_id"
|
192
|
-
# "Admin::Post".foreign_key #=> "post_id"
|
193
|
-
def foreign_key(class_name, key = "id")
|
194
|
-
underscore(demodulize(class_name.to_s)) << "_" << key.to_s
|
195
|
-
end
|
196
|
-
|
197
|
-
# Constantize tries to find a declared constant with the name specified
|
198
|
-
# in the string. It raises a NameError when the name is not in CamelCase
|
199
|
-
# or is not initialized.
|
200
|
-
#
|
201
|
-
# Examples
|
202
|
-
# "Module".constantize #=> Module
|
203
|
-
# "Class".constantize #=> Class
|
204
|
-
def constantize(camel_cased_word)
|
205
|
-
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
|
206
|
-
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
|
207
|
-
end
|
208
|
-
|
209
|
-
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
Inflector.inflections do |inflect|
|
215
|
-
inflect.plural(/$/, 's')
|
216
|
-
inflect.plural(/s$/i, 's')
|
217
|
-
inflect.plural(/(ax|test)is$/i, '\1es')
|
218
|
-
inflect.plural(/(octop|vir)us$/i, '\1i')
|
219
|
-
inflect.plural(/(alias|status)$/i, '\1es')
|
220
|
-
inflect.plural(/(bu)s$/i, '\1ses')
|
221
|
-
inflect.plural(/(buffal|tomat)o$/i, '\1oes')
|
222
|
-
inflect.plural(/([ti])um$/i, '\1a')
|
223
|
-
inflect.plural(/sis$/i, 'ses')
|
224
|
-
inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
225
|
-
inflect.plural(/(hive)$/i, '\1s')
|
226
|
-
inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
227
|
-
inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
|
228
|
-
inflect.plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
|
229
|
-
inflect.plural(/([m|l])ouse$/i, '\1ice')
|
230
|
-
inflect.plural(/^(ox)$/i, '\1en')
|
231
|
-
inflect.plural(/(quiz)$/i, '\1zes')
|
232
|
-
|
233
|
-
inflect.singular(/s$/i, '')
|
234
|
-
inflect.singular(/(n)ews$/i, '\1ews')
|
235
|
-
inflect.singular(/([ti])a$/i, '\1um')
|
236
|
-
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
|
237
|
-
inflect.singular(/(^analy)ses$/i, '\1sis')
|
238
|
-
inflect.singular(/([^f])ves$/i, '\1fe')
|
239
|
-
inflect.singular(/(hive)s$/i, '\1')
|
240
|
-
inflect.singular(/(tive)s$/i, '\1')
|
241
|
-
inflect.singular(/([lr])ves$/i, '\1f')
|
242
|
-
inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
243
|
-
inflect.singular(/(s)eries$/i, '\1eries')
|
244
|
-
inflect.singular(/(m)ovies$/i, '\1ovie')
|
245
|
-
inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
|
246
|
-
inflect.singular(/([m|l])ice$/i, '\1ouse')
|
247
|
-
inflect.singular(/(bus)es$/i, '\1')
|
248
|
-
inflect.singular(/(o)es$/i, '\1')
|
249
|
-
inflect.singular(/(shoe)s$/i, '\1')
|
250
|
-
inflect.singular(/(cris|ax|test)es$/i, '\1is')
|
251
|
-
inflect.singular(/(octop|vir)i$/i, '\1us')
|
252
|
-
inflect.singular(/(alias|status)es$/i, '\1')
|
253
|
-
inflect.singular(/^(ox)en/i, '\1')
|
254
|
-
inflect.singular(/(vert|ind)ices$/i, '\1ex')
|
255
|
-
inflect.singular(/(matr)ices$/i, '\1ix')
|
256
|
-
inflect.singular(/(quiz)zes$/i, '\1')
|
257
|
-
|
258
|
-
inflect.irregular('person', 'people')
|
259
|
-
inflect.irregular('man', 'men')
|
260
|
-
inflect.irregular('child', 'children')
|
261
|
-
inflect.irregular('sex', 'sexes')
|
262
|
-
inflect.irregular('move', 'moves')
|
263
|
-
|
264
|
-
inflect.uncountable(%w(equipment information rice money species series fish sheep))
|
265
|
-
end
|