module Commutator::Model::ClassMethods

Private Class Methods

action() click to toggle source
# File lib/commutator/model.rb, line 224
def self.action
  @action
end

Public Instance Methods

build_options_proxy(operation, context = self) click to toggle source
# File lib/commutator/model.rb, line 186
def build_options_proxy(operation, context = self)
  Options::Proxy.new(context, operation)
end
create(attrs) click to toggle source
# File lib/commutator/model.rb, line 151
def create(attrs)
  new(attrs).tap { |dp| dp.put_item_options.execute }
end
get_item(options = build_options_proxy(:get_item)) click to toggle source
# File lib/commutator/model.rb, line 173
def get_item(options = build_options_proxy(:get_item))
  item = client.get_item(options).item
  new(item) unless item.nil?
end
get_item_options() click to toggle source
# File lib/commutator/model.rb, line 161
def get_item_options
  build_options_proxy(:get_item)
end
inherited(subclass) click to toggle source
# File lib/commutator/model.rb, line 138
def inherited(subclass)
  subclass.attribute_names.merge(attribute_names)
  before_hooks.each { |k, v| subclass.before_hooks[k] = v.dup }

  subclass.table_name(table_name)
  subclass.primary_key(hash: primary_key_hash_name,
                       range: primary_key_range_name)
  subclass.scoped_options = options_cache_class

  scopes = const_defined?("Scopes", false) ? const_get("Scopes") : nil
  subclass.const_set("Scopes", Module.new { include scopes }) if scopes
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/commutator/model.rb, line 194
def method_missing(method, *args)
  super unless respond_to?(method)
  query_options.send(method, *args)
end
modify_collection_items_with(*modifiers, factory: false) click to toggle source
# File lib/commutator/model.rb, line 155
def modify_collection_items_with(*modifiers, factory: false)
  self.collection_item_modifiers = [
    ItemModifiers.new(modifiers, factory: factory)
  ].unshift(*collection_item_modifiers)
end
options_class(operation) click to toggle source
# File lib/commutator/model.rb, line 190
def options_class(operation)
  scoped_options[operation]
end
query(options = build_options_proxy(:query)) click to toggle source
# File lib/commutator/model.rb, line 178
def query(options = build_options_proxy(:query))
  collection_for(:query, options)
end
query_options() click to toggle source
# File lib/commutator/model.rb, line 165
def query_options
  build_options_proxy(:query)
end
respond_to?(method, *args) click to toggle source
Calls superclass method
# File lib/commutator/model.rb, line 199
def respond_to?(method, *args)
  super || (const_defined?(:Scopes, false) && const_get(:Scopes).method_defined?(method))
end
scan(options = build_options_proxy(:scan)) click to toggle source
# File lib/commutator/model.rb, line 182
def scan(options = build_options_proxy(:scan))
  collection_for(:scan, options)
end
scan_options() click to toggle source
# File lib/commutator/model.rb, line 169
def scan_options
  build_options_proxy(:scan)
end

Private Instance Methods

collection_for(operation, options) click to toggle source
# File lib/commutator/model.rb, line 234
def collection_for(operation, options)
  Collection.new(
    client.send(operation, options),
    self,
    modifiers: Array(collection_item_modifiers)
  )
end
configure_default_get_item(options) click to toggle source
# File lib/commutator/model.rb, line 250
def configure_default_get_item(options)
  options.table_name(table_name)
end
configure_default_query(options) click to toggle source
# File lib/commutator/model.rb, line 242
def configure_default_query(options)
  options.table_name(table_name)
end
configure_default_scan(options) click to toggle source
# File lib/commutator/model.rb, line 246
def configure_default_scan(options)
  options.table_name(table_name)
end
enhance_options(const_name, scopes = nil) click to toggle source
# File lib/commutator/model.rb, line 215
def enhance_options(const_name, scopes = nil)
  Class.new(Options.const_get(const_name, false)) do
    include ::Commutator::Util::Fluent
    include scopes if scopes && %w[Query Scan].include?(const_name)

    fluent_accessor :_proxy
    delegate :context, to: :_proxy

    @action = const_name
    def self.action
      @action
    end

    def inspect
      "#<#{self.class.action}Options #{to_h}>"
    end
  end
end
inspect() click to toggle source
# File lib/commutator/model.rb, line 228
def inspect
  "#<#{self.class.action}Options #{to_h}>"
end
options_cache_class() click to toggle source
# File lib/commutator/model.rb, line 205
def options_cache_class
  Concurrent::Map.new do |h, k|
    scopes = self.const_defined?("Scopes", false) ? self.const_get("Scopes") : nil
    h.compute_if_absent(k) do
      const_name = k.to_s.camelize
      enhance_options(const_name, scopes)
    end
  end
end