module Polyseerio::Resource::Factory

Resource building methods.

Public Class Methods

_make() click to toggle source

Create a resource.

# File lib/resource/factory.rb, line 84
def self._make
  proc do |type, request, cid, options = {}|
    unless Definition::DEFINITION.key? type
      raise ArgumentError, 'Could not find definition for resource: ' \
        "#{type}"
    end

    definition = Definition::DEFINITION.fetch(type)

    # Create the resource / class.
    resource = if defines_singleton? definition
                 {}
               else
                 create(type, request, cid, options)
               end

    # Add statics.
    if definition.key? Definition::STATICS
      statics = SDK::Static.factory(
        request,
        type,
        definition[Definition::STATICS],
        options
      )

      add_statics(resource, statics)
    end

    # Add methods.
    if definition.key? Definition::METHODS
      methods = SDK::Method.factory(
        request,
        type,
        definition[Definition::METHODS],
        options
      )

      add_methods(resource, request, methods)
    end

    resource
  end
end
add_method(*args) click to toggle source

Add an instance method to a Class.

# File lib/resource/factory.rb, line 27
def self.add_method(*args)
  proc do |(name, method), resource|
    resource.class_eval do
      define_method(name, &Helper.forward_self(method))
    end
  end.curry.call(*args)
end
add_methods(resource, _request, methods = {}) click to toggle source

Add a collection of instance methods to a Class.

# File lib/resource/factory.rb, line 43
def self.add_methods(resource, _request, methods = {})
  return resource if methods.empty?

  methods.each_with_object(resource, &add_method)
end
add_static(*args) click to toggle source

Add a static method to a Class.

# File lib/resource/factory.rb, line 18
def self.add_static(*args)
  proc do |(name, method), resource|
    resource.class_eval do
      define_singleton_method(name, &method)
    end
  end.curry.call(*args)
end
add_statics(resource, statics = {}) click to toggle source

Add a collection of static methods to a Class.

# File lib/resource/factory.rb, line 36
def self.add_statics(resource, statics = {})
  return resource if statics.empty?

  statics.each_with_object(resource, &add_static)
end
create(type, request, cid, copts = {}) click to toggle source

Create a resource. TODO: copts are not optional

# File lib/resource/factory.rb, line 62
def self.create(type, request, cid, copts = {})
  resource = Object.const_set(to_class_name(type, cid), Class.new(Base))
  resource.define_singleton_method(:copts) do
    copts
  end

  resource.define_singleton_method(:type) do
    type
  end

  resource.define_singleton_method(:request) do
    request
  end

  resource.define_singleton_method(:cid) do
    cid
  end

  resource
end
defines_singleton?(definition) click to toggle source

Determine if a resource definition represents a singleton.

# File lib/resource/factory.rb, line 13
def self.defines_singleton?(definition)
  !definition.key?(:methods) || definition[:methods].empty?
end
get_memoize_key(resource, _request, cid, _options = {}) click to toggle source

Generate a memoize key based on factory arguments

# File lib/resource/factory.rb, line 129
def self.get_memoize_key(resource, _request, cid, _options = {})
  :"#{resource}_#{cid}"
end
make(*args) click to toggle source

Memoized calls to make. A little ugly, would like a better memoize API.

# File lib/resource/factory.rb, line 144
def self.make(*args)
  @make.call(*args)
end
memoize_key() click to toggle source

Convenience for get_memoize_key in proc form.

# File lib/resource/factory.rb, line 134
def self.memoize_key
  proc do |*args|
    get_memoize_key(*args)
  end
end
to_class_name(resource, cid = '') click to toggle source

Takes a resource name and creates a class name.

# File lib/resource/factory.rb, line 50
def self.to_class_name(resource, cid = '')
  parts = resource.to_s.split('-').map(&:capitalize)

  parts[parts.size - 1] = Inflection.singular(parts.last)

  name = parts.join ''

  "#{name}#{cid}"
end