Module: DataMapper::Model
- Defined in:
- lib/dm-ar-finders.rb
Instance Method Summary collapse
-
#find(symbol_or_id) ⇒ Resource, ...
Lookup the resource or resources.
-
#find_by_sql(sql_or_query, options = {}) ⇒ DataMapper::Collection
Find resources by providing your own SQL query or DataMapper::Query instance.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object (private) Also known as: method_missing_without_find_by
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dm-ar-finders.rb', line 160 def method_missing_with_find_by(method, *args, &block) if match = matches_dynamic_finder?(method) finder = determine_finder(match) attribute_names = extract_attribute_names_from_match(match) send(finder, Hash[ attribute_names.zip(args) ]) else method_missing_without_find_by(method, *args, &block) end end |
Instance Method Details
#find(id) ⇒ Resource, ... #find(symbol) ⇒ Resource, ...
Lookup the resource or resources
27 28 29 30 31 32 33 34 |
# File 'lib/dm-ar-finders.rb', line 27 def find(symbol_or_id) case symbol_or_id when :first then first when :last then last when :all then all else get(symbol_or_id) end end |
#find_by_sql(string_query, options = {}) ⇒ DataMapper::Collection #find_by_sql(dm_query, options = {}) ⇒ DataMapper::Collection
Find resources by providing your own SQL query or DataMapper::Query instance.
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dm-ar-finders.rb', line 87 def find_by_sql(sql_or_query, = {}) # Figure out what the user passed in. case sql_or_query when Array sql, *bind_values = sql_or_query when String sql, bind_values = sql_or_query, [] when DataMapper::Query sql, bind_values = repository.adapter.send(:select_statement, sql_or_query) else raise ArgumentError, '#find_by_sql requires a query of some kind to work' end # Sort out the options. repository = repository(.fetch(:repository, default_repository_name)) if .key?(:properties) if [:properties].kind_of?(DataMapper::PropertySet) properties = [:properties] else # Normalize properties into PropertySet[Property]. properties = Array([:properties]).map! do |prop| prop.kind_of?(Symbol) ? self.properties[prop] : prop end properties = DataMapper::PropertySet.new(properties) end else properties = self.properties(repository.name) end unless defined?(Adapters::DataObjectsAdapter) && repository.adapter.kind_of?(Adapters::DataObjectsAdapter) raise '#find_by_sql only available for Repositories served by a DataObjectsAdapter' end records = [] repository.adapter.send(:with_connection) do |connection| reader = connection.create_command(sql).execute_reader(*bind_values) fields = properties.field_map.values_at(*reader.fields).compact begin while reader.next! records << Hash[ fields.zip(reader.values) ] end ensure reader.close end end query = Query.new(repository, self, :fields => properties, :reload => .fetch(:reload, false)) Collection.new(query, query.model.load(records, query)) end |