Module: DataMapper::Model::Property
- Extended by:
- Chainable
- Defined in:
- lib/dm-core/model/property.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#field_naming_convention(repository_name = default_storage_name) ⇒ #call
Gets the field naming conventions for this resource in the given Repository.
-
#key(repository_name = default_repository_name) ⇒ Array
Gets the list of key fields for this Model in
repository_name
. - #key_conditions(repository, key) ⇒ Object private
-
#properties(repository_name = default_repository_name) ⇒ PropertySet
Gets a list of all properties that have been defined on this Model in the requested repository.
- #properties_with_subclasses(repository_name = default_repository_name) ⇒ Object private
-
#property(name, type, options = {}) ⇒ Property
Defines a Property on the Resource.
- #serial(repository_name = default_repository_name) ⇒ Object
Methods included from Chainable
Class Method Details
.extended(model) ⇒ Object
11 12 13 14 |
# File 'lib/dm-core/model/property.rb', line 11 def self.extended(model) model.instance_variable_set(:@properties, {}) model.instance_variable_set(:@field_naming_conventions, {}) end |
Instance Method Details
#field_naming_convention(repository_name = default_storage_name) ⇒ #call
Gets the field naming conventions for this resource in the given Repository
159 160 161 |
# File 'lib/dm-core/model/property.rb', line 159 def field_naming_convention(repository_name = default_storage_name) @field_naming_conventions[repository_name] ||= repository(repository_name).adapter.field_naming_convention end |
#key(repository_name = default_repository_name) ⇒ Array
Gets the list of key fields for this Model in repository_name
140 141 142 |
# File 'lib/dm-core/model/property.rb', line 140 def key(repository_name = default_repository_name) properties(repository_name).key end |
#key_conditions(repository, key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
177 178 179 180 |
# File 'lib/dm-core/model/property.rb', line 177 def key_conditions(repository, key) conditions = self.key(repository.name).zip(key.nil? ? [] : key) DataMapper::Ext::Array.to_hash(conditions) end |
#properties(repository_name = default_repository_name) ⇒ PropertySet
Gets a list of all properties that have been defined on this Model in the requested repository
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dm-core/model/property.rb', line 116 def properties(repository_name = default_repository_name) # TODO: create PropertySet#copy that will copy the properties, but assign the # new Relationship objects to a supplied repository and model. dup does not really # do what is needed repository_name = repository_name.to_sym default_repository_name = self.default_repository_name @properties[repository_name] ||= if repository_name == default_repository_name PropertySet.new else properties(default_repository_name).dup end end |
#properties_with_subclasses(repository_name = default_repository_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dm-core/model/property.rb', line 164 def properties_with_subclasses(repository_name = default_repository_name) properties = properties(repository_name).dup descendants.each do |model| model.properties(repository_name).each do |property| properties << property end end properties end |
#property(name, type, options = {}) ⇒ Property
Defines a Property on the Resource
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/dm-core/model/property.rb', line 45 def property(name, type, = {}) if TrueClass == type raise "#{type} is deprecated, use Boolean instead at #{caller[2]}" elsif BigDecimal == type raise "#{type} is deprecated, use Decimal instead at #{caller[2]}" end # if the type can be found within Property then # use that class rather than the primitive unless klass = DataMapper::Property.determine_class(type) raise ArgumentError, "+type+ was #{type.inspect}, which is not a supported type" end property = klass.new(self, name, ) repository_name = self.repository_name properties = properties(repository_name) properties << property # Add property to the other mappings as well if this is for the default # repository. if repository_name == default_repository_name DataMapper::Ext::Hash.except(@properties, default_repository_name).each do |other_repository_name, properties| next if properties.named?(name) # make sure the property is created within the correct repository scope DataMapper.repository(other_repository_name) do properties << klass.new(self, name, , type) end end end # Add the property to the lazy_loads set for this resources repository # only. # TODO Is this right or should we add the lazy contexts to all # repositories? if property.lazy? context = .fetch(:lazy, :default) context = :default if context == true Array(context).each do |context| properties.lazy_context(context) << property end end # add the property to the child classes only if the property was # added after the child classes' properties have been copied from # the parent descendants.each do |descendant| descendant.properties(repository_name) << property end create_reader_for(property) create_writer_for(property) # FIXME: explicit return needed for YARD to parse this properly return property end |
#serial(repository_name = default_repository_name) ⇒ Object
145 146 147 |
# File 'lib/dm-core/model/property.rb', line 145 def serial(repository_name = default_repository_name) key(repository_name).detect { |property| property.serial? } end |