Class: DataMapper::Adapters::BaseAdapter

Inherits:
AbstractAdapter
  • Object
show all
Includes:
Slf4r::Logger
Defined in:
lib/datamapper4rails/adapters/base_adapter.rb

Direct Known Subclasses

RestfulAdapter

Instance Method Summary collapse

Constructor Details

#initialize(name, uri_or_options) ⇒ BaseAdapter

Returns a new instance of BaseAdapter.



25
26
27
# File 'lib/datamapper4rails/adapters/base_adapter.rb', line 25

def initialize(name, uri_or_options)
  super(name, uri_or_options)
end

Instance Method Details

#create(resources) ⇒ Fixnum

Returns number of the newly created resources.

Parameters:

  • resources (Array<DataMapper::Resources>)

    aaaa

Returns:

  • (Fixnum)

    number of the newly created resources

See Also:

  • AbstractAdapter


78
79
80
81
82
83
84
# 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) ⇒ Fixnum

Returns number of the deleted resources.

Parameters:

  • query (Query)

    which selects the resources to be deleted

Returns:

  • (Fixnum)

    number of the deleted resources

See Also:

  • AbstractAdapter


144
145
146
147
148
149
150
# 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) ⇒ DataMapper::Collection

Returns collection of Resources.

Parameters:

  • query (DataMapper::Query)

    which selects the resources

Returns:

  • (DataMapper::Collection)

    collection of Resources

See Also:

  • AbstractAdapter


124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ DataMapper::Resource

Returns the found Resource or nil.

Parameters:

  • query (DataMapper::Query)

    which selects the resource

Returns:

See Also:

  • AbstractAdapter


108
109
110
111
112
113
114
115
116
117
# 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_primitiveObject

See Also:

  • AbstractAdapter


21
22
23
# File 'lib/datamapper4rails/adapters/base_adapter.rb', line 21

def transaction_primitive
  NoopTransaction.new
end

#update(attributes, query) ⇒ Fixnum

Returns number of the updated resources.

Parameters:

  • attributes (Hash)

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

  • on (Query)

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

Returns:

  • (Fixnum)

    number of the updated resources

See Also:

  • AbstractAdapter


95
96
97
98
99
100
101
# 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