Sha256: 5c6a858db70af709a0c9abdaeff0e78f6496a97ac84c7d5402163d9712d7619a

Contents?: true

Size: 932 Bytes

Versions: 7

Compression:

Stored size: 932 Bytes

Contents

# frozen_string_literal: true

require_dependency 'scribo/application_drop'
require 'csv'

module Scribo
  class DataDrop < ApplicationDrop
    attr_accessor :data_path

    def initialize(object, data_path = [])
      @object = object
      @data_path = data_path
    end

    def [](name)
      method_missing(name)
    end

    def method_missing(method)
      content = @object.contents.data((data_path + [method.to_s]).join('/')).first

      return Scribo::DataDrop.new(@object, data_path + [content.path]) if content&.kind == 'folder'

      case content&.content_type
      when 'text/x-yaml'
        Scribo::Utility.yaml_safe_parse(content.data)
      when 'application/json'
        ::JSON.parse(content.data)
      when 'text/csv'
        CSV.parse(content.data, headers: true, liberal_parsing: true, quote_char: '"', col_sep: ';',
                                row_sep: "\r\n").map(&:to_h)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
scribo-1.0.44 app/drops/scribo/data_drop.rb
scribo-1.0.43 app/drops/scribo/data_drop.rb
scribo-1.0.42 app/drops/scribo/data_drop.rb
scribo-1.0.41 app/drops/scribo/data_drop.rb
scribo-1.0.40 app/drops/scribo/data_drop.rb
scribo-1.0.39 app/drops/scribo/data_drop.rb
scribo-1.0.38 app/drops/scribo/data_drop.rb