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

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


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


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"]


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'}]


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


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