Class: Airspace::Reader
- Inherits:
-
Object
- Object
- Airspace::Reader
- Extended by:
- InfoKeys, Forwardable
- Includes:
- HasMetadata
- Defined in:
- lib/airspace/reader.rb
Overview
This is the main class that knows how to fetch and interpret the dataset. It is optimized for chunking/paging and allows you to only pull back specific pages (if desired.)
Constant Summary
Constants included from InfoKeys
InfoKeys::DATA_KEY, InfoKeys::METADATA_KEY, InfoKeys::SEPARATOR_CHAR
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Class Method Summary collapse
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(client, data:, key:, metadata:, serializer:) ⇒ Reader
constructor
A new instance of Reader.
- #page(number) ⇒ Object
- #pages ⇒ Object
Constructor Details
#initialize(client, data:, key:, metadata:, serializer:) ⇒ Reader
Returns a new instance of Reader.
58 59 60 61 62 63 64 65 66 |
# File 'lib/airspace/reader.rb', line 58 def initialize(client, data:, key:, metadata:, serializer:) @client = client @key = key @data = data @metadata = @serializer = serializer freeze end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
50 51 52 |
# File 'lib/airspace/reader.rb', line 50 def client @client end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
50 51 52 |
# File 'lib/airspace/reader.rb', line 50 def data @data end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
50 51 52 |
# File 'lib/airspace/reader.rb', line 50 def key @key end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
50 51 52 |
# File 'lib/airspace/reader.rb', line 50 def @metadata end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
50 51 52 |
# File 'lib/airspace/reader.rb', line 50 def serializer @serializer end |
Class Method Details
.find_by_id(client, id, options: {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/airspace/reader.rb', line 20 def find_by_id(client, id, options: {}) key = ::Airspace::Key.new(id, prefix: [:prefix]) serializer = [:serializer] || ::Airspace::Serializer.new hash = fetch_and_transform(client, key, serializer) return nil unless hash = hash[METADATA_KEY].map { |k, v| [k.to_sym, v] }.to_h new( client, data: hash[DATA_KEY], key: key, metadata: ::Airspace::Metadata.new(), serializer: serializer ) end |
Instance Method Details
#delete ⇒ Object
86 87 88 |
# File 'lib/airspace/reader.rb', line 86 def delete store.delete(key, page_count) end |
#page(number) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/airspace/reader.rb', line 76 def page(number) return [] unless within_bounds?(number) page_index = number - 1 location = chunker.locate(page_index) chunk = store.chunk(key, location.chunk_index) chunk[location.page_index].map { |r| serializer.deserialize_row(r) } end |
#pages ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/airspace/reader.rb', line 68 def pages return [] unless page_count.positive? store.chunks(key, chunk_count).map do |chunk| chunk.map { |r| serializer.deserialize_row(r) } end end |