class Context::BaseContext

Attributes

parent_context[RW]

Public Class Methods

decorate(ancestor_context_method, decorator:, args: [], memoize: false) click to toggle source
# File lib/context/base_context.rb, line 24
def decorate(ancestor_context_method, decorator:, args: [], memoize: false)
  define_method(ancestor_context_method) do
    decorator.new(
      @parent_context.public_send(ancestor_context_method),
      *(args.map { |arg| instance_eval(arg.to_s) })
    )
  end

  @decorated_methods ||= []
  @decorated_methods << ancestor_context_method.to_sym

  if memoize
    public_send(:memoize, ancestor_context_method)
    @decorated_methods << "_unmemoized_#{ancestor_context_method}".to_sym
  end
end
has_view_helper?(method_name) click to toggle source
# File lib/context/base_context.rb, line 46
def has_view_helper?(method_name)
  @view_helpers.is_a?(Array) && @view_helpers.include?(method_name.to_sym)
end
is_decorated?(method_name) click to toggle source
# File lib/context/base_context.rb, line 41
def is_decorated?(method_name)
  @decorated_methods.is_a?(Array) &&
    @decorated_methods.include?(method_name.to_sym)
end
new(attributes = {}) click to toggle source
# File lib/context/base_context.rb, line 71
def initialize(attributes = {})
  attributes.each do |k, v|
    if respond_to?(:"#{k}=")
    then public_send(:"#{k}=", v)
    else raise ArgumentError, "unknown attribute: #{k}"
    end
  end
end
view_helpers(*method_names) click to toggle source
# File lib/context/base_context.rb, line 50
def view_helpers(*method_names)
  @view_helpers ||= []
  @view_helpers += method_names.map(&:to_sym)
end
wrap(parent_context, **args) click to toggle source
# File lib/context/base_context.rb, line 55
def wrap(parent_context, **args)
  existing_public_methods = parent_context.context_method_mapping.keys
  new_public_methods = public_instance_methods(false)
  redefined_methods = existing_public_methods & new_public_methods
  redefined_methods.reject! { |method| is_decorated?(method) }

  unless redefined_methods.empty?
    raise Context::MethodOverrideError.new(self, redefined_methods)
  end

  new(parent_context: parent_context, **args)
end

Public Instance Methods

context_class_chain() click to toggle source
# File lib/context/base_context.rb, line 80
def context_class_chain
  @context_class_chain ||=
    ((@parent_context.try(:context_class_chain) || []) + [self.class.name])
end
context_method_mapping() click to toggle source
# File lib/context/base_context.rb, line 91
def context_method_mapping
  @context_method_mapping ||=
    (@parent_context.try(:context_method_mapping) || {})
      .merge(get_context_method_mapping)
end
has_view_helper?(method_name) click to toggle source
# File lib/context/base_context.rb, line 85
def has_view_helper?(method_name)
  self.class.has_view_helper?(method_name) ||
    (@parent_context.present? &&
      @parent_context.has_view_helper?(method_name))
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/context/base_context.rb, line 97
def method_missing(method_name, *args, &block)
  if @parent_context
    @parent_context.public_send(method_name, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/context/base_context.rb, line 105
def respond_to_missing?(method_name, include_private = false)
  @parent_context&.respond_to?(method_name, include_private)
end
whereis(method_name) click to toggle source
# File lib/context/base_context.rb, line 109
def whereis(method_name)
  context_method_mapping[method_name.to_sym]
end

Private Instance Methods

get_context_method_mapping() click to toggle source
# File lib/context/base_context.rb, line 115
def get_context_method_mapping
  self.class.public_instance_methods(false).reduce({}) do |hash, method_name|
    hash.merge!(method_name => self.class.name)
  end
end