module ActsAsTable

ActsAsTable is a Ruby on Rails plugin for working with tabular data.

Constants

VERSION

@return [String]

Public Class Methods

config() click to toggle source

Returns the ActsAsTable configuration object.

@return [ActsAsTable::Configuration]

# File lib/acts_as_table.rb, line 228
def self.config
  Configuration.instance
end
configure(&block) click to toggle source

Configure ActsAsTable.

@yieldreturn [void] @return [void]

@example Set the ActsAsTable adapter object.

class CustomActsAsTableAdapter < ActsAsTable::Adapter
  # ...
end

ActsAsTable.configure do
  config.adapter = CustomActsAsTableAdapter.new
end

@example Register an ActsAsTable serialization format module.

require 'acts_as_table_custom_format' # underscore

ActsAsTable.configure do
  config.formats << :CustomFormat # constantize
end

@example Prefix the table names for the ActsAsTable model classes.

ActsAsTable.configure do
  config.methods.select { |method_name| method_name.to_s.ends_with?("_table") }.each do |method_name|
    config.send(:"#{method_name}=", :"prefix_#{config.send(method_name)}")
  end
end
# File lib/acts_as_table.rb, line 260
def self.configure(&block)
  if block_given?
    self.instance_eval(&block)
  end

  return
end
for(format) click to toggle source

Finds an ActsAsTable serialization format module based on a symbolic name.

@param [Symbol] format @return [Module] @raise [ArgumentError] If the given symbolic name is invalid.

@example Find the ActsAsTable serialization format module for CSV format.

ActsAsTable.for(:csv) #=> ActsAsTable::CSV

@example Implement an ActsAsTable serialization format module.

require 'active_support/core_ext/module'

module ActsAsTable
  # ActsAsTable serialization format module for "custom format."
  module CustomFormat
    extend ::ActiveSupport::Autoload

    autoload :Reader, 'acts_as_table/custom_format/reader'
    autoload :Writer, 'acts_as_table/custom_format/writer'

    # Returns the symbolic name for this ActsAsTable serialization format module.
    #
    # @return [Symbol]
    def format
      :custom_format
    end

    # Returns a new ActsAsTable reader object for this serialization format module.
    #
    # @param [Array<Object>] args
    # @yieldparam [ActsAsTable::CustomFormat::Reader] reader
    # @yieldreturn [void]
    # @return [ActsAsTable::CustomFormat::Reader]
    def reader(*args, &block)
      Reader.new(*args, &block)
    end

    # Returns a new ActsAsTable writer object for this serialization format module.
    #
    # @param [Array<Object>] args
    # @yieldparam [ActsAsTable::CustomFormat::Writer] writer
    # @yieldreturn [void]
    # @return [ActsAsTable::CustomFormat::Writer]
    def writer(*args, &block)
      Writer.new(*args, &block)
    end
  end
end
# File lib/acts_as_table.rb, line 79
def self.for(format)
  # @return [Hash<Symbol, Module>]
  module_by_format = self.config.formats.collect { |const_name|
    self.const_get(const_name, false)
  }.inject({}) { |acc, m|
    acc[m.format] ||= m
    acc
  }

  unless module_by_format.key?(format)
    raise ::ArgumentError.new("invalid format - expected: #{module_by_format.keys.inspect}, found: #{format.inspect}")
  end

  module_by_format[format]
end
method_missing(method_name, *args, &block) click to toggle source

Delegates to ActsAsTable configuration object.

@param [String] method_name @param [Array<Object>] args @yield [*args, &block] @yieldreturn [Object] @return [Object] @raise [NoMethodError]

Calls superclass method
# File lib/acts_as_table.rb, line 276
def self.method_missing(method_name, *args, &block)
  self.config.respond_to?(method_name, false) ? self.config.send(method_name, *args, &block) : super(method_name, *args, &block)
end
respond_to?(method_name, include_all = false) click to toggle source

Delegates to ActsAsTable configuration object.

@param [String] method_name @param [Boolean] include_all @return [Boolean]

Calls superclass method
# File lib/acts_as_table.rb, line 285
def self.respond_to?(method_name, include_all = false)
  self.config.respond_to?(method_name, false) || super(method_name, include_all)
end
use(new_adapter, &block) click to toggle source

Uses the given ActsAsTable adapter object within the scope of the execution of the given block.

If block given, yield with no arguments and return the result. Otherwise, return `nil`.

@param [ActsAsTable::Adapter] new_adapter @yieldreturn [Object] @return [Object, nil]

# File lib/acts_as_table.rb, line 102
def self.use(new_adapter, &block)
  # @return [Object, nil]
  result = nil

  if block_given?
    # @return [ActsAsTable::Adapter]
    orig_adapter = self.config.adapter

    begin
      self.config.adapter = new_adapter

      result = block.call
    ensure
      self.config.adapter = orig_adapter
    end
  end

  result
end