Sha256: 330129fd269bc9136cbfa8a990f40f213f781839cefb4bf6648cb0074a7e5056

Contents?: true

Size: 1.68 KB

Versions: 25

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Azeroth
  # @api private
  # @author Darthjee
  #
  # Builder responsible for for building the resource methods
  #
  # The builder adds 2 methods, one for listing all
  # entries of a resource, and one for fetching an specific
  # entry
  class ResourceBuilder
    # @param model [Model] Resource model interface
    # @param builder [Sinclair] method builder
    def initialize(model, builder)
      @model = model
      @builder = builder
    end

    # Append methods to be built to the builder
    #
    # Methods are the listing of all entities and fetching
    # of an specific entity
    #
    # @return [Array<Sinclair::MethodDefinition>]
    def append
      add_method(plural, "@#{plural} ||= #{model.klass}.all")
      add_method(name, "@#{name} ||= #{plural}.find(#{name}_id)")
    end

    private

    attr_reader :model, :builder
    # @method model
    # @api private
    # @private
    #
    # Returns the model class of the resource
    #
    # @return [Model]

    # @method builder
    # @api private
    # @private
    #
    # Returns a method builder
    #
    # @return [Sinclair]
    #
    # @see https://www.rubydoc.info/gems/sinclair Sinclair

    delegate :add_method, to: :builder
    # @api private
    # @method add_method
    #
    # Add a method to be build on the controller
    #
    # @return [Array<Sinclair::MethodDefinition>]

    delegate :name, :plural, to: :model
    # @method name
    # @api private
    #
    # Returns the name of the resource represented by the model
    #
    # @return [String]

    # @method plural
    # @api private
    #
    # Return the pluralized version of resource name
    #
    # @return [String]
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
azeroth-0.10.1 lib/azeroth/resource_builder.rb
azeroth-0.10.0 lib/azeroth/resource_builder.rb
azeroth-0.9.0 lib/azeroth/resource_builder.rb
azeroth-0.8.2 lib/azeroth/resource_builder.rb
azeroth-0.8.1 lib/azeroth/resource_builder.rb
azeroth-0.8.0 lib/azeroth/resource_builder.rb
azeroth-0.7.4 lib/azeroth/resource_builder.rb
azeroth-0.7.3 lib/azeroth/resource_builder.rb
azeroth-0.7.2 lib/azeroth/resource_builder.rb
azeroth-0.7.1 lib/azeroth/resource_builder.rb
azeroth-0.7.0 lib/azeroth/resource_builder.rb
azeroth-0.6.5 lib/azeroth/resource_builder.rb
azeroth-0.6.4 lib/azeroth/resource_builder.rb
azeroth-0.6.3 lib/azeroth/resource_builder.rb
azeroth-0.6.2 lib/azeroth/resource_builder.rb
azeroth-0.6.1 lib/azeroth/resource_builder.rb
azeroth-0.6.0 lib/azeroth/resource_builder.rb
azeroth-0.5.0 lib/azeroth/resource_builder.rb
azeroth-0.4.0 lib/azeroth/resource_builder.rb
azeroth-0.3.0 lib/azeroth/resource_builder.rb