Sha256: 391903b3f8a230b4098289ef5d8e29979ce5d24d6c34351e96edf28930277f23
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
# frozen_string_literal: true module Tempo # This is the base class for all the Tempo resource factory instances. class BaseFactory attr_reader :client def initialize(client) @client = client end # Return the name of the class which this factory generates, i.e. # Tempo::resource::FooFactory creates Tempo::resource::Foo instances. def target_class # Need to do a little bit of work here as Module.const_get doesn't work # with nested class names, i.e. Tempo::resource::Foo. # # So create a method chain from the class components. This code will # unroll to: # Module.const_get('Tempo').const_get('resource').const_get('Foo') # target_class_name = self.class.name.sub(/Factory$/, '') class_components = target_class_name.split('::') class_components.inject(Module) do |mod, const_name| mod.const_get(const_name) end end def self.delegate_to_target_class(*method_names) method_names.each do |method_name| define_method method_name do |*args| target_class.send(method_name, @client, *args) end end end # The principle purpose of this class is to delegate methods to the corresponding # non-factory class and automatically prepend the client argument to the argument # list. delegate_to_target_class :all, :find, :collection_path, :singular_path # This method needs special handling as it has a default argument value def build(attrs = {}) target_class.build(@client, attrs) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tempo-ruby-0.1.0 | lib/tempo/base_factory.rb |