module Hanami::Action::Exposable::ClassMethods

Exposures API class methods

@since 0.1.0 @api private

Public Instance Methods

_expose(*names)

Alias of expose to be used in internal modules. #_expose is not watched by the Guard

Alias for: expose
expose(*names) click to toggle source

Expose the given attributes on the outside of the object with a getter and a special method called exposures.

@param names [Array<Symbol>] the name(s) of the attribute(s) to be

exposed

@return [void]

@since 0.1.0

@example

require 'hanami/controller'

class Show
  include Hanami::Action

  expose :article, :tags

  def call(params)
    @article = Article.find params[:id]
    @tags    = Tag.for(article)
  end
end

action = Show.new
action.call({id: 23})

action.article # => #<Article ...>
action.tags    # => [#<Tag ...>, #<Tag ...>]

action.exposures # => { :article => #<Article ...>, :tags => [ ... ] }
# File lib/hanami/action/exposable.rb, line 65
def expose(*names)
  class_eval do
    names.each do |name|
      attr_reader(name) unless attr_reader?(name)
    end

    exposures.push(*names)
  end
end
Also aliased as: _expose
exposures() click to toggle source

Set of exposures attribute names

@return [Array] the exposures attribute names

@since 0.1.0 @api private

# File lib/hanami/action/exposable.rb, line 85
def exposures
  @exposures ||= []
end

Private Instance Methods

attr_reader?(name) click to toggle source

Check if the attr_reader is already defined

@since 0.3.0 @api private

# File lib/hanami/action/exposable.rb, line 94
def attr_reader?(name)
  (instance_methods | private_instance_methods).include?(name)
end