Module: ActsAsTable

Extended by:
ActiveSupport::Autoload
Defined in:
lib/acts_as_table.rb,
lib/acts_as_table/path.rb,
lib/acts_as_table/engine.rb,
lib/acts_as_table/mapper.rb,
lib/acts_as_table/reader.rb,
lib/acts_as_table/writer.rb,
lib/acts_as_table/adapter.rb,
lib/acts_as_table/headers.rb,
lib/acts_as_table/version.rb,
app/models/acts_as_table/lense.rb,
app/models/acts_as_table/table.rb,
app/models/acts_as_table/value.rb,
app/models/acts_as_table/record.rb,
app/models/acts_as_table/has_many.rb,
app/models/acts_as_table/row_model.rb,
app/models/acts_as_table/belongs_to.rb,
app/models/acts_as_table/foreign_key.rb,
app/models/acts_as_table/primary_key.rb,
app/models/acts_as_table/column_model.rb,
app/models/acts_as_table/record_error.rb,
app/models/acts_as_table/record_model.rb,
app/models/acts_as_table/foreign_key_map.rb,
app/models/acts_as_table/has_many_target.rb,
app/models/concerns/acts_as_table/value_provider.rb,
app/models/concerns/acts_as_table/record_model_class_methods.rb,
app/models/concerns/acts_as_table/value_provider_association_methods.rb

Overview

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

Defined Under Namespace

Modules: Headers, Mapper, RecordModelClassMethods, ValueProvider, ValueProviderAssociationMethods Classes: Adapter, BelongsTo, ColumnModel, Configuration, Engine, ForeignKey, ForeignKeyMap, HasMany, HasManyTarget, HeadersNotFound, InvalidHeaders, Lense, Path, PrimaryKey, Reader, ReaderError, Record, RecordError, RecordModel, RowModel, Table, Value, Writer

Constant Summary collapse

VERSION =

Returns:

  • (String)
'0.0.4'

Class Method Summary collapse

Class Method Details

.configActsAsTable::Configuration

Returns the ActsAsTable configuration object.



228
229
230
# File 'lib/acts_as_table.rb', line 228

def self.config
  Configuration.instance
end

.configure(&block) ⇒ void

This method returns an undefined value.

Configure ActsAsTable.

Examples:

Set the ActsAsTable adapter object.

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

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

Register an ActsAsTable serialization format module.

require 'acts_as_table_custom_format' # underscore

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

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

Yield Returns:

  • (void)


260
261
262
263
264
265
266
# File 'lib/acts_as_table.rb', line 260

def self.configure(&block)
  if block_given?
    self.instance_eval(&block)
  end

  return
end

.for(format) ⇒ Module

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

Examples:

Find the ActsAsTable serialization format module for CSV format.

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

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

Parameters:

  • format (Symbol)

Returns:

  • (Module)

Raises:

  • (ArgumentError)

    If the given symbolic name is invalid.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) {|*args, &block| ... } ⇒ Object

Delegates to ActsAsTable configuration object.

Parameters:

  • method_name (String)
  • args (Array<Object>)

Yields:

  • (*args, &block)

Yield Returns:

  • (Object)

Returns:

  • (Object)

Raises:

  • (NoMethodError)


276
277
278
# 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) ⇒ Boolean

Delegates to ActsAsTable configuration object.

Parameters:

  • method_name (String)
  • include_all (Boolean) (defaults to: false)

Returns:

  • (Boolean)


285
286
287
# 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) ⇒ Object?

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.

Parameters:

Yield Returns:

  • (Object)

Returns:

  • (Object, nil)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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