Class: DataMapper::Collection
- LazyArray
- DataMapper::Collection
Attributes
Instance Attributes
| query | [RW] | public |
Returns the value of attribute query. |
|---|
Constructor Summary
private
initialize(query, &loader)
[View source]
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
# File 'dm-core/lib/dm-core/collection.rb', line 83 def initialize(query, &loader) raise ArgumentError, "+query+ must be a DataMapper::Query, but was #{query.class}", caller unless query.kind_of?(Query) @query = query @properties_with_indexes = Hash[*query.fields.zip((0...query.fields.length).to_a).flatten] super() load_with(&loader) if inheritance_property = query.model.inheritance_property(repository.name) @inheritance_property_index = @properties_with_indexes[inheritance_property] end if (@key_properties = query.model.key(repository.name)).all? { |key| @properties_with_indexes.include?(key) } @key_property_indexes = @properties_with_indexes.values_at(*@key_properties) end end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
private
method_missing(method_name, *args)
[View source]
120 121 122 123 124 125 126 127 128 129
# File 'dm-core/lib/dm-core/collection.rb', line 120 def method_missing(method_name, *args) if query.model.relationships(repository.name)[method_name] map { |e| e.send(method_name) }.flatten.compact else super end end
Public Visibility
Public Instance Method Summary
| #clear | |
|---|---|
| #delete(resource, &block) | |
| #delete_at(index) | |
| #load(values, reload = false) | |
| #pop | |
| #reload(options = {}) | |
| #repository | |
| #shift | |
| #to_csv | |
| #to_json | |
| #to_xml | |
| #to_yaml(opts = {}) |
Public Instance Method Details
clear
public
clear
[View source]
60 61 62 63 64 65
# File 'dm-core/lib/dm-core/collection.rb', line 60 def clear each { |resource| remove_resource(resource) } super end
delete
public
delete(resource, &block)
[View source]
73 74 75
# File 'dm-core/lib/dm-core/collection.rb', line 73 def delete(resource, &block) remove_resource(super) end
delete_at
public
delete_at(index)
[View source]
77 78 79
# File 'dm-core/lib/dm-core/collection.rb', line 77 def delete_at(index) remove_resource(super) end
load
public
load(values, reload = false)
[View source]
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
# File 'dm-core/lib/dm-core/collection.rb', line 14 def load(values, reload = false) model = if @inheritance_property_index values.at(@inheritance_property_index) || query.model else query.model end resource = nil if @key_property_indexes key_values = values.values_at(*@key_property_indexes) if resource = repository.identity_map_get(model, key_values) resource.collection = self self << resource return resource unless reload else resource = begin model.allocate rescue NoMethodError DataMapper.logger.error("Model not found for row: #{values.inspect} at index #{@inheritance_property_index}") raise $! end self << resource resource.collection = self @key_properties.zip(key_values).each do |property,key_value| resource.instance_variable_set(property.instance_variable_name, key_value) end resource.instance_variable_set(:@new_record, false) repository.identity_map_set(resource) end else resource = model.allocate self << resource resource.collection = self resource.instance_variable_set(:@new_record, false) resource.readonly! end @properties_with_indexes.each_pair do |property, i| resource.instance_variable_set(property.instance_variable_name, values.at(i)) end self end
pop
public
pop
[View source]
65 66 67 68 69
# File 'dm-core/lib/dm-core/collection.rb', line 65 def pop remove_resource(super) end
reload
public
reload(options = {})
[View source]
9 10 11 12
# File 'dm-core/lib/dm-core/collection.rb', line 9 def reload(options = {}) @query = query.merge(keys.merge(:fields => @key_properties).merge(options)) replace(repository.adapter.read_set(repository, query.merge(:reload => true))) end
repository
public
repository
[View source]
5 6 7 8 9
# File 'dm-core/lib/dm-core/collection.rb', line 5 def repository query.repository end
shift
public
shift
[View source]
69 70 71 72 73
# File 'dm-core/lib/dm-core/collection.rb', line 69 def shift remove_resource(super) end
to_csv
public
to_csv
[View source]
155 156 157 158 159 160 161 162 163
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 155 def to_csv result = "" each do |item| result << item.to_csv + "\n" end result end
to_json
public
to_json
[View source]
143 144 145 146 147
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 143 def to_json to_a.to_json end
to_xml
public
to_xml
[View source]
147 148 149 150 151 152 153 154 155
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 147 def to_xml result = "" each do |item| result << item.to_xml + "\n" end result end
to_yaml
public
to_yaml(opts = {})
[View source]
139 140 141
# File 'dm-more/dm-serializer/lib/dm-serializer.rb', line 139 def to_yaml(opts = {}) to_a.to_yaml(opts) end