Class: DataMapper::PropertySet

Inherits:
SubjectSet show all
Includes:
Enumerable
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 Attribute Summary

Attributes inherited from SubjectSet

#entries

Instance Method Summary collapse

Methods inherited from SubjectSet

#[], #clear, #delete, #each, #empty?, #include?, #initialize, #initialize_copy, #named?, #size, #to_ary, #values_at

Constructor Details

This class inherits a constructor from DataMapper::SubjectSet

Instance Method Details

#&(other) ⇒ Object



36
37
38
# File 'lib/dm-core/property_set.rb', line 36

def &(other)
  self.class.new(to_a & other.to_a)
end

#+(other) ⇒ Object



44
45
46
# File 'lib/dm-core/property_set.rb', line 44

def +(other)
  self.class.new(to_a + other.to_a)
end

#-(other) ⇒ Object



40
41
42
# File 'lib/dm-core/property_set.rb', line 40

def -(other)
  self.class.new(to_a - other.to_a)
end

#<<(property) ⇒ Object



11
12
13
14
# File 'lib/dm-core/property_set.rb', line 11

def <<(property)
  clear_cache
  super
end

#==(other) ⇒ Object



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

def ==(other)
  to_a == other.to_a
end

#[]=(name, entry) ⇒ #name

Make sure that entry is part of this PropertySet

Parameters:

  • name (#to_s)
  • entry (#name)

Returns:

  • (#name)

    the entry that is now part of this PropertySet



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

def []=(name, entry)
  warn "#{self.class}#[]= is deprecated. Use #{self.class}#<< instead: #{caller.first}"
  raise "#{entry.class} is not added with the correct name" unless name && name.to_s == entry.name.to_s
  self << entry
  entry
end

#defaultsObject

TODO: make PropertySet#reject return a PropertySet instance



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

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

#discriminatorObject



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

def discriminator
  @discriminator ||= detect { |property| property.kind_of?(Property::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.



146
147
148
# File 'lib/dm-core/property_set.rb', line 146

def field_map
  DataMapper::Ext::Array.to_hash(map { |property| [ property.field, property ] })
end

#get(resource) ⇒ Object



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

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

#get!(resource) ⇒ Object



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

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.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dm-core/property_set.rb', line 133

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

#indexesObject



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

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

#inspectObject



150
151
152
# File 'lib/dm-core/property_set.rb', line 150

def inspect
  to_a.inspect
end

#keyObject



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

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.



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

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

#loaded?(resource) ⇒ Boolean

Returns:

  • (Boolean)


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

def loaded?(resource)
  all? { |property| property.loaded?(resource) }
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.



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

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

#set(resource, values) ⇒ Object



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

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

#set!(resource, values) ⇒ Object



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

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

#typecast(values) ⇒ Object



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

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

#unique_indexesObject



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

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

#valid?(values) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#|(other) ⇒ Object



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

def |(other)
  self.class.new(to_a | other.to_a)
end