Module: DataMapper::Model::Property

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

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)


241
242
243
244
245
246
247
# File 'lib/dm-core/model/property.rb', line 241

def method_missing(method, *args, &block)
  if property = properties(repository_name)[method]
    return property
  end

  super
end

Class Method Details

.extended(model) ⇒ Object


9
10
11
12
13
# File 'lib/dm-core/model/property.rb', line 9

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

  • repository_name (String, Symbol) (defaults to: default_storage_name)

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

Returns:

  • (#call)

    The naming convention for the given Repository


155
156
157
# File 'lib/dm-core/model/property.rb', line 155

def field_naming_convention(repository_name = default_storage_name)
  @field_naming_conventions[repository_name] ||= repository(repository_name).adapter.field_naming_convention
end

#inherited(model) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dm-core/model/property.rb', line 15

def inherited(model)
  model.instance_variable_set(:@properties,               {})
  model.instance_variable_set(:@field_naming_conventions, @field_naming_conventions.dup)

  @properties.each do |repository_name, properties|
    model_properties = model.properties(repository_name)
    properties.each { |property| model_properties << property }
  end

  super
end

#key(repository_name = default_repository_name) ⇒ Array

Gets the list of key fields for this Model in repository_name

Parameters:

  • repository_name (String) (defaults to: default_repository_name)

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

Returns:

  • (Array)

    The list of key fields for this Model in repository_name


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

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.


173
174
175
# File 'lib/dm-core/model/property.rb', line 173

def key_conditions(repository, key)
  Hash[self.key(repository.name).zip(Array(key))]
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

Parameters:

  • repository_name (Symbol, String) (defaults to: default_repository_name)

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

Returns:

  • (PropertySet)

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


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dm-core/model/property.rb', line 112

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.


160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dm-core/model/property.rb', line 160

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

Parameters:

  • name (Symbol)

    the name for which to call this property

  • type (Class)

    the ruby type to define this property as

  • options (Hash(Symbol => String)) (defaults to: {})

    a hash of available options

Returns:

See Also:


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

def property(name, type, options = {})
  # if the type can be found within Property then
  # use that class rather than the primitive
  klass = DataMapper::Property.determine_class(type)

  unless klass
    raise ArgumentError, "+type+ was #{type.inspect}, which is not a supported type"
  end

  property = klass.new(self, name, options)

  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
    other_repository_properties = DataMapper::Ext::Hash.except(@properties, default_repository_name)

    other_repository_properties.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, 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

    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


141
142
143
# File 'lib/dm-core/model/property.rb', line 141

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