Sha256: ba8281625cf79e0f5c3508b8d9e3e9839d152b95def2211e87538df190dc9159

Contents?: true

Size: 939 Bytes

Versions: 8

Compression:

Stored size: 939 Bytes

Contents

# frozen_string_literal: true

require 'comma/data_extractor'
require 'comma/header_extractor'

class Object
  class_attribute :comma_formats

  class << self
    def comma(style = :default, &block)
      (self.comma_formats ||= {})[style] = block
    end

    def inherited(subclass)
      super
      subclass.comma_formats = self.comma_formats ? self.comma_formats.dup : {}
    end
  end

  def to_comma(style = :default)
    extract_with(Comma::DataExtractor, style)
  end

  def to_comma_headers(style = :default)
    extract_with(Comma::HeaderExtractor, style)
  end

  private

  def extract_with(extractor_class, style = :default)
    raise_unless_style_exists(style)
    extractor_class.new(self, style, self.comma_formats).results
  end

  def raise_unless_style_exists(style)
    return if self.comma_formats && self.comma_formats[style]

    raise "No comma format for class #{self.class} defined for style #{style}"
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
comma-4.8.0 lib/comma/object.rb
comma-4.7.0 lib/comma/object.rb
comma-4.6.0 lib/comma/object.rb
comma-4.5.0 lib/comma/object.rb
comma-4.4.0 lib/comma/object.rb
comma-4.3.2 lib/comma/object.rb
comma-4.3.1 lib/comma/object.rb
comma-4.3.0 lib/comma/object.rb