Sha256: 2535b539fa330350cbd1eee6554c051f1edae6189dbc2c746246b96b0a4f637a

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Magicka
  # @api public
  #
  # Base class for element rendering
  class Element < Sinclair::Options
    autoload :ClassMethods,  'magicka/element/class_methods'
    autoload :MethodBuilder, 'magicka/element/method_builder'

    skip_validation

    class << self
      # @api public
      # @!visibility public
      #
      # Add available options
      #
      # @return [Array<Sinclair::MethodDefinition>]
      alias with_attributes with_options

      include ClassMethods
    end

    # @method with_attributes
    # @api public
    # @!visibility public
    #
    # Adds attribute
    #
    # This will affect initialization and add readers
    #
    # @return [Array]

    # Render element HTML
    def render
      renderer.render partial: template, locals: locals
    end

    private

    attr_reader :renderer
    # @api private
    # @private
    # @method renderer
    #
    # Object responsible for rendering the HTML

    # @api private
    # @private
    #
    # @param (see .render)
    def initialize(renderer:, **args)
      @renderer = renderer
      super(**args)
    end

    # @api private
    # @private
    #
    # Returns hash of local variables
    #
    # Local variablees will be available when rendering
    # the template
    #
    # @return [Hash]
    def locals
      self.class.locals.inject({}) do |hash, attribute|
        hash.merge!(attribute => send(attribute))
      end
    end

    # @api public
    # @private
    #
    # Returns template file location
    #
    # @return [String]
    def template
      self.class.name.underscore
          .gsub(%r{^.*/}, "#{template_folder}/")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
magicka-0.5.3 lib/magicka/element.rb
magicka-0.5.2 lib/magicka/element.rb
magicka-0.5.1 lib/magicka/element.rb
magicka-0.5.0 lib/magicka/element.rb