Class: DataMapper::PropertySet

Inherits:
Array show all
Extended by:
Deprecate
Defined in:
lib/dm-core/property_set.rb

Overview

Set of Property objects, used to associate queries with set of fields it performed over, to represent composite keys (esp. for associations) and so on.

Instance Method Summary collapse

Methods included from Deprecate

deprecate

Methods inherited from Array

#to_hash, #to_mash

Instance Method Details

#<<(property) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/dm-core/property_set.rb', line 37

def <<(property)
  found = named?(property.name)
  add_property(property)

  if found
    superclass_slice(index(property), property)
  else
    super
  end
end

#[](name) ⇒ Object



14
15
16
# File 'lib/dm-core/property_set.rb', line 14

def [](name)
  @properties[name]
end

#[]=(name, property) ⇒ Object



22
23
24
# File 'lib/dm-core/property_set.rb', line 22

def []=(name, property)
  self << property
end

#defaultsObject

TODO: make PropertySet#reject return a PropertySet instance



60
61
62
# File 'lib/dm-core/property_set.rb', line 60

def defaults
  @defaults ||= self.class.new(key | [ discriminator ].compact | reject { |property| property.lazy? }).freeze
end

#discriminatorObject



70
71
72
# File 'lib/dm-core/property_set.rb', line 70

def discriminator
  @discriminator ||= detect { |property| property.kind_of?(Property::Discriminator) || property.type == Types::Discriminator }
end

#field_mapObject

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.



152
153
154
# File 'lib/dm-core/property_set.rb', line 152

def field_map
  map { |property| [ property.field, property ] }.to_hash
end

#get(resource) ⇒ Object



89
90
91
92
# File 'lib/dm-core/property_set.rb', line 89

def get(resource)
  return [] if resource.nil?
  map { |property| resource.__send__(property.name) }
end

#get!(resource) ⇒ Object



95
96
97
# File 'lib/dm-core/property_set.rb', line 95

def get!(resource)
  map { |property| property.get!(resource) }
end

#in_context(properties) ⇒ 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.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dm-core/property_set.rb', line 139

def in_context(properties)
  properties_in_context = properties.map do |property|
    if (contexts = property_contexts(property)).any?
      lazy_contexts.values_at(*contexts)
    else
      property
    end
  end

  properties_in_context.flatten.uniq
end

#include?(property) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dm-core/property_set.rb', line 49

def include?(property)
  named?(property.name)
end

#index(property) ⇒ Object



54
55
56
# File 'lib/dm-core/property_set.rb', line 54

def index(property)
  each_index { |index| break index if at(index).name == property.name }
end

#indexesObject



75
76
77
78
79
# File 'lib/dm-core/property_set.rb', line 75

def indexes
  index_hash = {}
  each { |property| parse_index(property.index, property.field, index_hash) }
  index_hash
end

#keyObject



65
66
67
# File 'lib/dm-core/property_set.rb', line 65

def key
  @key ||= self.class.new(select { |property| property.key? }).freeze
end

#lazy_context(context) ⇒ 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.



134
135
136
# File 'lib/dm-core/property_set.rb', line 134

def lazy_context(context)
  lazy_contexts[context] ||= []
end

#loaded?(resource) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/dm-core/property_set.rb', line 110

def loaded?(resource)
  all? { |property| property.loaded?(resource) }
end

#named?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/dm-core/property_set.rb', line 27

def named?(name)
  @properties.key?(name.to_sym)
end

#property_contexts(property) ⇒ 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.



125
126
127
128
129
130
131
# File 'lib/dm-core/property_set.rb', line 125

def property_contexts(property)
  contexts = []
  lazy_contexts.each do |context, properties|
    contexts << context if properties.include?(property)
  end
  contexts
end

#set(resource, values) ⇒ Object



100
101
102
# File 'lib/dm-core/property_set.rb', line 100

def set(resource, values)
  zip(values) { |property, value| resource.__send__("#{property.name}=", value) }
end

#set!(resource, values) ⇒ Object



105
106
107
# File 'lib/dm-core/property_set.rb', line 105

def set!(resource, values)
  zip(values) { |property, value| property.set!(resource, value) }
end

#typecast(values) ⇒ Object



120
121
122
# File 'lib/dm-core/property_set.rb', line 120

def typecast(values)
  zip(values.nil? ? [] : values).map { |property, value| property.typecast(value) }
end

#unique_indexesObject



82
83
84
85
86
# File 'lib/dm-core/property_set.rb', line 82

def unique_indexes
  index_hash = {}
  each { |property| parse_index(property.unique_index, property.field, index_hash) }
  index_hash
end

#valid?(values) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/dm-core/property_set.rb', line 115

def valid?(values)
  zip(values.nil? ? [] : values).all? { |property, value| property.valid?(value) }
end

#values_at(*names) ⇒ Object



32
33
34
# File 'lib/dm-core/property_set.rb', line 32

def values_at(*names)
  @properties.values_at(*names)
end