Sha256: 23a5a70c15112ae54b595dc41b1aa8b81bc858c2edcf263e853106a26a06a59e

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

module Scrivito

class WorkspaceDataFromService
  class << self
    # Fetches a workspace data previously store in cache storage.
    # Returns nil if not found.
    def find_from_cache(id)
      if data = CmsCacheStorage.read_workspace_data(id)
        new(data)
      end
    end

    private

    def data_attr_reader(attr_name)
      define_method(attr_name) { @data[attr_name.to_s] }
    end
  end

  data_attr_reader :id
  data_attr_reader :title
  data_attr_reader :revision_id
  data_attr_reader :content_state_id
  data_attr_reader :base_revision_id
  data_attr_reader :base_content_state_id
  data_attr_reader :diff

  def initialize(data)
    @data = data
  end

  def content_state
    @content_state ||= ContentState.find_or_create(content_state_id) if content_state_id
  end

  def base_content_state
    if base_content_state_id
      @base_content_state ||= ContentState.find_or_create(base_content_state_id)
    end
  end

  def changes
    diff && diff['changes']
  end

  def from_content_state_id
    diff && diff['from_content_state_id']
  end

  def to_content_state_id
    diff && diff['to_content_state_id']
  end

  # Serializes and stores a workspace data in cache storage.
  # Also creates an appropriate content state if needed.
  def store_in_cache
    create_content_state if content_state_id
    CmsCacheStorage.write_workspace_data(id, to_hash)
  end

  private

  def to_hash
    {
      'id'                    => id,
      'revision_id'           => revision_id,
      'title'                 => title,
      'content_state_id'      => content_state_id,
      'base_revision_id'      => base_revision_id,
      'base_content_state_id' => base_content_state_id,
    }
  end

  def create_content_state
    ContentState.create(content_state_id: to_content_state_id, changes: changes,
        from_content_state_id: from_content_state_id)
  end
end

end # module Scrivito

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scrivito_sdk-0.16.0 lib/scrivito/workspace_data_from_service.rb
scrivito_sdk-0.15.0 lib/scrivito/workspace_data_from_service.rb
scrivito_sdk-0.14.0 lib/scrivito/workspace_data_from_service.rb
scrivito_sdk-0.13.0 lib/scrivito/workspace_data_from_service.rb
scrivito_sdk-0.12.0 lib/scrivito/workspace_data_from_service.rb