sbf-dm-core 1.3.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +29 -0
- data/.document +5 -0
- data/.gitignore +44 -0
- data/.rspec +1 -0
- data/.rubocop.yml +468 -0
- data/.travis.yml +57 -0
- data/.yardopts +1 -0
- data/Gemfile +70 -0
- data/LICENSE +20 -0
- data/README.md +269 -0
- data/Rakefile +4 -0
- data/dm-core.gemspec +21 -0
- data/lib/dm-core/adapters/abstract_adapter.rb +233 -0
- data/lib/dm-core/adapters/in_memory_adapter.rb +110 -0
- data/lib/dm-core/adapters.rb +249 -0
- data/lib/dm-core/associations/many_to_many.rb +477 -0
- data/lib/dm-core/associations/many_to_one.rb +282 -0
- data/lib/dm-core/associations/one_to_many.rb +332 -0
- data/lib/dm-core/associations/one_to_one.rb +84 -0
- data/lib/dm-core/associations/relationship.rb +650 -0
- data/lib/dm-core/backwards.rb +11 -0
- data/lib/dm-core/collection.rb +1486 -0
- data/lib/dm-core/core_ext/kernel.rb +21 -0
- data/lib/dm-core/core_ext/pathname.rb +4 -0
- data/lib/dm-core/core_ext/symbol.rb +10 -0
- data/lib/dm-core/identity_map.rb +6 -0
- data/lib/dm-core/model/hook.rb +99 -0
- data/lib/dm-core/model/is.rb +30 -0
- data/lib/dm-core/model/property.rb +244 -0
- data/lib/dm-core/model/relationship.rb +366 -0
- data/lib/dm-core/model/scope.rb +87 -0
- data/lib/dm-core/model.rb +876 -0
- data/lib/dm-core/property/binary.rb +19 -0
- data/lib/dm-core/property/boolean.rb +35 -0
- data/lib/dm-core/property/class.rb +23 -0
- data/lib/dm-core/property/date.rb +45 -0
- data/lib/dm-core/property/date_time.rb +44 -0
- data/lib/dm-core/property/decimal.rb +47 -0
- data/lib/dm-core/property/discriminator.rb +40 -0
- data/lib/dm-core/property/float.rb +27 -0
- data/lib/dm-core/property/integer.rb +32 -0
- data/lib/dm-core/property/invalid_value_error.rb +17 -0
- data/lib/dm-core/property/lookup.rb +26 -0
- data/lib/dm-core/property/numeric.rb +35 -0
- data/lib/dm-core/property/object.rb +33 -0
- data/lib/dm-core/property/serial.rb +13 -0
- data/lib/dm-core/property/string.rb +47 -0
- data/lib/dm-core/property/text.rb +12 -0
- data/lib/dm-core/property/time.rb +46 -0
- data/lib/dm-core/property/typecast/numeric.rb +32 -0
- data/lib/dm-core/property/typecast/time.rb +33 -0
- data/lib/dm-core/property.rb +856 -0
- data/lib/dm-core/property_set.rb +177 -0
- data/lib/dm-core/query/conditions/comparison.rb +886 -0
- data/lib/dm-core/query/conditions/operation.rb +710 -0
- data/lib/dm-core/query/direction.rb +33 -0
- data/lib/dm-core/query/operator.rb +34 -0
- data/lib/dm-core/query/path.rb +113 -0
- data/lib/dm-core/query/sort.rb +38 -0
- data/lib/dm-core/query.rb +1352 -0
- data/lib/dm-core/relationship_set.rb +69 -0
- data/lib/dm-core/repository.rb +226 -0
- data/lib/dm-core/resource/persistence_state/clean.rb +36 -0
- data/lib/dm-core/resource/persistence_state/deleted.rb +26 -0
- data/lib/dm-core/resource/persistence_state/dirty.rb +91 -0
- data/lib/dm-core/resource/persistence_state/immutable.rb +32 -0
- data/lib/dm-core/resource/persistence_state/persisted.rb +25 -0
- data/lib/dm-core/resource/persistence_state/transient.rb +87 -0
- data/lib/dm-core/resource/persistence_state.rb +70 -0
- data/lib/dm-core/resource.rb +1220 -0
- data/lib/dm-core/spec/lib/adapter_helpers.rb +63 -0
- data/lib/dm-core/spec/lib/collection_helpers.rb +21 -0
- data/lib/dm-core/spec/lib/counter_adapter.rb +38 -0
- data/lib/dm-core/spec/lib/pending_helpers.rb +50 -0
- data/lib/dm-core/spec/lib/spec_helper.rb +74 -0
- data/lib/dm-core/spec/setup.rb +164 -0
- data/lib/dm-core/spec/shared/adapter_spec.rb +366 -0
- data/lib/dm-core/spec/shared/public/property_spec.rb +229 -0
- data/lib/dm-core/spec/shared/resource_spec.rb +1221 -0
- data/lib/dm-core/spec/shared/sel_spec.rb +111 -0
- data/lib/dm-core/spec/shared/semipublic/property_spec.rb +184 -0
- data/lib/dm-core/spec/shared/semipublic/query/conditions/abstract_comparison_spec.rb +261 -0
- data/lib/dm-core/support/assertions.rb +8 -0
- data/lib/dm-core/support/chainable.rb +18 -0
- data/lib/dm-core/support/deprecate.rb +12 -0
- data/lib/dm-core/support/descendant_set.rb +89 -0
- data/lib/dm-core/support/equalizer.rb +48 -0
- data/lib/dm-core/support/ext/array.rb +22 -0
- data/lib/dm-core/support/ext/blank.rb +25 -0
- data/lib/dm-core/support/ext/hash.rb +67 -0
- data/lib/dm-core/support/ext/module.rb +47 -0
- data/lib/dm-core/support/ext/object.rb +57 -0
- data/lib/dm-core/support/ext/string.rb +24 -0
- data/lib/dm-core/support/ext/try_dup.rb +12 -0
- data/lib/dm-core/support/hook.rb +388 -0
- data/lib/dm-core/support/inflections.rb +60 -0
- data/lib/dm-core/support/inflector/inflections.rb +211 -0
- data/lib/dm-core/support/inflector/methods.rb +151 -0
- data/lib/dm-core/support/lazy_array.rb +451 -0
- data/lib/dm-core/support/local_object_space.rb +13 -0
- data/lib/dm-core/support/logger.rb +201 -0
- data/lib/dm-core/support/mash.rb +176 -0
- data/lib/dm-core/support/naming_conventions.rb +109 -0
- data/lib/dm-core/support/ordered_set.rb +381 -0
- data/lib/dm-core/support/subject.rb +33 -0
- data/lib/dm-core/support/subject_set.rb +251 -0
- data/lib/dm-core/version.rb +3 -0
- data/lib/dm-core.rb +274 -0
- data/script/performance.rb +275 -0
- data/script/profile.rb +218 -0
- data/spec/lib/rspec_immediate_feedback_formatter.rb +54 -0
- data/spec/public/associations/many_to_many/read_multiple_join_spec.rb +69 -0
- data/spec/public/associations/many_to_many_spec.rb +197 -0
- data/spec/public/associations/many_to_one_spec.rb +83 -0
- data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +40 -0
- data/spec/public/associations/many_to_one_with_custom_fk_spec.rb +49 -0
- data/spec/public/associations/one_to_many_spec.rb +81 -0
- data/spec/public/associations/one_to_one_spec.rb +176 -0
- data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +46 -0
- data/spec/public/collection_spec.rb +69 -0
- data/spec/public/finalize_spec.rb +77 -0
- data/spec/public/model/hook_spec.rb +245 -0
- data/spec/public/model/property_spec.rb +91 -0
- data/spec/public/model/relationship_spec.rb +1040 -0
- data/spec/public/model_spec.rb +456 -0
- data/spec/public/property/binary_spec.rb +43 -0
- data/spec/public/property/boolean_spec.rb +21 -0
- data/spec/public/property/class_spec.rb +27 -0
- data/spec/public/property/date_spec.rb +21 -0
- data/spec/public/property/date_time_spec.rb +21 -0
- data/spec/public/property/decimal_spec.rb +23 -0
- data/spec/public/property/discriminator_spec.rb +134 -0
- data/spec/public/property/float_spec.rb +22 -0
- data/spec/public/property/integer_spec.rb +22 -0
- data/spec/public/property/object_spec.rb +117 -0
- data/spec/public/property/serial_spec.rb +22 -0
- data/spec/public/property/string_spec.rb +21 -0
- data/spec/public/property/text_spec.rb +62 -0
- data/spec/public/property/time_spec.rb +21 -0
- data/spec/public/property_spec.rb +333 -0
- data/spec/public/resource/state_spec.rb +72 -0
- data/spec/public/resource_spec.rb +289 -0
- data/spec/public/sel_spec.rb +53 -0
- data/spec/public/setup_spec.rb +145 -0
- data/spec/public/shared/association_collection_shared_spec.rb +309 -0
- data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
- data/spec/public/shared/collection_shared_spec.rb +1637 -0
- data/spec/public/shared/finder_shared_spec.rb +1647 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +13 -0
- data/spec/semipublic/associations/many_to_many_spec.rb +94 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +63 -0
- data/spec/semipublic/associations/one_to_many_spec.rb +55 -0
- data/spec/semipublic/associations/one_to_one_spec.rb +53 -0
- data/spec/semipublic/associations/relationship_spec.rb +200 -0
- data/spec/semipublic/associations_spec.rb +177 -0
- data/spec/semipublic/collection_spec.rb +110 -0
- data/spec/semipublic/model_spec.rb +96 -0
- data/spec/semipublic/property/binary_spec.rb +13 -0
- data/spec/semipublic/property/boolean_spec.rb +47 -0
- data/spec/semipublic/property/class_spec.rb +33 -0
- data/spec/semipublic/property/date_spec.rb +43 -0
- data/spec/semipublic/property/date_time_spec.rb +46 -0
- data/spec/semipublic/property/decimal_spec.rb +83 -0
- data/spec/semipublic/property/discriminator_spec.rb +19 -0
- data/spec/semipublic/property/float_spec.rb +82 -0
- data/spec/semipublic/property/integer_spec.rb +82 -0
- data/spec/semipublic/property/lookup_spec.rb +29 -0
- data/spec/semipublic/property/serial_spec.rb +13 -0
- data/spec/semipublic/property/string_spec.rb +13 -0
- data/spec/semipublic/property/text_spec.rb +31 -0
- data/spec/semipublic/property/time_spec.rb +50 -0
- data/spec/semipublic/property_spec.rb +114 -0
- data/spec/semipublic/query/conditions/comparison_spec.rb +1502 -0
- data/spec/semipublic/query/conditions/operation_spec.rb +1296 -0
- data/spec/semipublic/query/path_spec.rb +471 -0
- data/spec/semipublic/query_spec.rb +3665 -0
- data/spec/semipublic/resource/state/clean_spec.rb +89 -0
- data/spec/semipublic/resource/state/deleted_spec.rb +79 -0
- data/spec/semipublic/resource/state/dirty_spec.rb +163 -0
- data/spec/semipublic/resource/state/immutable_spec.rb +107 -0
- data/spec/semipublic/resource/state/transient_spec.rb +163 -0
- data/spec/semipublic/resource/state_spec.rb +230 -0
- data/spec/semipublic/resource_spec.rb +23 -0
- data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +198 -0
- data/spec/semipublic/shared/resource_state_shared_spec.rb +91 -0
- data/spec/semipublic/shared/subject_shared_spec.rb +79 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/core_ext/hash.rb +10 -0
- data/spec/support/core_ext/inheritable_attributes.rb +46 -0
- data/spec/support/properties/huge_integer.rb +17 -0
- data/spec/unit/array_spec.rb +23 -0
- data/spec/unit/blank_spec.rb +73 -0
- data/spec/unit/data_mapper/ordered_set/append_spec.rb +26 -0
- data/spec/unit/data_mapper/ordered_set/clear_spec.rb +24 -0
- data/spec/unit/data_mapper/ordered_set/delete_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/each_spec.rb +19 -0
- data/spec/unit/data_mapper/ordered_set/empty_spec.rb +20 -0
- data/spec/unit/data_mapper/ordered_set/entries_spec.rb +22 -0
- data/spec/unit/data_mapper/ordered_set/eql_spec.rb +51 -0
- data/spec/unit/data_mapper/ordered_set/equal_value_spec.rb +84 -0
- data/spec/unit/data_mapper/ordered_set/hash_spec.rb +12 -0
- data/spec/unit/data_mapper/ordered_set/include_spec.rb +23 -0
- data/spec/unit/data_mapper/ordered_set/index_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/initialize_spec.rb +32 -0
- data/spec/unit/data_mapper/ordered_set/merge_spec.rb +36 -0
- data/spec/unit/data_mapper/ordered_set/shared/append_spec.rb +24 -0
- data/spec/unit/data_mapper/ordered_set/shared/clear_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/delete_spec.rb +25 -0
- data/spec/unit/data_mapper/ordered_set/shared/each_spec.rb +17 -0
- data/spec/unit/data_mapper/ordered_set/shared/empty_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/entries_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/include_spec.rb +9 -0
- data/spec/unit/data_mapper/ordered_set/shared/index_spec.rb +13 -0
- data/spec/unit/data_mapper/ordered_set/shared/initialize_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/shared/merge_spec.rb +28 -0
- data/spec/unit/data_mapper/ordered_set/shared/size_spec.rb +13 -0
- data/spec/unit/data_mapper/ordered_set/shared/to_ary_spec.rb +11 -0
- data/spec/unit/data_mapper/ordered_set/size_spec.rb +27 -0
- data/spec/unit/data_mapper/ordered_set/to_ary_spec.rb +23 -0
- data/spec/unit/data_mapper/subject_set/append_spec.rb +47 -0
- data/spec/unit/data_mapper/subject_set/clear_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/delete_spec.rb +40 -0
- data/spec/unit/data_mapper/subject_set/each_spec.rb +30 -0
- data/spec/unit/data_mapper/subject_set/empty_spec.rb +31 -0
- data/spec/unit/data_mapper/subject_set/entries_spec.rb +31 -0
- data/spec/unit/data_mapper/subject_set/get_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/include_spec.rb +32 -0
- data/spec/unit/data_mapper/subject_set/named_spec.rb +33 -0
- data/spec/unit/data_mapper/subject_set/shared/append_spec.rb +18 -0
- data/spec/unit/data_mapper/subject_set/shared/clear_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/delete_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/each_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/empty_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/entries_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/get_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/include_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/named_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/size_spec.rb +13 -0
- data/spec/unit/data_mapper/subject_set/shared/to_ary_spec.rb +9 -0
- data/spec/unit/data_mapper/subject_set/shared/values_at_spec.rb +44 -0
- data/spec/unit/data_mapper/subject_set/size_spec.rb +42 -0
- data/spec/unit/data_mapper/subject_set/to_ary_spec.rb +34 -0
- data/spec/unit/data_mapper/subject_set/values_at_spec.rb +57 -0
- data/spec/unit/hash_spec.rb +27 -0
- data/spec/unit/hook_spec.rb +1216 -0
- data/spec/unit/inflections_spec.rb +14 -0
- data/spec/unit/lazy_array_spec.rb +1949 -0
- data/spec/unit/mash_spec.rb +289 -0
- data/spec/unit/module_spec.rb +70 -0
- data/spec/unit/object_spec.rb +38 -0
- data/spec/unit/try_dup_spec.rb +46 -0
- data/tasks/ci.rake +1 -0
- data/tasks/spec.rake +18 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +323 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
module DataMapper
|
2
|
+
Inflector.inflections do |inflect|
|
3
|
+
inflect.plural(/$/, 's')
|
4
|
+
inflect.plural(/s$/i, 's')
|
5
|
+
inflect.plural(/(ax|test)is$/i, '\1es')
|
6
|
+
inflect.plural(/(octop|vir)us$/i, '\1i')
|
7
|
+
inflect.plural(/(octop|vir)i$/i, '\1i')
|
8
|
+
inflect.plural(/(alias|status)$/i, '\1es')
|
9
|
+
inflect.plural(/(bu)s$/i, '\1ses')
|
10
|
+
inflect.plural(/(buffal|tomat)o$/i, '\1oes')
|
11
|
+
inflect.plural(/([ti])um$/i, '\1a')
|
12
|
+
inflect.plural(/([ti])a$/i, '\1a')
|
13
|
+
inflect.plural(/sis$/i, 'ses')
|
14
|
+
inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
|
15
|
+
inflect.plural(/(hive)$/i, '\1s')
|
16
|
+
inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
|
17
|
+
inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
|
18
|
+
inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices')
|
19
|
+
inflect.plural(/([m|l])ouse$/i, '\1ice')
|
20
|
+
inflect.plural(/([m|l])ice$/i, '\1ice')
|
21
|
+
inflect.plural(/^(ox)$/i, '\1en')
|
22
|
+
inflect.plural(/^(oxen)$/i, '\1')
|
23
|
+
inflect.plural(/(quiz)$/i, '\1zes')
|
24
|
+
|
25
|
+
inflect.singular(/s$/i, '')
|
26
|
+
inflect.singular(/(n)ews$/i, '\1ews')
|
27
|
+
inflect.singular(/([ti])a$/i, '\1um')
|
28
|
+
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
|
29
|
+
inflect.singular(/(^analy)ses$/i, '\1sis')
|
30
|
+
inflect.singular(/([^f])ves$/i, '\1fe')
|
31
|
+
inflect.singular(/(hive)s$/i, '\1')
|
32
|
+
inflect.singular(/(tive)s$/i, '\1')
|
33
|
+
inflect.singular(/([lr])ves$/i, '\1f')
|
34
|
+
inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
|
35
|
+
inflect.singular(/(s)eries$/i, '\1eries')
|
36
|
+
inflect.singular(/(m)ovies$/i, '\1ovie')
|
37
|
+
inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
|
38
|
+
inflect.singular(/([m|l])ice$/i, '\1ouse')
|
39
|
+
inflect.singular(/(bus)es$/i, '\1')
|
40
|
+
inflect.singular(/(o)es$/i, '\1')
|
41
|
+
inflect.singular(/(shoe)s$/i, '\1')
|
42
|
+
inflect.singular(/(cris|ax|test)es$/i, '\1is')
|
43
|
+
inflect.singular(/(octop|vir)i$/i, '\1us')
|
44
|
+
inflect.singular(/(alias|status)(es)?$/i, '\1')
|
45
|
+
inflect.singular(/^(ox)en/i, '\1')
|
46
|
+
inflect.singular(/(vert|ind)ices$/i, '\1ex')
|
47
|
+
inflect.singular(/(matr)ices$/i, '\1ix')
|
48
|
+
inflect.singular(/(quiz)zes$/i, '\1')
|
49
|
+
inflect.singular(/(database)s$/i, '\1')
|
50
|
+
|
51
|
+
inflect.irregular('person', 'people')
|
52
|
+
inflect.irregular('man', 'men')
|
53
|
+
inflect.irregular('child', 'children')
|
54
|
+
inflect.irregular('sex', 'sexes')
|
55
|
+
inflect.irregular('move', 'moves')
|
56
|
+
inflect.irregular('cow', 'kine')
|
57
|
+
|
58
|
+
inflect.uncountable(%w(equipment information rice money species series fish sheep jeans))
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,211 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Inflector
|
3
|
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
|
4
|
+
# inflection rules. Examples:
|
5
|
+
#
|
6
|
+
# DataMapper::Inflector.inflections do |inflect|
|
7
|
+
# inflect.plural /^(ox)$/i, '\1\2en'
|
8
|
+
# inflect.singular /^(ox)en/i, '\1'
|
9
|
+
#
|
10
|
+
# inflect.irregular 'octopus', 'octopi'
|
11
|
+
#
|
12
|
+
# inflect.uncountable "equipment"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
|
16
|
+
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
|
17
|
+
# already have been loaded.
|
18
|
+
class Inflections
|
19
|
+
def self.instance
|
20
|
+
@__instance__ ||= new
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :plurals, :singulars, :uncountables, :humans
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@plurals, @singulars, @uncountables, @humans = [], [], [], []
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
|
30
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
31
|
+
def plural(rule, replacement)
|
32
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
33
|
+
@uncountables.delete(replacement)
|
34
|
+
@plurals.insert(0, [rule, replacement])
|
35
|
+
end
|
36
|
+
|
37
|
+
# Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
|
38
|
+
# The replacement should always be a string that may include references to the matched data from the rule.
|
39
|
+
def singular(rule, replacement)
|
40
|
+
@uncountables.delete(rule) if rule.is_a?(String)
|
41
|
+
@uncountables.delete(replacement)
|
42
|
+
@singulars.insert(0, [rule, replacement])
|
43
|
+
end
|
44
|
+
|
45
|
+
# Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
|
46
|
+
# for strings, not regular expressions. You simply pass the irregular in singular and plural form.
|
47
|
+
#
|
48
|
+
# Examples:
|
49
|
+
# irregular 'octopus', 'octopi'
|
50
|
+
# irregular 'person', 'people'
|
51
|
+
def irregular(singular, plural)
|
52
|
+
@uncountables.delete(singular)
|
53
|
+
@uncountables.delete(plural)
|
54
|
+
if singular[0,1].upcase == plural[0,1].upcase
|
55
|
+
plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
|
56
|
+
plural(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + plural[1..-1])
|
57
|
+
singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
|
58
|
+
else
|
59
|
+
plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
60
|
+
plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
61
|
+
plural(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
|
62
|
+
plural(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
|
63
|
+
singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
|
64
|
+
singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add uncountable words that shouldn't be attempted inflected.
|
69
|
+
#
|
70
|
+
# Examples:
|
71
|
+
# uncountable "money"
|
72
|
+
# uncountable "money", "information"
|
73
|
+
# uncountable %w( money information rice )
|
74
|
+
def uncountable(*words)
|
75
|
+
(@uncountables << words).flatten!
|
76
|
+
end
|
77
|
+
|
78
|
+
# Specifies a humanized form of a string by a regular expression rule or by a string mapping.
|
79
|
+
# When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
|
80
|
+
# When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
|
81
|
+
#
|
82
|
+
# Examples:
|
83
|
+
# human /_cnt$/i, '\1_count'
|
84
|
+
# human "legacy_col_person_name", "Name"
|
85
|
+
def human(rule, replacement)
|
86
|
+
@humans.insert(0, [rule, replacement])
|
87
|
+
end
|
88
|
+
|
89
|
+
# Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
|
90
|
+
# Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>,
|
91
|
+
# <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>.
|
92
|
+
#
|
93
|
+
# Examples:
|
94
|
+
# clear :all
|
95
|
+
# clear :plurals
|
96
|
+
def clear(scope = :all)
|
97
|
+
case scope
|
98
|
+
when :all
|
99
|
+
@plurals, @singulars, @uncountables = [], [], []
|
100
|
+
else
|
101
|
+
instance_variable_set "@#{scope}", []
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Yields a singleton instance of Inflector::Inflections so you can specify additional
|
107
|
+
# inflector rules.
|
108
|
+
#
|
109
|
+
# Example:
|
110
|
+
# DataMapper::Inflector.inflections do |inflect|
|
111
|
+
# inflect.uncountable "rails"
|
112
|
+
# end
|
113
|
+
def inflections
|
114
|
+
if block_given?
|
115
|
+
yield Inflections.instance
|
116
|
+
else
|
117
|
+
Inflections.instance
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the plural form of the word in the string.
|
122
|
+
#
|
123
|
+
# Examples:
|
124
|
+
# "post".pluralize # => "posts"
|
125
|
+
# "octopus".pluralize # => "octopi"
|
126
|
+
# "sheep".pluralize # => "sheep"
|
127
|
+
# "words".pluralize # => "words"
|
128
|
+
# "CamelOctopus".pluralize # => "CamelOctopi"
|
129
|
+
def pluralize(word)
|
130
|
+
result = word.to_s.dup
|
131
|
+
|
132
|
+
if word.empty? || inflections.uncountables.include?(result.downcase)
|
133
|
+
result
|
134
|
+
else
|
135
|
+
inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# The reverse of +pluralize+, returns the singular form of a word in a string.
|
141
|
+
#
|
142
|
+
# Examples:
|
143
|
+
# "posts".singularize # => "post"
|
144
|
+
# "octopi".singularize # => "octopus"
|
145
|
+
# "sheep".singularize # => "sheep"
|
146
|
+
# "word".singularize # => "word"
|
147
|
+
# "CamelOctopi".singularize # => "CamelOctopus"
|
148
|
+
def singularize(word)
|
149
|
+
result = word.to_s.dup
|
150
|
+
|
151
|
+
if inflections.uncountables.any? { |inflection| result =~ /\b(#{inflection})\Z/i }
|
152
|
+
result
|
153
|
+
else
|
154
|
+
inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
155
|
+
result
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Capitalizes the first word and turns underscores into spaces and strips a
|
160
|
+
# trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
|
161
|
+
#
|
162
|
+
# Examples:
|
163
|
+
# "employee_salary" # => "Employee salary"
|
164
|
+
# "author_id" # => "Author"
|
165
|
+
def humanize(lower_case_and_underscored_word)
|
166
|
+
result = lower_case_and_underscored_word.to_s.dup
|
167
|
+
|
168
|
+
inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
|
169
|
+
result.gsub(/_id$/, "").gsub(/_/, " ").capitalize
|
170
|
+
end
|
171
|
+
|
172
|
+
# Capitalizes all the words and replaces some characters in the string to create
|
173
|
+
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
|
174
|
+
# used in the Rails internals.
|
175
|
+
#
|
176
|
+
# +titleize+ is also aliased as as +titlecase+.
|
177
|
+
#
|
178
|
+
# Examples:
|
179
|
+
# "man from the boondocks".titleize # => "Man From The Boondocks"
|
180
|
+
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
|
181
|
+
def titleize(word)
|
182
|
+
humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
|
183
|
+
end
|
184
|
+
|
185
|
+
# Create the name of a table like Rails does for models to table names. This method
|
186
|
+
# uses the +pluralize+ method on the last word in the string.
|
187
|
+
#
|
188
|
+
# Examples
|
189
|
+
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
|
190
|
+
# "egg_and_ham".tableize # => "egg_and_hams"
|
191
|
+
# "fancyCategory".tableize # => "fancy_categories"
|
192
|
+
def tableize(class_name)
|
193
|
+
pluralize(underscore(class_name))
|
194
|
+
end
|
195
|
+
|
196
|
+
# Create a class name from a plural table name like Rails does for table names to models.
|
197
|
+
# Note that this returns a string and not a Class. (To convert to an actual class
|
198
|
+
# follow +classify+ with +constantize+.)
|
199
|
+
#
|
200
|
+
# Examples:
|
201
|
+
# "egg_and_hams".classify # => "EggAndHam"
|
202
|
+
# "posts".classify # => "Post"
|
203
|
+
#
|
204
|
+
# Singular names are not handled correctly:
|
205
|
+
# "business".classify # => "Busines"
|
206
|
+
def classify(table_name)
|
207
|
+
# strip out any leading schema name
|
208
|
+
camelize(singularize(table_name.to_s.sub(/.*\./, '')))
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
module DataMapper
|
2
|
+
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
|
3
|
+
# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
|
4
|
+
# in inflections.rb.
|
5
|
+
#
|
6
|
+
# The Rails core team has stated patches for the inflections library will not be accepted
|
7
|
+
# in order to avoid breaking legacy applications which may be relying on errant inflections.
|
8
|
+
# If you discover an incorrect inflection and require it for your application, you'll need
|
9
|
+
# to correct it yourself (explained below).
|
10
|
+
module Inflector
|
11
|
+
extend self
|
12
|
+
|
13
|
+
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
|
14
|
+
# is set to <tt>:lower</tt> then +camelize+ produces lowerCamelCase.
|
15
|
+
#
|
16
|
+
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
# "active_record".camelize # => "ActiveRecord"
|
20
|
+
# "active_record".camelize(:lower) # => "activeRecord"
|
21
|
+
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
22
|
+
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
23
|
+
#
|
24
|
+
# As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
|
25
|
+
# though there are cases where that does not hold:
|
26
|
+
#
|
27
|
+
# "SSLError".underscore.camelize # => "SslError"
|
28
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
29
|
+
if first_letter_in_uppercase
|
30
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
31
|
+
else
|
32
|
+
lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Makes an underscored, lowercase form from the expression in the string.
|
37
|
+
#
|
38
|
+
# Changes '::' to '/' to convert namespaces to paths.
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
# "ActiveRecord".underscore # => "active_record"
|
42
|
+
# "ActiveRecord::Errors".underscore # => active_record/errors
|
43
|
+
#
|
44
|
+
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
|
45
|
+
# though there are cases where that does not hold:
|
46
|
+
#
|
47
|
+
# "SSLError".underscore.camelize # => "SslError"
|
48
|
+
def underscore(camel_cased_word)
|
49
|
+
word = camel_cased_word.to_s.dup
|
50
|
+
word.gsub!(/::/, '/')
|
51
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
52
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
53
|
+
word.tr!("-", "_")
|
54
|
+
word.downcase!
|
55
|
+
word
|
56
|
+
end
|
57
|
+
|
58
|
+
# Replaces underscores with dashes in the string.
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# "puni_puni" # => "puni-puni"
|
62
|
+
def dasherize(underscored_word)
|
63
|
+
underscored_word.gsub(/_/, '-')
|
64
|
+
end
|
65
|
+
|
66
|
+
# Removes the module part from the expression in the string.
|
67
|
+
#
|
68
|
+
# Examples:
|
69
|
+
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
|
70
|
+
# "Inflections".demodulize # => "Inflections"
|
71
|
+
def demodulize(class_name_in_module)
|
72
|
+
class_name_in_module.to_s.gsub(/^.*::/, '')
|
73
|
+
end
|
74
|
+
|
75
|
+
# Creates a foreign key name from a class name.
|
76
|
+
# +separate_class_name_and_id_with_underscore+ sets whether
|
77
|
+
# the method should put '_' between the name and 'id'.
|
78
|
+
#
|
79
|
+
# Examples:
|
80
|
+
# "Message".foreign_key # => "message_id"
|
81
|
+
# "Message".foreign_key(false) # => "messageid"
|
82
|
+
# "Admin::Post".foreign_key # => "post_id"
|
83
|
+
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
84
|
+
underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
|
85
|
+
end
|
86
|
+
|
87
|
+
# Ruby 1.9 introduces an inherit argument for Module#const_get and
|
88
|
+
# #const_defined? and changes their default behavior.
|
89
|
+
if Module.method(:const_get).arity == 1
|
90
|
+
# Tries to find a constant with the name specified in the argument string:
|
91
|
+
#
|
92
|
+
# "Module".constantize # => Module
|
93
|
+
# "Test::Unit".constantize # => Test::Unit
|
94
|
+
#
|
95
|
+
# The name is assumed to be the one of a top-level constant, no matter whether
|
96
|
+
# it starts with "::" or not. No lexical context is taken into account:
|
97
|
+
#
|
98
|
+
# C = 'outside'
|
99
|
+
# module M
|
100
|
+
# C = 'inside'
|
101
|
+
# C # => 'inside'
|
102
|
+
# "C".constantize # => 'outside', same as ::C
|
103
|
+
# end
|
104
|
+
#
|
105
|
+
# NameError is raised when the name is not in CamelCase or the constant is
|
106
|
+
# unknown.
|
107
|
+
def constantize(camel_cased_word)
|
108
|
+
names = camel_cased_word.split('::')
|
109
|
+
names.shift if names.empty? || names.first.empty?
|
110
|
+
|
111
|
+
constant = Object
|
112
|
+
names.each do |name|
|
113
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
114
|
+
end
|
115
|
+
constant
|
116
|
+
end
|
117
|
+
else
|
118
|
+
def constantize(camel_cased_word) #:nodoc:
|
119
|
+
names = camel_cased_word.split('::')
|
120
|
+
names.shift if names.empty? || names.first.empty?
|
121
|
+
|
122
|
+
constant = Object
|
123
|
+
names.each do |name|
|
124
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
125
|
+
end
|
126
|
+
constant
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Turns a number into an ordinal string used to denote the position in an
|
131
|
+
# ordered sequence such as 1st, 2nd, 3rd, 4th.
|
132
|
+
#
|
133
|
+
# Examples:
|
134
|
+
# ordinalize(1) # => "1st"
|
135
|
+
# ordinalize(2) # => "2nd"
|
136
|
+
# ordinalize(1002) # => "1002nd"
|
137
|
+
# ordinalize(1003) # => "1003rd"
|
138
|
+
def ordinalize(number)
|
139
|
+
if (11..13).include?(number.to_i % 100)
|
140
|
+
"#{number}th"
|
141
|
+
else
|
142
|
+
case number.to_i % 10
|
143
|
+
when 1; "#{number}st"
|
144
|
+
when 2; "#{number}nd"
|
145
|
+
when 3; "#{number}rd"
|
146
|
+
else "#{number}th"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|