Module: Handle::Command::Persistence

Defined in:
lib/handle/command/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



4
5
6
# File 'lib/handle/command/persistence.rb', line 4

def connection
  @connection
end

#handleObject

Returns the value of attribute handle.



4
5
6
# File 'lib/handle/command/persistence.rb', line 4

def handle
  @handle
end

Instance Method Details

#destroyObject



50
51
52
# File 'lib/handle/command/persistence.rb', line 50

def destroy
  connection.delete_handle(self.handle)
end

#reloadObject



19
20
21
# File 'lib/handle/command/persistence.rb', line 19

def reload
  self.initialize_with(connection.resolve_handle(self.handle).fields)
end

#save(new_handle = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/handle/command/persistence.rb', line 23

def save(new_handle=nil)
  save_handle = new_handle || self.handle
  if save_handle.nil?
    raise Handle::HandleError.new("No handle provided.")
  end

  if save_handle == self.handle
    begin
      original = connection.resolve_handle(save_handle)
      actions = original | self
      actions.each_value { |v| v.connection = connection }
      [:delete,:update,:add].each do |action|
        unless actions[action].empty?
          connection.send("#{action}_handle_values".to_sym, save_handle, actions[action])
        end
      end
    rescue Handle::NotFound
      connection.create_handle(save_handle, self)
      @handle = save_handle
    end
  else
    connection.create_handle(save_handle, self)
    @handle = save_handle
  end
  self
end

#to_batchObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/handle/command/persistence.rb', line 6

def to_batch
  self.collect do |field|
    perm_params = field.perms.to_s
    data_type = case field.class.value_type
    when 'HS_ADMIN'  then 'ADMIN'
    when 'HS_SITE'   then 'FILE'
    when 'HS_PUBKEY' then 'FILE'
    else 'UTF8'
    end
    "#{field.index} #{field.class.value_type} #{field.ttl} #{perm_params} #{data_type} #{field.value_str}"
  end
end