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

- (Object) &(other)



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

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

- (Object) +(other)



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

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

- (Object) -(other)



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

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

- (Object) <<(property)



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

def <<(property)
  clear_cache
  super
end

- (Object) ==(other)



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

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

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

Make sure that entry is part of this PropertySet



23
24
25
26
27
28
# 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

- (Object) defaults

TODO: make PropertySet#reject return a PropertySet instance



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

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

- (Object) discriminator



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

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

- (Object) field_map

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.



144
145
146
# File 'lib/dm-core/property_set.rb', line 144

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

- (Object) get(resource)



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

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

- (Object) get!(resource)



87
88
89
# File 'lib/dm-core/property_set.rb', line 87

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

- (Object) in_context(properties)

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.



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

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

- (Object) indexes



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

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

- (Object) inspect



148
149
150
# File 'lib/dm-core/property_set.rb', line 148

def inspect
  to_a.inspect
end

- (Object) key



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

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

- (Object) lazy_context(context)

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.



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

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

- (Boolean) loaded?(resource)



102
103
104
# File 'lib/dm-core/property_set.rb', line 102

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

- (Object) property_contexts(property)

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.



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

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

- (Object) set(resource, values)



92
93
94
# File 'lib/dm-core/property_set.rb', line 92

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

- (Object) set!(resource, values)



97
98
99
# File 'lib/dm-core/property_set.rb', line 97

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

- (Object) typecast(values)



112
113
114
# File 'lib/dm-core/property_set.rb', line 112

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

- (Object) unique_indexes



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

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

- (Boolean) valid?(values)



107
108
109
# File 'lib/dm-core/property_set.rb', line 107

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

- (Object) |(other)



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

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