Class: ActsAsTable::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_table/adapter.rb

Overview

Note:

The "adapter pattern" is a software design pattern that is used to make existing classes work with others without modifying their source code.

ActsAsTable adapter object.

Instance Method Summary collapse

Instance Method Details

#classify_for(value_provider, table_name) ⇒ Object



73
74
75
# File 'lib/acts_as_table/adapter.rb', line 73

def classify_for(value_provider, table_name)
  table_name.classify
end

#find_or_initialize_by_for(record_model, callee, method_name = :find_or_initialize_by, *args) {|base| ... } ⇒ ActiveRecord::Base

Finds the first record with the given attributes, or creates a record with the attributes if one is not found.

Parameters:

  • record_model (ActsAsTable::RecordModel)
  • callee (#find_or_initialize_by)
  • method_name (Symbol) (defaults to: :find_or_initialize_by)
  • args (Array<Object>)

Yield Parameters:

  • base (ActiveRecord::Base)

Yield Returns:

  • (void)

Returns:

  • (ActiveRecord::Base)


19
20
21
# File 'lib/acts_as_table/adapter.rb', line 19

def find_or_initialize_by_for(record_model, callee, method_name = :find_or_initialize_by, *args, &block)
  callee.send(method_name, *args, &block)
end

#get_value_for(value_provider, base = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Get the value for the given record using the given options.

Parameters:

  • value_provider (#get_value)
  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)

Returns:



43
44
45
# File 'lib/acts_as_table/adapter.rb', line 43

def get_value_for(value_provider, base = nil, **options)
  value_provider.get_value(base, **options)
end

#new_for(record_model, callee, method_name = :new, *args) {|base| ... } ⇒ ActiveRecord::Base

Initializes new record from relation while maintaining the current scope.

Parameters:

Yield Parameters:

  • base (ActiveRecord::Base)

Yield Returns:

  • (void)

Returns:

  • (ActiveRecord::Base)


32
33
34
# File 'lib/acts_as_table/adapter.rb', line 32

def new_for(record_model, callee, method_name = :new, *args, &block)
  callee.send(method_name, *args, &block)
end

#set_value_for(value_provider, base = nil, new_value = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Set the new value for the given record using the given options.

Parameters:

  • value_provider (#set_value)
  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • new_value (Object, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :default (Boolean)

Returns:



55
56
57
# File 'lib/acts_as_table/adapter.rb', line 55

def set_value_for(value_provider, base = nil, new_value = nil, **options)
  value_provider.set_value(base, new_value, **options)
end

#tableize_for(value_provider, class_name) ⇒ Object



77
78
79
# File 'lib/acts_as_table/adapter.rb', line 77

def tableize_for(value_provider, class_name)
  class_name.tableize
end

#wrap_value_for(value_provider, base = nil, source_value = nil, target_value = nil, **options) ⇒ ActsAsTable::ValueProvider::WrappedValue

Returns a new ActsAsTable wrapped value object.

Parameters:

  • value_provider (ActsAsTable::ValueProvider::InstanceMethods)
  • base (ActiveRecord::Base, nil) (defaults to: nil)
  • source_value (Object, nil) (defaults to: nil)
  • target_value (Object, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :changed (Boolean)
  • :default (Boolean)

Returns:



69
70
71
# File 'lib/acts_as_table/adapter.rb', line 69

def wrap_value_for(value_provider, base = nil, source_value = nil, target_value = nil, **options)
  ActsAsTable::ValueProvider::WrappedValue.new(value_provider, base, source_value, target_value, **options)
end