Sha256: 2975eeed182ba703a2db774474c0db59612417f386311d2ca04b43c433dc7fe4

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

require "forwardable"
require "virtus"

module AdaptivePayments
  # Provides an interface to create shortcuts to child nodes without traversing an object graph
  module Aliases
    include Forwardable

    # Invoke alias_name as if original_name were invoked on attr
    #
    # @param [Symbol] attr
    #   the name of the attribute containing the child node
    # @param [Symbol] alias_name
    #   the name of the alias method to define
    # @param [Symbol] original_name
    #   the name of the original method in attr
    def alias_param(attr, alias_name, original_name)
      def_delegator attr, original_name,        alias_name
      def_delegator attr, :"#{original_name}=", :"#{alias_name}="
      def_delegator attr, :"#{original_name}?", :"#{alias_name}?" # FIXME: Only alias this for Booleans
    end

    # Shortcut for definining multiple aliases in a single call
    #
    # @param [Symbol] attr
    #   the name of the attribute containing the child node
    # @param [Hash] aliases
    #   a Hash mapping { :alias_name => :original_name }
    def alias_params(attr, aliases)
      aliases.each { |k, v| alias_param(attr, k, v) }
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
creative-pp-adaptive-1.1.1 lib/pp-adaptive/support/aliases.rb
pp-adaptive-1.0.0 lib/pp-adaptive/support/aliases.rb
pp-adaptive-0.0.6 lib/pp-adaptive/support/aliases.rb
pp-adaptive-0.0.5 lib/pp-adaptive/support/aliases.rb
pp-adaptive-0.0.4 lib/pp-adaptive/support/aliases.rb
pp-adaptive-0.0.3 lib/pp-adaptive/support/aliases.rb
pp-adaptive-0.0.2 lib/pp-adaptive/support/aliases.rb