Class: Pressy::LocalChangeset

Inherits:
Object
  • Object
show all
Defined in:
lib/pressy/changeset/local.rb

Overview

A LocalChangeset holds set of changes to apply to a local Store.

Defined Under Namespace

Classes: AddedPost, DeletedPost, UpdatedPost

Building the Changeset collapse

Querying the Changes collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalChangeset

Creates a new, empty changeset.



4
5
6
7
# File 'lib/pressy/changeset/local.rb', line 4

def initialize
  @server_posts = {}
  @local_posts = {}
end

Instance Method Details

#add_local_post(id, post) ⇒ self

Adds a rendered post from the local store to the changeset.

If the id is nil, the post is a local-only draft and will be ignored.

If no server post is added with the same id, this post will be present in #deleted_posts.

If a server post is added with the same id, the post may be present in #updated_posts if it was changed on the server.

Returns:

  • (self)

    the updated changeset



37
38
39
40
41
42
43
# File 'lib/pressy/changeset/local.rb', line 37

def add_local_post(id, post)
  return self unless id

  @local_posts[id] = post
  @changes = nil
  self
end

#add_server_post(id, post) ⇒ self

Adds a rendered post from the server to the changeset.

If no local post is added with the same id, the post will be present in #added_posts.

If a local post is added with the same id, the post may be present in #updated_posts if it was changed on the server.

Returns:

  • (self)

    the updated changeset



20
21
22
23
24
# File 'lib/pressy/changeset/local.rb', line 20

def add_server_post(id, post)
  @server_posts[id] = post
  @changes = nil
  self
end

#changesArray<AddedPost, UpdatedPost, DeletedPost>

Returns a list of changes to apply to the store

Returns:



56
57
58
# File 'lib/pressy/changeset/local.rb', line 56

def changes
  @changes ||= build_changes
end

#has_changes?Boolean

Checks if there are differences present in the changeset.

Returns:

  • (Boolean)

    true if there are any differences between the local and server posts



50
51
52
# File 'lib/pressy/changeset/local.rb', line 50

def has_changes?
  !changes.empty?
end