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



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

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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

#<<(property) ⇒ Object



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

def <<(property)
  clear_cache
  super
end

#==(other) ⇒ Object



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

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

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

Make sure that entry is part of this PropertySet

Parameters:

Returns:

  • the entry that is now part of this PropertySet

API:

  • semipublic



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

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

API:

  • semipublic



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

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

#discriminatorObject

API:

  • semipublic



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

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

API:

  • private



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

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

#get(resource) ⇒ Object

API:

  • semipublic



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

def get(resource)
  return [] if resource.nil?

  map { |property| resource.__send__(property.name) }
end

#get!(resource) ⇒ Object

API:

  • semipublic



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.

API:

  • private



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

API:

  • semipublic



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

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

API:

  • semipublic



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

def key
  @key ||= self.class.new(select(&: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.

API:

  • private



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

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

#loaded?(resource) ⇒ Boolean

Returns:

API:

  • semipublic



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.

API:

  • private



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

API:

  • semipublic



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

API:

  • semipublic



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

API:

  • semipublic



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

API:

  • semipublic



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

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

#valid?(values) ⇒ Boolean

Returns:

API:

  • semipublic



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



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

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