Class: CopycopterClient::Cache

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

Overview

Manages synchronization of copy between I18nBackend and Client. Acts like a Hash. Applications using the client will not need to interact with this class directly.

Responsible for locking down access to data used by both threads.

Instance Method Summary collapse

Constructor Details

#initialize(client, options) ⇒ Cache

Usually instantiated when CopycopterClient::Configuration#apply is invoked.

Parameters:

  • client (Client)

    the client used to fetch and upload data

  • options (Hash)

Options Hash (options):

  • :logger (Logger)

    where errors should be logged



15
16
17
18
19
20
21
22
23
# File 'lib/copycopter_client/cache.rb', line 15

def initialize(client, options)
  @blurbs = {}
  @client = client
  @downloaded = false
  @logger = options[:logger]
  @mutex = Mutex.new
  @queued = {}
  @started = false
end

Instance Method Details

#[](key) ⇒ String

Returns content for the given blurb.

Parameters:

  • key (String)

    the key of the desired blurb

Returns:

  • (String)

    the contents of the blurb



28
29
30
# File 'lib/copycopter_client/cache.rb', line 28

def [](key)
  lock { @blurbs[key] }
end

#[]=(key, value) ⇒ Object

Sets content for the given blurb. The content will be pushed to the server on the next flush.

Parameters:

  • key (String)

    the key of the blurb to update

  • value (String)

    the new contents of the blurb



36
37
38
# File 'lib/copycopter_client/cache.rb', line 36

def []=(key, value)
  lock { @queued[key] = value }
end

#downloadObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/copycopter_client/cache.rb', line 98

def download
  @started = true

  client.download do |downloaded_blurbs|
    downloaded_blurbs.reject! { |key, value| value == '' }
    lock { @blurbs = downloaded_blurbs }
  end
rescue ConnectionError => error
  logger.error error.message
ensure
  @downloaded = true
end

#exportString

Yaml representation of all blurbs

Returns:

  • (String)

    yaml



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/copycopter_client/cache.rb', line 48

def export
  keys = {}
  lock do
    @blurbs.sort.each do |(blurb_key, value)|
      current = keys
      yaml_keys = blurb_key.split('.')

      0.upto(yaml_keys.size - 2) do |i|
        key = yaml_keys[i]

        # Overwrite en.key with en.sub.key
        unless current[key].class == Hash
          current[key] = {}
        end

        current = current[key]
      end

      current[yaml_keys.last] = value
    end
  end

  unless keys.size < 1
    keys.to_yaml
  end
end

#flushObject



90
91
92
93
94
95
96
# File 'lib/copycopter_client/cache.rb', line 90

def flush
  with_queued_changes do |queued|
    client.upload queued
  end
rescue ConnectionError => error
  logger.error error.message
end

#keysArray<String>

Keys for all blurbs stored on the server.

Returns:

  • (Array<String>)

    keys



42
43
44
# File 'lib/copycopter_client/cache.rb', line 42

def keys
  lock { @blurbs.keys }
end

#syncObject

Downloads and then flushes



112
113
114
115
# File 'lib/copycopter_client/cache.rb', line 112

def sync
  download
  flush
end

#wait_for_downloadObject

Waits until the first download has finished.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/copycopter_client/cache.rb', line 76

def wait_for_download
  if pending?
    logger.info 'Waiting for first download'

    if logger.respond_to? :flush
      logger.flush
    end

    while pending?
      sleep 0.1
    end
  end
end