class DataMapper::Adapters::BaseAdapter

Public Class Methods

new(name, uri_or_options) click to toggle source
Calls superclass method
# File lib/datamapper4rails/adapters/base_adapter.rb, line 25
def initialize(name, uri_or_options)
  super(name, uri_or_options)
end

Public Instance Methods

create(resources) click to toggle source

@see AbstractAdapter @param [Array<DataMapper::Resources>] resources

aaaa

@return [Fixnum]

number of the newly created resources
# File lib/datamapper4rails/adapters/base_adapter.rb, line 78
def create(resources)
  resources.select do |resource|

    create_resource(resource)

  end.size # just return the number of create resources
end
delete(query) click to toggle source

@see AbstractAdapter @param [Query] query

which selects the resources to be deleted

@return [Fixnum]

number of the deleted resources
# File lib/datamapper4rails/adapters/base_adapter.rb, line 144
def delete(query)
  read_many(query).each do |resource|

    delete_resource(resource)

  end.size
end
read_many(query) click to toggle source

@see AbstractAdapter @param [DataMapper::Query] query

which selects the resources

@return [DataMapper::Collection]

collection of Resources
# File lib/datamapper4rails/adapters/base_adapter.rb, line 124
      def read_many(query)
        Collection.new(query) do |set|
          result = read_resources(query)
#puts "read_many"
#p result
          if result.size > 0 and result.first.is_a? Resource
            set.replace(result)
          else
            result.each do |values|
              set.load(values)
            end
          end
        end
      end
read_one(query) click to toggle source

@see AbstractAdapter @param [DataMapper::Query] query

which selects the resource

@return [DataMapper::Resource]

the found Resource or nil
# File lib/datamapper4rails/adapters/base_adapter.rb, line 108
def read_one(query)
  result = read_resource(query)
  if result.is_a? Resource
    result
  elsif result # assume result to be Array with the values
    #puts "------------------"
    #p result
    query.model.load(result, query)
  end
end
transaction_primitive() click to toggle source

@see AbstractAdapter

# File lib/datamapper4rails/adapters/base_adapter.rb, line 21
def transaction_primitive
  NoopTransaction.new
end
update(attributes, query) click to toggle source

@see AbstractAdapter @param [Hash] attributes

collection of attribute, i.e. the name/value pairs which 
needs to be updated

@param [Query]

on all resources which are selected by that query the 
update will be applied

@return [Fixnum]

number of the updated resources
# File lib/datamapper4rails/adapters/base_adapter.rb, line 95
def update(attributes, query)
  read_many(query).select do |resource|
    
    update_resource(resource, attributes)

  end.size
end

Protected Instance Methods

equality_comparison(bind_value, value) click to toggle source

helper method to dispatch the equality test for different classes

# File lib/datamapper4rails/adapters/base_adapter.rb, line 63
def equality_comparison(bind_value, value)
  case bind_value
    when Array, Range then bind_value.include?(value)
    when NilClass     then value.nil?
    else                   bind_value == value
  end
end
filter_resource(resource, conditions) click to toggle source

checks whether a given resource fullfils the conditions @param [DataMapper::Resource] resource @param [Array<Condition>] conditions @return [Boolean]

true if the resource are within the conditions otherwise false
# File lib/datamapper4rails/adapters/base_adapter.rb, line 36
def filter_resource(resource, conditions)
  #puts "condi"
  #p conditions
  # no conditation => no filter
  if conditions.size == 0
    true
  else
    conditions.all? do |tuple|
      operator, property, bind_value = *tuple
      
      value = property.get!(resource)
      case operator
      when :eql, :in then equality_comparison(bind_value, value)
      when :not      then !equality_comparison(bind_value, value)
      when :like     then Regexp.new(bind_value.gsub(/%/, ".*")) =~ value
      when :gt       then !value.nil? && value >  bind_value
      when :gte      then !value.nil? && value >= bind_value
      when :lt       then !value.nil? && value <  bind_value
      when :lte      then !value.nil? && value <= bind_value
      else raise "Invalid query operator: #{operator.inspect}"
      end
    end
  end
end

Private Instance Methods

create_resource(resource) click to toggle source

@param [DataMapper::Resource] resource

which will be created

@return [DataMapper::Resource]

either the resource itself if the creation was successful or nil
# File lib/datamapper4rails/adapters/base_adapter.rb, line 158
def create_resource(resource)
  raise NotImplementedError.new
end
delete_resource(resource) click to toggle source

@param [DataMapper::Resource] resource

which will be deleted

@return [DataMapper::Resource]

either the resource if the deletion was successful or nil
# File lib/datamapper4rails/adapters/base_adapter.rb, line 193
def delete_resource(resource)
  raise NotImplementedError.new
end
read_resource(query) click to toggle source

@param [DataMapper::Query] query

which selects the resource

@return [DataMapper::Resource,Array<String>]

the resource or a set of values ordered in the same manner as query.fields attributes
# File lib/datamapper4rails/adapters/base_adapter.rb, line 166
def read_resource(query)
  raise NotImplementedError.new
end
read_resources(query) click to toggle source

@param [DataMapper::Query] query

which selects the resources

@return [Array<DataMapper::Resource>,Array<String>]

resources or ordered values

@see read_resource

# File lib/datamapper4rails/adapters/base_adapter.rb, line 175
def read_resources(query)
  raise NotImplementedError.new
end
update_resource(resource, attributes) click to toggle source

@param [DataMapper::Resource] resource

which will be updated with the given attributes.

@param [Hash] attributes

the keys are the property names and the values are the new values of that property.

@return [DataMapper::Resource]

resource on success otherwise nil
# File lib/datamapper4rails/adapters/base_adapter.rb, line 185
def update_resource(resource, attributes)
  raise NotImplementedError.new
end