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



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

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



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

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



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

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



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

#defaultsObject

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

#discriminatorObject



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

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.



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

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

#get(resource) ⇒ Object



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

#get!(resource) ⇒ Object



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

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.



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

#indexesObject



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

#inspectObject



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

def inspect
  to_a.inspect
end

#keyObject



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

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.



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

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

#loaded?(resource) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



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

#set(resource, values) ⇒ Object



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

#set!(resource, values) ⇒ Object



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

#typecast(values) ⇒ Object



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

#unique_indexesObject



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

#valid?(values) ⇒ Boolean

Returns:

  • (Boolean)


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

#|(other) ⇒ Object



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

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