Class: DataMapper::PropertySet
- Object
- DataMapper::PropertySet
Included Modules
Enumerable
Constructor Summary
private
initialize(properties = [])
[View source]
127 128 129 130 131 132
# File 'dm-core/lib/dm-core/property_set.rb', line 127 def initialize(properties = []) raise ArgumentError, "+properties+ should be an Array, but was #{properties.class}", caller unless Array === properties @entries = properties @property_for = hash_for_property_for end
Public Visibility
Public Instance Method Summary
| #[](name) | |
|---|---|
| #add(*properties) | |
| #defaults | |
| #dup(target = nil) | |
| #each | |
| #empty? | |
| #get(resource) | |
| #indexes | |
| #inheritance_property | |
| #inspect | |
| #key | |
| #lazy_context(name) | |
| #lazy_load_context(names) | |
| #length | |
| #property_contexts(name) | |
| #set(resource, values) | |
| #slice(*names) | |
| #to_query(bind_values) | |
| #unique_indexes |
Public Instance Methods Inherited from Object
Public Instance Method Details
[]
public
[](name)
[View source]
5 6 7
# File 'dm-core/lib/dm-core/property_set.rb', line 5 def [](name) @property_for[name] end
add
public
add(*properties)
[View source]
15 16 17 18 19
# File 'dm-core/lib/dm-core/property_set.rb', line 15 def add(*properties) @entries.push(*properties) properties.each { |property| property.hash } self end
defaults
public
defaults
[View source]
36 37 38 39 40
# File 'dm-core/lib/dm-core/property_set.rb', line 36 def defaults reject { |property| property.lazy? } end
dup
public
dup(target = nil)
[View source]
115 116 117 118 119 120 121 122 123
# File 'dm-core/lib/dm-core/property_set.rb', line 115 def dup(target = nil) return super() unless target properties = map do |property| Property.new(target || property.model, property.name, property.type, property.options.dup) end self.class.new(properties) end
each
public
each
[View source]
31 32 33 34 35 36
# File 'dm-core/lib/dm-core/property_set.rb', line 31 def each @entries.each { |property| yield property } self end
empty?
public
empty?
[View source]
27 28 29 30 31
# File 'dm-core/lib/dm-core/property_set.rb', line 27 def empty? @entries.empty? end
get
public
get(resource)
[View source]
60 61 62
# File 'dm-core/lib/dm-core/property_set.rb', line 60 def get(resource) map { |property| property.get(resource) } end
indexes
public
indexes
[View source]
44 45 46 47 48 49 50
# File 'dm-core/lib/dm-core/property_set.rb', line 44 def indexes index_hash = {} each { |property| parse_index(property.index, property.field.to_s, index_hash) } index_hash end
inheritance_property
public
inheritance_property
[View source]
56 57 58 59 60
# File 'dm-core/lib/dm-core/property_set.rb', line 56 def inheritance_property detect { |property| property.type == DataMapper::Types::Discriminator } end
inspect
public
inspect
[View source]
111 112 113 114 115
# File 'dm-core/lib/dm-core/property_set.rb', line 111 def inspect '#<PropertySet:{' + map { |property| property.inspect }.join(',') + '}>' end
key
public
key
[View source]
40 41 42 43 44
# File 'dm-core/lib/dm-core/property_set.rb', line 40 def key select { |property| property.key? } end
lazy_context
public
lazy_context(name)
[View source]
83 84 85
# File 'dm-core/lib/dm-core/property_set.rb', line 83 def lazy_context(name) lazy_contexts[name] end
lazy_load_context
public
lazy_load_context(names)
[View source]
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
# File 'dm-core/lib/dm-core/property_set.rb', line 87 def lazy_load_context(names) if Array === names raise ArgumentError, "+names+ cannot be an empty Array", caller if names.empty? elsif !(Symbol === names) raise ArgumentError, "+names+ must be a Symbol or an Array of Symbols, but was a #{names.class}", caller end result = [] Array(names).each do |name| contexts = property_contexts(name) if contexts.empty? result << name # not lazy else result |= lazy_contexts.values_at(*contexts).flatten.uniq end end result end
length
public
length
[View source]
23 24 25 26 27
# File 'dm-core/lib/dm-core/property_set.rb', line 23 def length @entries.length end
property_contexts
public
property_contexts(name)
[View source]
75 76 77 78 79 80 81
# File 'dm-core/lib/dm-core/property_set.rb', line 75 def property_contexts(name) contexts = [] lazy_contexts.each do |context,property_names| contexts << context if property_names.include?(name) end contexts end
set
public
set(resource, values)
[View source]
64 65 66 67 68 69 70 71 72 73 74 75 76
# File 'dm-core/lib/dm-core/property_set.rb', line 64 def set(resource, values) raise ArgumentError, "+resource+ should be a DataMapper::Resource, but was #{resource.class}" unless Resource === resource if Array === values raise ArgumentError, "+values+ must have a length of #{length}, but has #{values.length}", caller if values.length != length elsif !values.nil? raise ArgumentError, "+values+ must be nil or an Array, but was a #{values.class}", caller end each_with_index { |property,i| property.set(resource, values.nil? ? nil : values[i]) } end
slice
public
slice(*names)
[View source]
11 12 13
# File 'dm-core/lib/dm-core/property_set.rb', line 11 def slice(*names) @property_for.values_at(*names) end
to_query
public
to_query(bind_values)
[View source]
107 108 109
# File 'dm-core/lib/dm-core/property_set.rb', line 107 def to_query(bind_values) Hash[ *zip(bind_values).flatten ] end
unique_indexes
public
unique_indexes
[View source]
50 51 52 53 54 55 56
# File 'dm-core/lib/dm-core/property_set.rb', line 50 def unique_indexes index_hash = {} each { |property| parse_index(property.unique_index, property.field.to_s, index_hash) } index_hash end