Class: Kvom::Storage::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kvom/storage/base.rb

Direct Known Subclasses

FileSystemStorage, S3Storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
# File 'lib/kvom/storage/base.rb', line 13

def initialize(config = nil)
  @config =
      if config
        config.symbolize_keys
      else
        {}
      end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/kvom/storage/base.rb', line 11

def config
  @config
end

Instance Method Details

#get(spec) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kvom/storage/base.rb', line 30

def get(spec)
  raise "Invalid key spec (not a hash): #{spec.inspect}" unless Hash === spec
  id =
      case
      when spec.key?("id")
        spec["id"]
      when spec.key?(:id)
        spec[:id]
      else
        raise "Invalid key spec (no id): #{spec.inspect}"
      end
  read(id)
end

#put(data) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/kvom/storage/base.rb', line 22

def put(data)
  id = generate_id
  json_string = MultiJson.dump(data)
  normalized = MultiJson.load(json_string)
  write(id, normalized, json_string)
  {"id" => id}
end