Sha256: fc4fcf9167e0d6d3479f820307d0c2292332c4d71a349a745b186c284057f675

Contents?: true

Size: 924 Bytes

Versions: 4

Compression:

Stored size: 924 Bytes

Contents

# -*- coding: utf-8 -*-
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)
    raise "No comma format for class #{self.class} defined for style #{style}" unless self.comma_formats && self.comma_formats[style]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
comma-4.1.0 lib/comma/object.rb
comma-4.0.1 lib/comma/object.rb
comma-4.0.0 lib/comma/object.rb
comma-3.2.4 lib/comma/object.rb