Class: DataMapper::PropertySet
- Extended by:
- Deprecate
- 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 Method Summary collapse
- #<<(property) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, property) ⇒ Object
-
#defaults ⇒ Object
TODO: make PropertySet#reject return a PropertySet instance.
- #discriminator ⇒ Object
- #field_map ⇒ Object private
- #get(resource) ⇒ Object
- #get!(resource) ⇒ Object
- #in_context(properties) ⇒ Object private
- #include?(property) ⇒ Boolean
- #index(property) ⇒ Object
- #indexes ⇒ Object
- #key ⇒ Object
- #lazy_context(context) ⇒ Object private
- #loaded?(resource) ⇒ Boolean
- #named?(name) ⇒ Boolean
- #property_contexts(property) ⇒ Object private
- #set(resource, values) ⇒ Object
- #set!(resource, values) ⇒ Object
- #typecast(values) ⇒ Object
- #unique_indexes ⇒ Object
- #valid?(values) ⇒ Boolean
- #values_at(*names) ⇒ Object
Methods included from Deprecate
Methods inherited from Array
Instance Method Details
#<<(property) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dm-core/property_set.rb', line 37 def <<(property) found = named?(property.name) add_property(property) if found superclass_slice(index(property), property) else super end end |
#[](name) ⇒ Object
14 15 16 |
# File 'lib/dm-core/property_set.rb', line 14 def [](name) @properties[name] end |
#[]=(name, property) ⇒ Object
22 23 24 |
# File 'lib/dm-core/property_set.rb', line 22 def []=(name, property) self << property end |
#defaults ⇒ Object
TODO: make PropertySet#reject return a PropertySet instance
60 61 62 |
# File 'lib/dm-core/property_set.rb', line 60 def defaults @defaults ||= self.class.new(key | [ discriminator ].compact | reject { |property| property.lazy? }).freeze end |
#discriminator ⇒ Object
70 71 72 |
# File 'lib/dm-core/property_set.rb', line 70 def discriminator @discriminator ||= detect { |property| property.kind_of?(Property::Discriminator) || property.type == Types::Discriminator } end |
#field_map ⇒ 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.
152 153 154 |
# File 'lib/dm-core/property_set.rb', line 152 def field_map map { |property| [ property.field, property ] }.to_hash end |
#get(resource) ⇒ Object
89 90 91 92 |
# File 'lib/dm-core/property_set.rb', line 89 def get(resource) return [] if resource.nil? map { |property| resource.__send__(property.name) } end |
#get!(resource) ⇒ Object
95 96 97 |
# File 'lib/dm-core/property_set.rb', line 95 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.
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dm-core/property_set.rb', line 139 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 |
#include?(property) ⇒ Boolean
49 50 51 |
# File 'lib/dm-core/property_set.rb', line 49 def include?(property) named?(property.name) end |
#index(property) ⇒ Object
54 55 56 |
# File 'lib/dm-core/property_set.rb', line 54 def index(property) each_index { |index| break index if at(index).name == property.name } end |
#indexes ⇒ Object
75 76 77 78 79 |
# File 'lib/dm-core/property_set.rb', line 75 def indexes index_hash = {} each { |property| parse_index(property.index, property.field, index_hash) } index_hash end |
#key ⇒ Object
65 66 67 |
# File 'lib/dm-core/property_set.rb', line 65 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.
134 135 136 |
# File 'lib/dm-core/property_set.rb', line 134 def lazy_context(context) lazy_contexts[context] ||= [] end |
#loaded?(resource) ⇒ Boolean
110 111 112 |
# File 'lib/dm-core/property_set.rb', line 110 def loaded?(resource) all? { |property| property.loaded?(resource) } end |
#named?(name) ⇒ Boolean
27 28 29 |
# File 'lib/dm-core/property_set.rb', line 27 def named?(name) @properties.key?(name.to_sym) 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.
125 126 127 128 129 130 131 |
# File 'lib/dm-core/property_set.rb', line 125 def property_contexts(property) contexts = [] lazy_contexts.each do |context, properties| contexts << context if properties.include?(property) end contexts end |
#set(resource, values) ⇒ Object
100 101 102 |
# File 'lib/dm-core/property_set.rb', line 100 def set(resource, values) zip(values) { |property, value| resource.__send__("#{property.name}=", value) } end |
#set!(resource, values) ⇒ Object
105 106 107 |
# File 'lib/dm-core/property_set.rb', line 105 def set!(resource, values) zip(values) { |property, value| property.set!(resource, value) } end |
#typecast(values) ⇒ Object
120 121 122 |
# File 'lib/dm-core/property_set.rb', line 120 def typecast(values) zip(values.nil? ? [] : values).map { |property, value| property.typecast(value) } end |
#unique_indexes ⇒ Object
82 83 84 85 86 |
# File 'lib/dm-core/property_set.rb', line 82 def unique_indexes index_hash = {} each { |property| parse_index(property.unique_index, property.field, index_hash) } index_hash end |
#valid?(values) ⇒ Boolean
115 116 117 |
# File 'lib/dm-core/property_set.rb', line 115 def valid?(values) zip(values.nil? ? [] : values).all? { |property, value| property.valid?(value) } end |
#values_at(*names) ⇒ Object
32 33 34 |
# File 'lib/dm-core/property_set.rb', line 32 def values_at(*names) @properties.values_at(*names) end |