Class: RemoteResource::AttributeStorageValue

Inherits:
Object
  • Object
show all
Includes:
Notifications
Defined in:
lib/remote_resource/attribute_storage_value.rb

Instance Method Summary collapse

Methods included from Notifications

#instrument, #instrument_attribute

Constructor Details

#initialize(attribute) ⇒ AttributeStorageValue

Returns a new instance of AttributeStorageValue.



15
16
17
18
# File 'lib/remote_resource/attribute_storage_value.rb', line 15

def initialize(attribute)
  @attribute = attribute
  @storage_entry = nil
end

Instance Method Details

#fetchObject



28
29
30
31
32
33
# File 'lib/remote_resource/attribute_storage_value.rb', line 28

def fetch
  @attribute.with_error_handling action: :fetch do
    attr_client = AttributeHttpClient.new(@attribute)
    write(StorageEntry.from_response(attr_client.get))
  end
end

#storage_entryObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/remote_resource/attribute_storage_value.rb', line 51

def storage_entry
  return @storage_entry if @storage_entry
  instrument_attribute('storage_lookup', @attribute) do
    storages.each do |storage|
      if (storage_entry = storage.read_key(@attribute.key.for_storage))
        return (@storage_entry = storage_entry)
      end
    end
    return (@storage_entry = NullStorageEntry.new)
  end
end

#storagesObject



24
25
26
# File 'lib/remote_resource/attribute_storage_value.rb', line 24

def storages
  RemoteResource.storages
end

#validateObject



35
36
37
38
39
40
41
42
# File 'lib/remote_resource/attribute_storage_value.rb', line 35

def validate
  @attribute.with_error_handling action: :validate do
    attr_client = AttributeHttpClient.new(@attribute)
    response = attr_client.get(headers_for_validation)
    write(StorageEntry.from_response(response))
    response.headers['status'] == '304 Not Modified'
  end
end

#valueObject



20
21
22
# File 'lib/remote_resource/attribute_storage_value.rb', line 20

def value
  storage_entry.data[@attribute.name]
end

#write(storage_entry) ⇒ Object



44
45
46
47
48
49
# File 'lib/remote_resource/attribute_storage_value.rb', line 44

def write(storage_entry)
  @storage_entry = nil
  storages.each do |storage|
    storage.write_key(@attribute.key.for_storage, storage_entry)
  end
end