Module: DataMapper::Model::Property

Extended by:
Chainable
Defined in:
lib/dm-core/model/property.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chainable

chainable, extendable

Class Method Details

.extended(model) ⇒ Object



14
15
16
17
18
# File 'lib/dm-core/model/property.rb', line 14

def self.extended(model)
  model.instance_variable_set(:@properties,               {})
  model.instance_variable_set(:@field_naming_conventions, {})
  model.instance_variable_set(:@paranoid_properties,      {})
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

Parameters:

  • (defaults to: default_storage_name)

    the name of the Repository for which the field naming convention will be retrieved

Returns:

  • The naming convention for the given Repository

API:

  • public



149
150
151
# File 'lib/dm-core/model/property.rb', line 149

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

Parameters:

  • (defaults to: default_repository_name)

    The name of the Repository for which the key is to be reported

Returns:

  • The list of key fields for this Model in repository_name

API:

  • public



129
130
131
# File 'lib/dm-core/model/property.rb', line 129

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.

TODO: document

API:

  • private



181
182
183
# File 'lib/dm-core/model/property.rb', line 181

def key_conditions(repository, key)
  self.key(repository.name).zip(key).to_hash
end

#paranoid_propertiesObject

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.

TODO: document

API:

  • private



169
170
171
# File 'lib/dm-core/model/property.rb', line 169

def paranoid_properties
  @paranoid_properties
end

#properties(repository_name = default_repository_name) ⇒ Array

Gets a list of all properties that have been defined on this Model in the requested repository

Parameters:

  • (defaults to: default_repository_name)

    The name of the repository to use. Uses the default Repository if none is specified.

Returns:

  • A list of Properties defined on this Model in the given Repository

API:

  • public



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dm-core/model/property.rb', line 108

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

  @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.

TODO: document

API:

  • private



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dm-core/model/property.rb', line 155

def properties_with_subclasses(repository_name = default_repository_name)
  properties = PropertySet.new

  descendants.each do |model|
    model.properties(repository_name).each do |property|
      properties << property unless properties.named?(property.name)
    end
  end

  properties
end

#property(name, type, options = {}) ⇒ Property

Defines a Property on the Resource

Parameters:

  • the name for which to call this property

  • the type to define this property ass

  • (defaults to: {})

    a hash of available options

Returns:

  • the created Property

See Also:

API:

  • public



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
# File 'lib/dm-core/model/property.rb', line 50

def property(name, type, options = {})
  property = DataMapper::Property.new(self, name, type, options)

  properties(repository_name) << property

  # Add property to the other mappings as well if this is for the default
  # repository.
  if repository_name == default_repository_name
    @properties.except(repository_name).each do |repository_name, properties|
      next if properties.named?(name)

      # make sure the property is created within the correct repository scope
      DataMapper.repository(repository_name) do
        properties << DataMapper::Property.new(self, name, type, options)
      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 = options.fetch(:lazy, :default)
    context = :default if context == true

    properties = properties(repository_name)

    Array(context).each do |context|
      properties.lazy_context(context) << self
    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|
    next if descendant.properties(repository_name).named?(name)
    descendant.properties(repository_name) << property
  end

  create_reader_for(property)
  create_writer_for(property)

  property
end

#serial(repository_name = default_repository_name) ⇒ Object

TODO: document

API:

  • public



135
136
137
# File 'lib/dm-core/model/property.rb', line 135

def serial(repository_name = default_repository_name)
  key(repository_name).detect { |property| property.serial? }
end

#set_paranoid_property(name, &block) ⇒ 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.

TODO: document

API:

  • private



175
176
177
# File 'lib/dm-core/model/property.rb', line 175

def set_paranoid_property(name, &block)
  paranoid_properties[name] = block
end