Class: DataMapper::Adapters::RiakAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/dm-riak-adapter/adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ RiakAdapter

Initializes a new RiakAdapter instance

Parameters:

  • name (String, Symbol)

    Repository name

  • options (Hash)

    Configuration options

Options Hash (options):

  • :host (String) — default: '127.0.0.1'

    Server hostname

  • :port (Integer) — default: 8098

    Server port

  • :prefix (String) — default: 'riak'

    Path prefix to the HTTP endpoint

  • :namespace (String) — default: ''

    Bucket namespace



15
16
17
18
19
20
21
22
23
24
# File 'lib/dm-riak-adapter/adapter.rb', line 15

def initialize(name, options)
  super
  
  @riak = Riak::Client.new(
    :host   => options[:host],
    :port   => options[:port],
    :prefix => options[:prefix]
  )
  @namespace = options[:namespace] ? options[:namespace] + ':' : ''
end

Instance Method Details

#create(resources) ⇒ Integer

Persists one or many new resources

Examples:

adapter.create(collection)  # => 1

Parameters:

  • resources (Enumerable<Resource>)

    List of resources (model instances) to create

Returns:

  • (Integer)

    Number of objects created



36
37
38
# File 'lib/dm-riak-adapter/adapter.rb', line 36

def create(resources)
  create_objects resources
end

#delete(collection) ⇒ Integer

Deletes one or many existing resources

Examples:

adapter.delete(collection)  # => 1

Parameters:

  • collection (Collection)

    Collection of records to be deleted

Returns:

  • (Integer)

    Number of records deleted



88
89
90
# File 'lib/dm-riak-adapter/adapter.rb', line 88

def delete(collection)
  delete_objects collection
end

#flush(model) ⇒ Array<String>

Flushes the bucket for the specified model

Examples:

adapter.flush(Post)  # => ["6moGsRVfutpG4wzibgwDBKc37Dd"]

Parameters:

  • model (Class)

    Model to flush

Returns:

  • (Array<String>)

    Keys of the flushed objects



102
103
104
# File 'lib/dm-riak-adapter/adapter.rb', line 102

def flush(model)
  bucket(model).keys.each {|key| bucket(model)[key].delete}
end

#read(query) ⇒ Enumerable<Hash>

Reads one or many resources from a datastore

Examples:

adapter.read(query)  # => [{'title' => 'Lorem Ipsum'}]

Parameters:

  • query (Query)

    Query to match objects in the datastore

Returns:

  • (Enumerable<Hash>)

    Array of hashes to become resources



50
51
52
53
54
55
56
# File 'lib/dm-riak-adapter/adapter.rb', line 50

def read(query)
  query.filter_records(objects_for(query.model)).each do |object|
    query.fields.each do |property|
      object[property.name.to_s] = property.typecast(object[property.name.to_s])
    end
  end
end

#update(attributes, collection) ⇒ Integer

Updates one or many existing resources

Examples:

adapter.update(attributes, collection)  # => 1

Parameters:

  • attributes (Hash(Property => Object))

    Hash of attribute values to set, keyed by Property

  • collection (Collection)

    Collection of records to be updated

Returns:

  • (Integer)

    Number of records updated



71
72
73
74
75
76
# File 'lib/dm-riak-adapter/adapter.rb', line 71

def update(attributes, collection)
  attributes = attributes_as_fields(attributes)
  objects_for(collection.query.model).each {|r| r.update(attributes)}
  
  update_objects collection
end