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,856 @@
|
|
1
|
+
module DataMapper
|
2
|
+
# = Properties
|
3
|
+
# Properties for a model are not derived from a database structure, but
|
4
|
+
# instead explicitly declared inside your model class definitions. These
|
5
|
+
# properties then map (or, if using automigrate, generate) fields in your
|
6
|
+
# repository/database.
|
7
|
+
#
|
8
|
+
# If you are coming to DataMapper from another ORM framework, such as
|
9
|
+
# ActiveRecord, this may be a fundamental difference in thinking to you.
|
10
|
+
# However, there are several advantages to defining your properties in your
|
11
|
+
# models:
|
12
|
+
#
|
13
|
+
# * information about your model is centralized in one place: rather than
|
14
|
+
# having to dig out migrations, xml or other configuration files.
|
15
|
+
# * use of mixins can be applied to model properties: better code reuse
|
16
|
+
# * having information centralized in your models, encourages you and the
|
17
|
+
# developers on your team to take a model-centric view of development.
|
18
|
+
# * it provides the ability to use Ruby's access control functions.
|
19
|
+
# * and, because DataMapper only cares about properties explicitly defined
|
20
|
+
# in your models, DataMapper plays well with legacy databases, and shares
|
21
|
+
# databases easily with other applications.
|
22
|
+
#
|
23
|
+
# == Declaring Properties
|
24
|
+
# Inside your class, you call the property method for each property you want
|
25
|
+
# to add. The only two required arguments are the name and type, everything
|
26
|
+
# else is optional.
|
27
|
+
#
|
28
|
+
# class Post
|
29
|
+
# include DataMapper::Resource
|
30
|
+
#
|
31
|
+
# property :title, String, :required => true # Cannot be null
|
32
|
+
# property :publish, Boolean, :default => false # Default value for new records is false
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# By default, DataMapper supports the following primitive (Ruby) types
|
36
|
+
# also called core properties:
|
37
|
+
#
|
38
|
+
# * Boolean
|
39
|
+
# * Class (datastore primitive is the same as String. Used for Inheritance)
|
40
|
+
# * Date
|
41
|
+
# * DateTime
|
42
|
+
# * Decimal
|
43
|
+
# * Float
|
44
|
+
# * Integer
|
45
|
+
# * Object (marshalled out during serialization)
|
46
|
+
# * String (default length is 50)
|
47
|
+
# * Text (limit of 65k characters by default)
|
48
|
+
# * Time
|
49
|
+
#
|
50
|
+
# == Limiting Access
|
51
|
+
# Property access control is uses the same terminology Ruby does. Properties
|
52
|
+
# are public by default, but can also be declared private or protected as
|
53
|
+
# needed (via the :accessor option).
|
54
|
+
#
|
55
|
+
# class Post
|
56
|
+
# include DataMapper::Resource
|
57
|
+
#
|
58
|
+
# property :title, String, :accessor => :private # Both reader and writer are private
|
59
|
+
# property :body, Text, :accessor => :protected # Both reader and writer are protected
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# Access control is also analogous to Ruby attribute readers and writers, and can
|
63
|
+
# be declared using :reader and :writer, in addition to :accessor.
|
64
|
+
#
|
65
|
+
# class Post
|
66
|
+
# include DataMapper::Resource
|
67
|
+
#
|
68
|
+
# property :title, String, :writer => :private # Only writer is private
|
69
|
+
# property :tags, String, :reader => :protected # Only reader is protected
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# == Overriding Accessors
|
73
|
+
# The reader/writer for any property can be overridden in the same manner that Ruby
|
74
|
+
# attr readers/writers can be. After the property is defined, just add your custom
|
75
|
+
# reader or writer:
|
76
|
+
#
|
77
|
+
# class Post
|
78
|
+
# include DataMapper::Resource
|
79
|
+
#
|
80
|
+
# property :title, String
|
81
|
+
#
|
82
|
+
# def title=(new_title)
|
83
|
+
# raise ArgumentError if new_title != 'Lee is l337'
|
84
|
+
# super(new_title)
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# Calling super ensures that any validators defined for the property are kept active.
|
89
|
+
#
|
90
|
+
# == Lazy Loading
|
91
|
+
# By default, some properties are not loaded when an object is fetched in
|
92
|
+
# DataMapper. These lazily loaded properties are fetched on demand when their
|
93
|
+
# accessor is called for the first time (as it is often unnecessary to
|
94
|
+
# instantiate -every- property -every- time an object is loaded). For
|
95
|
+
# instance, DataMapper::Property::Text fields are lazy loading by default,
|
96
|
+
# although you can over-ride this behavior if you wish:
|
97
|
+
#
|
98
|
+
# Example:
|
99
|
+
#
|
100
|
+
# class Post
|
101
|
+
# include DataMapper::Resource
|
102
|
+
#
|
103
|
+
# property :title, String # Loads normally
|
104
|
+
# property :body, Text # Is lazily loaded by default
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# If you want to over-ride the lazy loading on any field you can set it to a
|
108
|
+
# context or false to disable it with the :lazy option. Contexts allow
|
109
|
+
# multiple lazy properties to be loaded at one time. If you set :lazy to
|
110
|
+
# true, it is placed in the :default context
|
111
|
+
#
|
112
|
+
# class Post
|
113
|
+
# include DataMapper::Resource
|
114
|
+
#
|
115
|
+
# property :title, String # Loads normally
|
116
|
+
# property :body, Text, :lazy => false # The default is now over-ridden
|
117
|
+
# property :comment, String, :lazy => [ :detailed ] # Loads in the :detailed context
|
118
|
+
# property :author, String, :lazy => [ :summary, :detailed ] # Loads in :summary & :detailed context
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# Delaying the request for lazy-loaded attributes even applies to objects
|
122
|
+
# accessed through associations. In a sense, DataMapper anticipates that
|
123
|
+
# you will likely be iterating over objects in associations and rolls all
|
124
|
+
# of the load commands for lazy-loaded properties into one request from
|
125
|
+
# the database.
|
126
|
+
#
|
127
|
+
# Example:
|
128
|
+
#
|
129
|
+
# Widget.get(1).components
|
130
|
+
# # loads when the post object is pulled from database, by default
|
131
|
+
#
|
132
|
+
# Widget.get(1).components.first.body
|
133
|
+
# # loads the values for the body property on all objects in the
|
134
|
+
# # association, rather than just this one.
|
135
|
+
#
|
136
|
+
# Widget.get(1).components.first.comment
|
137
|
+
# # loads both comment and author for all objects in the association
|
138
|
+
# # since they are both in the :detailed context
|
139
|
+
#
|
140
|
+
# == Keys
|
141
|
+
# Properties can be declared as primary or natural keys on a table.
|
142
|
+
# You should a property as the primary key of the table:
|
143
|
+
#
|
144
|
+
# Examples:
|
145
|
+
#
|
146
|
+
# property :id, Serial # auto-incrementing key
|
147
|
+
# property :legacy_pk, String, :key => true # 'natural' key
|
148
|
+
#
|
149
|
+
# This is roughly equivalent to ActiveRecord's <tt>set_primary_key</tt>,
|
150
|
+
# though non-integer data types may be used, thus DataMapper supports natural
|
151
|
+
# keys. When a property is declared as a natural key, accessing the object
|
152
|
+
# using the indexer syntax <tt>Class[key]</tt> remains valid.
|
153
|
+
#
|
154
|
+
# User.get(1)
|
155
|
+
# # when :id is the primary key on the users table
|
156
|
+
# User.get('bill')
|
157
|
+
# # when :name is the primary (natural) key on the users table
|
158
|
+
#
|
159
|
+
# == Indices
|
160
|
+
# You can add indices for your properties by using the <tt>:index</tt>
|
161
|
+
# option. If you use <tt>true</tt> as the option value, the index will be
|
162
|
+
# automatically named. If you want to name the index yourself, use a symbol
|
163
|
+
# as the value.
|
164
|
+
#
|
165
|
+
# property :last_name, String, :index => true
|
166
|
+
# property :first_name, String, :index => :name
|
167
|
+
#
|
168
|
+
# You can create multi-column composite indices by using the same symbol in
|
169
|
+
# all the columns belonging to the index. The columns will appear in the
|
170
|
+
# index in the order they are declared.
|
171
|
+
#
|
172
|
+
# property :last_name, String, :index => :name
|
173
|
+
# property :first_name, String, :index => :name
|
174
|
+
# # => index on (last_name, first_name)
|
175
|
+
#
|
176
|
+
# If you want to make the indices unique, use <tt>:unique_index</tt> instead
|
177
|
+
# of <tt>:index</tt>
|
178
|
+
#
|
179
|
+
# == Inferred Validations
|
180
|
+
# If you require the dm-validations plugin, auto-validations will
|
181
|
+
# automatically be mixed-in in to your model classes: validation rules that
|
182
|
+
# are inferred when properties are declared with specific column restrictions.
|
183
|
+
#
|
184
|
+
# class Post
|
185
|
+
# include DataMapper::Resource
|
186
|
+
#
|
187
|
+
# property :title, String, :length => 250, :min => 0, :max => 250
|
188
|
+
# # => infers 'validates_length :title'
|
189
|
+
#
|
190
|
+
# property :title, String, :required => true
|
191
|
+
# # => infers 'validates_present :title'
|
192
|
+
#
|
193
|
+
# property :email, String, :format => :email_address
|
194
|
+
# # => infers 'validates_format :email, :with => :email_address'
|
195
|
+
#
|
196
|
+
# property :title, String, :length => 255, :required => true
|
197
|
+
# # => infers both 'validates_length' as well as 'validates_present'
|
198
|
+
# # better: property :title, String, :length => 1..255
|
199
|
+
# end
|
200
|
+
#
|
201
|
+
# This functionality is available with the dm-validations gem. For more information
|
202
|
+
# about validations, check the documentation for dm-validations.
|
203
|
+
#
|
204
|
+
# == Default Values
|
205
|
+
# To set a default for a property, use the <tt>:default</tt> key. The
|
206
|
+
# property will be set to the value associated with that key the first time
|
207
|
+
# it is accessed, or when the resource is saved if it hasn't been set with
|
208
|
+
# another value already. This value can be a static value, such as 'hello'
|
209
|
+
# but it can also be a proc that will be evaluated when the property is read
|
210
|
+
# before its value has been set. The property is set to the return of the
|
211
|
+
# proc. The proc is passed two values, the resource the property is being set
|
212
|
+
# for and the property itself.
|
213
|
+
#
|
214
|
+
# property :display_name, String, :default => lambda { |resource, property| resource.login }
|
215
|
+
#
|
216
|
+
# Word of warning. Don't try to read the value of the property you're setting
|
217
|
+
# the default for in the proc. An infinite loop will ensue.
|
218
|
+
#
|
219
|
+
# == Embedded Values (not implemented yet)
|
220
|
+
# As an alternative to extraneous has_one relationships, consider using an
|
221
|
+
# EmbeddedValue.
|
222
|
+
#
|
223
|
+
# == Property options reference
|
224
|
+
#
|
225
|
+
# :accessor if false, neither reader nor writer methods are
|
226
|
+
# created for this property
|
227
|
+
#
|
228
|
+
# :reader if false, reader method is not created for this property
|
229
|
+
#
|
230
|
+
# :writer if false, writer method is not created for this property
|
231
|
+
#
|
232
|
+
# :lazy if true, property value is only loaded when on first read
|
233
|
+
# if false, property value is always loaded
|
234
|
+
# if a symbol, property value is loaded with other properties
|
235
|
+
# in the same group
|
236
|
+
#
|
237
|
+
# :default default value of this property
|
238
|
+
#
|
239
|
+
# :allow_nil if true, property may have a nil value on save
|
240
|
+
#
|
241
|
+
# :key name of the key associated with this property.
|
242
|
+
#
|
243
|
+
# :field field in the data-store which the property corresponds to
|
244
|
+
#
|
245
|
+
# :length string field length
|
246
|
+
#
|
247
|
+
# :format format for autovalidation. Use with dm-validations plugin.
|
248
|
+
#
|
249
|
+
# :index if true, index is created for the property. If a Symbol, index
|
250
|
+
# is named after Symbol value instead of being based on property name.
|
251
|
+
#
|
252
|
+
# :unique_index true specifies that index on this property should be unique
|
253
|
+
#
|
254
|
+
# :auto_validation if true, automatic validation is performed on the property
|
255
|
+
#
|
256
|
+
# :validates validation context. Use together with dm-validations.
|
257
|
+
#
|
258
|
+
# :unique if true, property column is unique. Properties of type Serial
|
259
|
+
# are unique by default.
|
260
|
+
#
|
261
|
+
# :precision Indicates the number of significant digits. Usually only makes sense
|
262
|
+
# for float type properties. Must be >= scale option value. Default is 10.
|
263
|
+
#
|
264
|
+
# :scale The number of significant digits to the right of the decimal point.
|
265
|
+
# Only makes sense for float type properties. Must be > 0.
|
266
|
+
# Default is nil for Float type and 10 for BigDecimal
|
267
|
+
#
|
268
|
+
# == Overriding default Property options
|
269
|
+
#
|
270
|
+
# There is the ability to reconfigure a Property and it's subclasses by explicitly
|
271
|
+
# setting a value in the Property, eg:
|
272
|
+
#
|
273
|
+
# # set all String properties to have a default length of 255
|
274
|
+
# DataMapper::Property::String.length(255)
|
275
|
+
#
|
276
|
+
# # set all Boolean properties to not allow nil (force true or false)
|
277
|
+
# DataMapper::Property::Boolean.allow_nil(false)
|
278
|
+
#
|
279
|
+
# # set all properties to be required by default
|
280
|
+
# DataMapper::Property.required(true)
|
281
|
+
#
|
282
|
+
# # turn off auto-validation for all properties by default
|
283
|
+
# DataMapper::Property.auto_validation(false)
|
284
|
+
#
|
285
|
+
# # set all mutator methods to be private by default
|
286
|
+
# DataMapper::Property.writer(:private)
|
287
|
+
#
|
288
|
+
# Please note that this has no effect when a subclass has explicitly
|
289
|
+
# defined it's own option. For example, setting the String length to
|
290
|
+
# 255 will not affect the Text property even though it inherits from
|
291
|
+
# String, because it sets it's own default length to 65535.
|
292
|
+
#
|
293
|
+
# == Misc. Notes
|
294
|
+
# * Properties declared as strings will default to a length of 50, rather than
|
295
|
+
# 255 (typical max varchar column size). To overload the default, pass
|
296
|
+
# <tt>:length => 255</tt> or <tt>:length => 0..255</tt>. Since DataMapper
|
297
|
+
# does not introspect for properties, this means that legacy database tables
|
298
|
+
# may need their <tt>String</tt> columns defined with a <tt>:length</tt> so
|
299
|
+
# that DM does not apply an un-needed length validation, or allow overflow.
|
300
|
+
# * You may declare a Property with the data-type of <tt>Class</tt>.
|
301
|
+
# see SingleTableInheritance for more on how to use <tt>Class</tt> columns.
|
302
|
+
class Property
|
303
|
+
include Subject, DataMapper::Assertions
|
304
|
+
extend Equalizer
|
305
|
+
|
306
|
+
equalize :model, :name, :options
|
307
|
+
|
308
|
+
PRIMITIVES = [
|
309
|
+
TrueClass,
|
310
|
+
::String,
|
311
|
+
::Float,
|
312
|
+
::Integer,
|
313
|
+
::BigDecimal,
|
314
|
+
::DateTime,
|
315
|
+
::Date,
|
316
|
+
::Time,
|
317
|
+
::Class
|
318
|
+
].to_set.freeze
|
319
|
+
|
320
|
+
OPTIONS = %i(
|
321
|
+
load_as dump_as
|
322
|
+
accessor reader writer
|
323
|
+
lazy default key field
|
324
|
+
index unique_index
|
325
|
+
unique allow_nil allow_blank required
|
326
|
+
).freeze
|
327
|
+
|
328
|
+
# Possible :visibility option values
|
329
|
+
VISIBILITY_OPTIONS = %i(public protected private).to_set.freeze
|
330
|
+
|
331
|
+
# Invalid property names
|
332
|
+
INVALID_NAMES = (Resource.instance_methods +
|
333
|
+
Resource.private_instance_methods +
|
334
|
+
Query::OPTIONS.to_a
|
335
|
+
).map(&:to_s)
|
336
|
+
|
337
|
+
attr_reader :load_as, :dump_as, :model, :name, :instance_variable_name,
|
338
|
+
:reader_visibility, :writer_visibility, :options,
|
339
|
+
:default, :repository_name, :allow_nil, :allow_blank, :required
|
340
|
+
|
341
|
+
alias_method :load_class, :load_as
|
342
|
+
alias_method :dump_class, :dump_as
|
343
|
+
|
344
|
+
class << self
|
345
|
+
extend Deprecate
|
346
|
+
|
347
|
+
deprecate :all_descendants, :descendants
|
348
|
+
|
349
|
+
# @api semipublic
|
350
|
+
def determine_class(type)
|
351
|
+
return type if type < DataMapper::Property::Object
|
352
|
+
|
353
|
+
find_class(DataMapper::Inflector.demodulize(type.name))
|
354
|
+
end
|
355
|
+
|
356
|
+
# @api private
|
357
|
+
def demodulized_names
|
358
|
+
@demodulized_names ||= {}
|
359
|
+
end
|
360
|
+
|
361
|
+
# @api semipublic
|
362
|
+
def find_class(name)
|
363
|
+
klass = demodulized_names[name]
|
364
|
+
klass ||= const_get(name) if const_defined?(name)
|
365
|
+
klass
|
366
|
+
end
|
367
|
+
|
368
|
+
# @api public
|
369
|
+
def descendants
|
370
|
+
@descendants ||= DescendantSet.new
|
371
|
+
end
|
372
|
+
|
373
|
+
# @api private
|
374
|
+
def inherited(descendant)
|
375
|
+
# Descendants is a tree rooted in DataMapper::Property that tracks
|
376
|
+
# inheritance. We pre-calculate each comparison value (demodulized
|
377
|
+
# class name) to achieve a Hash[]-time lookup, rather than walk the
|
378
|
+
# entire descendant tree and calculate names on-demand (expensive,
|
379
|
+
# redundant).
|
380
|
+
#
|
381
|
+
# Since the algorithm relegates property class name lookups to a flat
|
382
|
+
# namespace, we need to ensure properties defined outside of DM don't
|
383
|
+
# override built-ins (Serial, String, etc) by merely defining a property
|
384
|
+
# of a same name. We avoid this by only ever adding to the lookup
|
385
|
+
# table. Given that DM loads its own property classes first, we can
|
386
|
+
# assume that their names are "reserved" when added to the table.
|
387
|
+
#
|
388
|
+
# External property authors who want to provide "replacements" for
|
389
|
+
# builtins (e.g. in a non-DM-supported adapter) should follow the
|
390
|
+
# convention of wrapping those properties in a module, and include'ing
|
391
|
+
# the module on the model class directly. This bypasses the DM-hooked
|
392
|
+
# const_missing lookup that would normally check this table.
|
393
|
+
descendants << descendant
|
394
|
+
|
395
|
+
Property.demodulized_names[DataMapper::Inflector.demodulize(descendant.name)] ||= descendant
|
396
|
+
|
397
|
+
# inherit accepted options
|
398
|
+
descendant.accepted_options.concat(accepted_options)
|
399
|
+
|
400
|
+
# inherit the option values
|
401
|
+
options.each { |key, value| descendant.send(key, value) }
|
402
|
+
|
403
|
+
super
|
404
|
+
end
|
405
|
+
|
406
|
+
# @api public
|
407
|
+
def accepted_options
|
408
|
+
@accepted_options ||= []
|
409
|
+
end
|
410
|
+
|
411
|
+
# @api public
|
412
|
+
def accept_options(*args)
|
413
|
+
accepted_options.concat(args)
|
414
|
+
|
415
|
+
# create methods for each new option
|
416
|
+
args.each do |property_option|
|
417
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
418
|
+
def self.#{property_option}(value = Undefined) # def self.unique(value = Undefined)
|
419
|
+
return @#{property_option} if value.equal?(Undefined) # return @unique if value.equal?(Undefined)
|
420
|
+
descendants.each do |descendant| # descendants.each do |descendant|
|
421
|
+
unless descendant.instance_variable_defined?(:@#{property_option}) # unless descendant.instance_variable_defined?(:@unique)
|
422
|
+
descendant.#{property_option}(value) # descendant.unique(value)
|
423
|
+
end # end
|
424
|
+
end # end
|
425
|
+
@#{property_option} = value # @unique = value
|
426
|
+
end # end
|
427
|
+
RUBY
|
428
|
+
end
|
429
|
+
|
430
|
+
descendants.each { |descendant| descendant.accepted_options.concat(args) }
|
431
|
+
end
|
432
|
+
|
433
|
+
# @api private
|
434
|
+
def nullable(*_args)
|
435
|
+
# :required is preferable to :allow_nil, but :nullable maps precisely to :allow_nil
|
436
|
+
raise "#nullable is deprecated, use #required instead (#{caller.first})"
|
437
|
+
end
|
438
|
+
|
439
|
+
# Gives all the options set on this property
|
440
|
+
#
|
441
|
+
# @return [Hash] with all options and their values set on this property
|
442
|
+
#
|
443
|
+
# @api public
|
444
|
+
def options
|
445
|
+
options = {}
|
446
|
+
accepted_options.each do |name|
|
447
|
+
options[name] = send(name) if instance_variable_defined?("@#{name}")
|
448
|
+
end
|
449
|
+
options
|
450
|
+
end
|
451
|
+
|
452
|
+
# @api deprecated
|
453
|
+
def primitive(*args)
|
454
|
+
warn "DataMapper::Property.primitive is deprecated, use .load_as instead (#{caller.first})"
|
455
|
+
load_as(*args)
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
accept_options(*Property::OPTIONS)
|
460
|
+
|
461
|
+
# A hook to allow properties to extend or modify the model it's bound to.
|
462
|
+
# Implementations are not supposed to modify the state of the property
|
463
|
+
# class, and should produce no side-effects on the property instance.
|
464
|
+
def bind
|
465
|
+
# no op
|
466
|
+
end
|
467
|
+
|
468
|
+
# Supplies the field in the data-store which the property corresponds to
|
469
|
+
#
|
470
|
+
# @return [String] name of field in data-store
|
471
|
+
#
|
472
|
+
# @api semipublic
|
473
|
+
def field(repository_name = nil)
|
474
|
+
raise "Passing in +repository_name+ to #{self.class}#field is deprecated (#{caller.first})" if repository_name
|
475
|
+
|
476
|
+
# defer setting the field with the adapter specific naming
|
477
|
+
# conventions until after the adapter has been setup
|
478
|
+
@field ||= model.field_naming_convention(self.repository_name).call(self).freeze
|
479
|
+
end
|
480
|
+
|
481
|
+
# Returns true if property is unique. Serial properties and keys
|
482
|
+
# are unique by default.
|
483
|
+
#
|
484
|
+
# @return [Boolean]
|
485
|
+
# true if property has uniq index defined, false otherwise
|
486
|
+
#
|
487
|
+
# @api public
|
488
|
+
def unique?
|
489
|
+
!!@unique
|
490
|
+
end
|
491
|
+
|
492
|
+
# Returns index name if property has index.
|
493
|
+
#
|
494
|
+
# @return [Boolean, Symbol, Array]
|
495
|
+
# returns true if property is indexed by itself
|
496
|
+
# returns a Symbol if the property is indexed with other properties
|
497
|
+
# returns an Array if the property belongs to multiple indexes
|
498
|
+
# returns false if the property does not belong to any indexes
|
499
|
+
#
|
500
|
+
# @api public
|
501
|
+
attr_reader :index
|
502
|
+
|
503
|
+
# Returns true if property has unique index. Serial properties and
|
504
|
+
# keys are unique by default.
|
505
|
+
#
|
506
|
+
# @return [Boolean, Symbol, Array]
|
507
|
+
# returns true if property is indexed by itself
|
508
|
+
# returns a Symbol if the property is indexed with other properties
|
509
|
+
# returns an Array if the property belongs to multiple indexes
|
510
|
+
# returns false if the property does not belong to any indexes
|
511
|
+
#
|
512
|
+
# @api public
|
513
|
+
attr_reader :unique_index
|
514
|
+
|
515
|
+
# Returns whether or not the property is to be lazy-loaded
|
516
|
+
#
|
517
|
+
# @return [Boolean]
|
518
|
+
# true if the property is to be lazy-loaded
|
519
|
+
#
|
520
|
+
# @api public
|
521
|
+
def lazy?
|
522
|
+
@lazy
|
523
|
+
end
|
524
|
+
|
525
|
+
# Returns whether or not the property is a key or a part of a key
|
526
|
+
#
|
527
|
+
# @return [Boolean]
|
528
|
+
# true if the property is a key or a part of a key
|
529
|
+
#
|
530
|
+
# @api public
|
531
|
+
def key?
|
532
|
+
@key
|
533
|
+
end
|
534
|
+
|
535
|
+
# Returns whether or not the property is "serial" (auto-incrementing)
|
536
|
+
#
|
537
|
+
# @return [Boolean]
|
538
|
+
# whether or not the property is "serial"
|
539
|
+
#
|
540
|
+
# @api public
|
541
|
+
def serial?
|
542
|
+
@serial
|
543
|
+
end
|
544
|
+
|
545
|
+
# Returns whether or not the property must be non-nil and non-blank
|
546
|
+
#
|
547
|
+
# @return [Boolean]
|
548
|
+
# whether or not the property is required
|
549
|
+
#
|
550
|
+
# @api public
|
551
|
+
def required?
|
552
|
+
@required
|
553
|
+
end
|
554
|
+
|
555
|
+
# Returns whether or not the property can accept 'nil' as it's value
|
556
|
+
#
|
557
|
+
# @return [Boolean]
|
558
|
+
# whether or not the property can accept 'nil'
|
559
|
+
#
|
560
|
+
# @api public
|
561
|
+
def allow_nil?
|
562
|
+
@allow_nil
|
563
|
+
end
|
564
|
+
|
565
|
+
# Returns whether or not the property can be a blank value
|
566
|
+
#
|
567
|
+
# @return [Boolean]
|
568
|
+
# whether or not the property can be blank
|
569
|
+
#
|
570
|
+
# @api public
|
571
|
+
def allow_blank?
|
572
|
+
@allow_blank
|
573
|
+
end
|
574
|
+
|
575
|
+
# Standardized reader method for the property
|
576
|
+
#
|
577
|
+
# @param [Resource] resource
|
578
|
+
# model instance for which this property is to be loaded
|
579
|
+
#
|
580
|
+
# @return [Object]
|
581
|
+
# the value of this property for the provided instance
|
582
|
+
#
|
583
|
+
# @raise [ArgumentError] "+resource+ should be a Resource, but was ...."
|
584
|
+
#
|
585
|
+
# @api private
|
586
|
+
def get(resource)
|
587
|
+
get!(resource)
|
588
|
+
end
|
589
|
+
|
590
|
+
# Fetch the ivar value in the resource
|
591
|
+
#
|
592
|
+
# @param [Resource] resource
|
593
|
+
# model instance for which this property is to be unsafely loaded
|
594
|
+
#
|
595
|
+
# @return [Object]
|
596
|
+
# current @ivar value of this property in +resource+
|
597
|
+
#
|
598
|
+
# @api private
|
599
|
+
def get!(resource)
|
600
|
+
resource.instance_variable_get(instance_variable_name)
|
601
|
+
end
|
602
|
+
|
603
|
+
# Provides a standardized setter method for the property
|
604
|
+
#
|
605
|
+
# @param [Resource] resource
|
606
|
+
# the resource to get the value from
|
607
|
+
# @param [Object] value
|
608
|
+
# the value to set in the resource
|
609
|
+
#
|
610
|
+
# @return [Object]
|
611
|
+
# +value+ after being typecasted according to this property's primitive
|
612
|
+
#
|
613
|
+
# @raise [ArgumentError] "+resource+ should be a Resource, but was ...."
|
614
|
+
#
|
615
|
+
# @api private
|
616
|
+
def set(resource, value)
|
617
|
+
set!(resource, value)
|
618
|
+
end
|
619
|
+
|
620
|
+
# Set the ivar value in the resource
|
621
|
+
#
|
622
|
+
# @param [Resource] resource
|
623
|
+
# the resource to set
|
624
|
+
# @param [Object] value
|
625
|
+
# the value to set in the resource
|
626
|
+
#
|
627
|
+
# @return [Object]
|
628
|
+
# the value set in the resource
|
629
|
+
#
|
630
|
+
# @api private
|
631
|
+
def set!(resource, value)
|
632
|
+
resource.instance_variable_set(instance_variable_name, value)
|
633
|
+
end
|
634
|
+
|
635
|
+
# Check if the attribute corresponding to the property is loaded
|
636
|
+
#
|
637
|
+
# @param [Resource] resource
|
638
|
+
# model instance for which the attribute is to be tested
|
639
|
+
#
|
640
|
+
# @return [Boolean]
|
641
|
+
# true if the attribute is loaded in the resource
|
642
|
+
#
|
643
|
+
# @api private
|
644
|
+
def loaded?(resource)
|
645
|
+
resource.instance_variable_defined?(instance_variable_name)
|
646
|
+
end
|
647
|
+
|
648
|
+
# Loads lazy columns when get or set is called.
|
649
|
+
#
|
650
|
+
# @param [Resource] resource
|
651
|
+
# model instance for which lazy loaded attribute are loaded
|
652
|
+
#
|
653
|
+
# @api private
|
654
|
+
def lazy_load(resource)
|
655
|
+
return if loaded?(resource)
|
656
|
+
|
657
|
+
resource.__send__(:lazy_load, lazy_load_properties)
|
658
|
+
end
|
659
|
+
|
660
|
+
# @api private
|
661
|
+
def lazy_load_properties
|
662
|
+
@lazy_load_properties ||=
|
663
|
+
begin
|
664
|
+
properties = self.properties
|
665
|
+
properties.in_context(lazy? ? [self] : properties.defaults)
|
666
|
+
end
|
667
|
+
end
|
668
|
+
|
669
|
+
# @api private
|
670
|
+
def properties
|
671
|
+
@properties ||= model.properties(repository_name)
|
672
|
+
end
|
673
|
+
|
674
|
+
# @api semipublic
|
675
|
+
def typecast(value)
|
676
|
+
if (value.nil? || value_loaded?(value)) && !respond_to?(:typecast_to_primitive, true)
|
677
|
+
value
|
678
|
+
elsif respond_to?(:typecast_to_primitive, true)
|
679
|
+
typecast_to_primitive(value)
|
680
|
+
end
|
681
|
+
end
|
682
|
+
|
683
|
+
# Test the value to see if it is a valid value for this Property
|
684
|
+
#
|
685
|
+
# @param [Object] value
|
686
|
+
# the value to be tested
|
687
|
+
#
|
688
|
+
# @return [Boolean]
|
689
|
+
# true if the value is valid
|
690
|
+
#
|
691
|
+
# @api semipublic
|
692
|
+
def valid?(value, negated = false)
|
693
|
+
dumped_value = dump(value)
|
694
|
+
|
695
|
+
if required? && dumped_value.nil?
|
696
|
+
negated || false
|
697
|
+
else
|
698
|
+
value_dumped?(dumped_value) || (dumped_value.nil? && (allow_nil? || negated))
|
699
|
+
end
|
700
|
+
end
|
701
|
+
|
702
|
+
# Asserts value is valid
|
703
|
+
#
|
704
|
+
# @param [Object] value
|
705
|
+
# the value to be tested
|
706
|
+
#
|
707
|
+
# @return [Boolean]
|
708
|
+
# true if the value is valid
|
709
|
+
#
|
710
|
+
# @raise [Property::InvalidValueError]
|
711
|
+
# if value is not valid
|
712
|
+
def assert_valid_value(value)
|
713
|
+
raise Property::InvalidValueError.new(self, value) unless valid?(value)
|
714
|
+
|
715
|
+
true
|
716
|
+
end
|
717
|
+
|
718
|
+
# Returns a concise string representation of the property instance.
|
719
|
+
#
|
720
|
+
# @return [String]
|
721
|
+
# Concise string representation of the property instance.
|
722
|
+
#
|
723
|
+
# @api public
|
724
|
+
def inspect
|
725
|
+
"#<#{self.class.name} @model=#{model.inspect} @name=#{name.inspect}>"
|
726
|
+
end
|
727
|
+
|
728
|
+
# Test a value to see if it matches the primitive type
|
729
|
+
#
|
730
|
+
# @param [Object] value
|
731
|
+
# value to test
|
732
|
+
#
|
733
|
+
# @return [Boolean]
|
734
|
+
# true if the value is the correct type
|
735
|
+
#
|
736
|
+
# @api semipublic
|
737
|
+
def primitive?(value)
|
738
|
+
warn "#primitive? is deprecated, use #value_dumped? instead (#{caller.first})"
|
739
|
+
value_dumped?(value)
|
740
|
+
end
|
741
|
+
|
742
|
+
def primitive
|
743
|
+
warn "#primitive is deprecated, use #dump_as instead (#{caller.first})"
|
744
|
+
dump_as
|
745
|
+
end
|
746
|
+
|
747
|
+
# @api semipublic
|
748
|
+
def value_dumped?(value)
|
749
|
+
value.is_a?(dump_as)
|
750
|
+
end
|
751
|
+
|
752
|
+
# @api semipublic
|
753
|
+
def value_loaded?(value)
|
754
|
+
value.is_a?(load_as)
|
755
|
+
end
|
756
|
+
|
757
|
+
# @api semipublic
|
758
|
+
protected def initialize(model, name, options = {})
|
759
|
+
options = options.to_hash.dup
|
760
|
+
|
761
|
+
if INVALID_NAMES.include?(name.to_s) || (is_a?(Boolean) && INVALID_NAMES.include?("#{name}?"))
|
762
|
+
raise ArgumentError,
|
763
|
+
"+name+ was #{name.inspect}, which cannot be used as a property name since it collides with an existing method or a query option"
|
764
|
+
end
|
765
|
+
|
766
|
+
assert_valid_options(options)
|
767
|
+
|
768
|
+
predefined_options = self.class.options
|
769
|
+
|
770
|
+
@repository_name = model.repository_name
|
771
|
+
@model = model
|
772
|
+
@name = name.to_s.chomp('?').to_sym
|
773
|
+
@options = predefined_options.merge(options).freeze
|
774
|
+
@instance_variable_name = "@#{@name}".freeze
|
775
|
+
@load_as = self.class.load_as
|
776
|
+
@dump_as = @options.fetch(:dump_as, @load_as)
|
777
|
+
@field = @options[:field].freeze unless @options[:field].nil?
|
778
|
+
@default = @options[:default]
|
779
|
+
|
780
|
+
@serial = @options.fetch(:serial, false)
|
781
|
+
@key = @options.fetch(:key, @serial)
|
782
|
+
@unique = @options.fetch(:unique, @key ? :key : false)
|
783
|
+
@required = @options.fetch(:required, @key)
|
784
|
+
@allow_nil = @options.fetch(:allow_nil, !@required)
|
785
|
+
@allow_blank = @options.fetch(:allow_blank, !@required)
|
786
|
+
@index = @options.fetch(:index, false)
|
787
|
+
@unique_index = @options.fetch(:unique_index, @unique)
|
788
|
+
@lazy = @options.fetch(:lazy, false) && !@key
|
789
|
+
|
790
|
+
determine_visibility
|
791
|
+
|
792
|
+
bind
|
793
|
+
end
|
794
|
+
|
795
|
+
# @api private
|
796
|
+
protected def assert_valid_options(options)
|
797
|
+
keys = options.keys
|
798
|
+
|
799
|
+
if (unknown_keys = keys - self.class.accepted_options).any?
|
800
|
+
raise ArgumentError, "options #{unknown_keys.map(&:inspect).join(' and ')} are unknown"
|
801
|
+
end
|
802
|
+
|
803
|
+
options.each do |key, value|
|
804
|
+
boolean_value = [true, false].include?(value)
|
805
|
+
|
806
|
+
case key
|
807
|
+
when :field
|
808
|
+
assert_kind_of "options[:#{key}]", value, ::String
|
809
|
+
|
810
|
+
when :default
|
811
|
+
raise ArgumentError, "options[:#{key}] must not be nil" if value.nil?
|
812
|
+
|
813
|
+
when :serial, :key, :allow_nil, :allow_blank, :required, :auto_validation
|
814
|
+
raise ArgumentError, "options[:#{key}] must be either true or false" unless boolean_value
|
815
|
+
|
816
|
+
if key == :required && (keys.include?(:allow_nil) || keys.include?(:allow_blank))
|
817
|
+
raise ArgumentError, 'options[:required] cannot be mixed with :allow_nil or :allow_blank'
|
818
|
+
end
|
819
|
+
|
820
|
+
when :index, :unique_index, :unique, :lazy
|
821
|
+
unless boolean_value || value.is_a?(Symbol) || (value.is_a?(Array) && value.any? && value.all? { |val| val.is_a?(Symbol) })
|
822
|
+
raise ArgumentError, "options[:#{key}] must be either true, false, a Symbol or an Array of Symbols"
|
823
|
+
end
|
824
|
+
|
825
|
+
when :length
|
826
|
+
assert_kind_of "options[:#{key}]", value, Range, ::Integer
|
827
|
+
|
828
|
+
when :size, :precision, :scale
|
829
|
+
assert_kind_of "options[:#{key}]", value, ::Integer
|
830
|
+
|
831
|
+
when :reader, :writer, :accessor
|
832
|
+
assert_kind_of "options[:#{key}]", value, Symbol
|
833
|
+
|
834
|
+
raise ArgumentError, "options[:#{key}] must be #{VISIBILITY_OPTIONS.join(' or ')}" unless VISIBILITY_OPTIONS.include?(value)
|
835
|
+
end
|
836
|
+
end
|
837
|
+
end
|
838
|
+
|
839
|
+
# Assert given visibility value is supported.
|
840
|
+
#
|
841
|
+
# Will raise ArgumentError if this Property's reader and writer
|
842
|
+
# visibilities are not included in VISIBILITY_OPTIONS.
|
843
|
+
#
|
844
|
+
# @return [undefined]
|
845
|
+
#
|
846
|
+
# @raise [ArgumentError] "property visibility must be :public, :protected, or :private"
|
847
|
+
#
|
848
|
+
# @api private
|
849
|
+
protected def determine_visibility
|
850
|
+
default_accessor = @options.fetch(:accessor, :public)
|
851
|
+
|
852
|
+
@reader_visibility = @options.fetch(:reader, default_accessor)
|
853
|
+
@writer_visibility = @options.fetch(:writer, default_accessor)
|
854
|
+
end
|
855
|
+
end
|
856
|
+
end
|