Class: Automerge::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/automerge.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(value = nil, **opts) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/automerge.rb', line 46

def self.from(value = nil, **opts)
  actor_id = opts.delete(:actor_id)
  value = opts if value.nil? && !opts.empty?
  doc = new(actor_id: actor_id)
  unless value.is_a?(Hash)
    raise ArgumentError, "Automerge document root must be a Hash"
  end

  value.each do |key, child|
    doc.put([key], child)
  end
  doc.commit
  doc
end

Instance Method Details

#[](path) ⇒ Object



61
62
63
# File 'lib/automerge.rb', line 61

def [](path)
  get(path)
end

#[]=(path, value) ⇒ Object



65
66
67
# File 'lib/automerge.rb', line 65

def []=(path, value)
  put(path, value)
end

#change(message: nil, timestamp: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/automerge.rb', line 69

def change(message: nil, timestamp: nil)
  if block_given?
    begin
      yield self
      commit(message, timestamp)
    rescue Exception
      rollback
      raise
    end
  else
    commit(message, timestamp)
  end
  self
end

#sync_stateObject



90
91
92
# File 'lib/automerge.rb', line 90

def sync_state
  SyncState.new
end

#to_hObject Also known as: to_hash



84
85
86
# File 'lib/automerge.rb', line 84

def to_h
  get([])
end