Class: Pyper::Pipes::Content::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/pyper/pipes/content/store.rb

Overview

A pipe for storing content to an object store. Uses the StorageStrategy gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_field, &storage_strategy_builder) ⇒ Store

Returns a new instance of Store.

Parameters:

  • storage_field (Symbol)

    The attributes field in which the content is located.

  • storage_strategy_builder (Block)

    A block that takes an item and returns a StorageStrategy.



9
10
11
12
# File 'lib/pyper/pipes/content/store.rb', line 9

def initialize(storage_field, &storage_strategy_builder)
  @storage_field = storage_field
  @storage_strategy_builder = storage_strategy_builder
end

Instance Attribute Details

#storage_fieldObject (readonly)

Returns the value of attribute storage_field.



5
6
7
# File 'lib/pyper/pipes/content/store.rb', line 5

def storage_field
  @storage_field
end

#storage_strategy_builderObject (readonly)

Returns the value of attribute storage_strategy_builder.



5
6
7
# File 'lib/pyper/pipes/content/store.rb', line 5

def storage_strategy_builder
  @storage_strategy_builder
end

Instance Method Details

#pipe(attributes, status = {}) ⇒ Hash

Stores content using the specified storage strategy

Parameters:

  • attributes (Hash)

    The attributes of the item for which content is to be stored

  • status (Hash) (defaults to: {})

    The mutable status field

Returns:

  • (Hash)

    The item attributes, with the storage_field deleted.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pyper/pipes/content/store.rb', line 18

def pipe(attributes, status = {})
  strategy = storage_strategy_builder.call(attributes)

  content = attributes.delete(storage_field)

  raise ArgumentError.new("#{storage_field} must be present in ContentStorage") unless content

  case content
  when NilClass then # do nothing -- there's no content to write
  when String then strategy.write(content)
  else strategy.write_from(content)
  end

  attributes.merge!(strategy.)

  attributes
end