Module: Streamingly::SerDe
- Defined in:
- lib/streamingly/serde.rb
Class Method Summary collapse
- .from_csv(string) ⇒ Object
- .from_tabbed_csv(string) ⇒ Object
- .resolve_class(class_name) ⇒ Object
- .to_csv(record) ⇒ Object
Class Method Details
.from_csv(string) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/streamingly/serde.rb', line 27 def self.from_csv(string) tokens = CSV.parse_line(string) klass = resolve_class(tokens.first) klass.new(*tokens[1..-1]) rescue NameError tokens end |
.from_tabbed_csv(string) ⇒ Object
35 36 37 38 |
# File 'lib/streamingly/serde.rb', line 35 def self.from_tabbed_csv(string) k,v = string.split("\t") KV.new(from_csv(k), from_csv(v)) end |
.resolve_class(class_name) ⇒ Object
40 41 42 |
# File 'lib/streamingly/serde.rb', line 40 def self.resolve_class(class_name) class_name.split('::').reduce(Kernel) { |parent, element| parent.const_get(element) } end |
.to_csv(record) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/streamingly/serde.rb', line 7 def self.to_csv(record) case record when String record when Streamingly::KV record.to_s when Struct tokens = *record.map { |token| case token when BigDecimal token.to_s('F') else token end } CSV.generate_line( [ record.class.name, *tokens ]).rstrip end end |