Class: Quant::Ticks::Serializers::Tick Abstract
- Inherits:
-
Object
- Object
- Quant::Ticks::Serializers::Tick
- Defined in:
- lib/quant/ticks/serializers/tick.rb
Overview
The Tick class serializes and deserializes Tick objects to and from various formats. These classes are wired into the Tick classes and are used to convert Tick objects to and from Ruby hashes, JSON strings, and CSV strings. They’re not typically used directly, but are extracted to make it easier to provide custom serialization and deserialization for Tick objects not shipped with the library.
Class Method Summary collapse
-
.from(hash, tick_class:) ⇒ Quant::Ticks::Tick
Returns a
Quant::Ticks::Tick
from a RubyHash
. -
.from_json(json, tick_class:) ⇒ Quant::Ticks::Tick
Returns a
Quant::Ticks::Tick
from a valid JSONString
. -
.to_csv(tick, headers: false) ⇒ String
Returns a
String
that is a valid CSV row. -
.to_h(instance) ⇒ Hash
Returns a Ruby
Hash
comprised of the key properties for the tick. -
.to_json(tick) ⇒ String
Returns a
String
that is a valid JSON representation of the tick’s key properties.
Class Method Details
.from(hash, tick_class:) ⇒ Quant::Ticks::Tick
Returns a Quant::Ticks::Tick
from a Ruby Hash
.
59 60 61 |
# File 'lib/quant/ticks/serializers/tick.rb', line 59 def self.from(hash, tick_class:) raise NotImplementedError end |
.from_json(json, tick_class:) ⇒ Quant::Ticks::Tick
Returns a Quant::Ticks::Tick
from a valid JSON String
.
70 71 72 73 |
# File 'lib/quant/ticks/serializers/tick.rb', line 70 def self.from_json(json, tick_class:) hash = Oj.load(json) from(hash, tick_class:) end |
.to_csv(tick, headers: false) ⇒ String
Returns a String
that is a valid CSV row
23 24 25 26 27 28 29 30 |
# File 'lib/quant/ticks/serializers/tick.rb', line 23 def self.to_csv(tick, headers: false) hash = to_h(tick) row = CSV::Row.new(hash.keys, hash.values) return row.to_csv unless headers header_row = CSV::Row.new(hash.keys, hash.keys) header_row.to_csv << row.to_csv end |
.to_h(instance) ⇒ Hash
Returns a Ruby Hash
comprised of the key properties for the tick.
48 49 50 |
# File 'lib/quant/ticks/serializers/tick.rb', line 48 def self.to_h(instance) raise NotImplementedError end |
.to_json(tick) ⇒ String
Returns a String
that is a valid JSON representation of the tick’s key properties.
38 39 40 |
# File 'lib/quant/ticks/serializers/tick.rb', line 38 def self.to_json(tick) Oj.dump to_h(tick) end |