Sha256: e563bbce2912a8c7977942135aa29389f07a20b23663ef6528e7b44a2bc10ffb

Contents?: true

Size: 1.42 KB

Versions: 20

Compression:

Stored size: 1.42 KB

Contents

module Rollbar
  class LazyStore
    attr_reader :loaded_data
    private :loaded_data

    attr_reader :raw

    def initialize(initial_data)
      initial_data ||= {}

      @raw = initial_data
      @loaded_data = {}
    end

    def eql?(other)
      if other.is_a?(self.class)
        raw.eql?(other.raw)
      else
        raw.eql?(other)
      end
    end

    def ==(other)
      raw == if other.is_a?(self.class)
               other.raw
             else
               other
             end
    end

    # With this version of clone we ensure that the loaded_data is empty
    def clone
      self.class.new(raw.clone)
    end

    def [](key)
      load_value(key)
    end

    def []=(key, value)
      raw[key] = value

      loaded_data.delete(key)

      value
    end

    def data
      raw.reduce({}) do |acc, (k, _)|
        acc[k] = self[k]

        acc
      end
    end

    private

    def load_value(key)
      return loaded_data[key] if loaded_data.key?(key)
      return unless raw.key?(key)

      value = find_value(key)
      loaded_data[key] = value

      value
    end

    def find_value(key)
      value = raw[key]
      value.respond_to?(:call) ? value.call : value
    end

    def method_missing(method_sym, *args, &block)
      return raw.send(method_sym, *args, &block) if raw.respond_to?(method_sym)

      super
    end

    def respond_to?(method_sym)
      super || raw.respond_to?(method_sym)
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rollbar-3.0.1 lib/rollbar/lazy_store.rb
rollbar-3.0.0 lib/rollbar/lazy_store.rb
rollbar-2.27.1 lib/rollbar/lazy_store.rb
rollbar-2.27.0 lib/rollbar/lazy_store.rb
rollbar-2.26.1 lib/rollbar/lazy_store.rb
rollbar-2.26.0 lib/rollbar/lazy_store.rb
rollbar-2.25.1 lib/rollbar/lazy_store.rb
rollbar-2.25.0 lib/rollbar/lazy_store.rb
rollbar-2.24.0 lib/rollbar/lazy_store.rb
rollbar-2.23.2 lib/rollbar/lazy_store.rb
rollbar-2.23.1 lib/rollbar/lazy_store.rb
rollbar-2.23.0 lib/rollbar/lazy_store.rb
rollbar-2.22.1 lib/rollbar/lazy_store.rb
rollbar-2.22.0 lib/rollbar/lazy_store.rb
rollbar-2.21.0 lib/rollbar/lazy_store.rb
rollbar-2.20.2 lib/rollbar/lazy_store.rb
rollbar-2.20.1 lib/rollbar/lazy_store.rb
rollbar-2.20.0 lib/rollbar/lazy_store.rb
rollbar-2.19.4 lib/rollbar/lazy_store.rb
rollbar-2.19.3 lib/rollbar/lazy_store.rb