Class: Context::BaseContext

Inherits:
Object
  • Object
show all
Extended by:
Memoizer
Includes:
Memoizer
Defined in:
lib/context/base_context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseContext



71
72
73
74
75
76
77
78
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



97
98
99
100
101
102
103
# 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

Instance Attribute Details

#parent_contextObject

Returns the value of attribute parent_context.



69
70
71
# File 'lib/context/base_context.rb', line 69

def parent_context
  @parent_context
end

Class Method Details

.decorate(ancestor_context_method, decorator:, args: [], memoize: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Boolean



46
47
48
# 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) ⇒ Boolean



41
42
43
44
# 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

.view_helpers(*method_names) ⇒ Object



50
51
52
53
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#context_class_chainObject



80
81
82
83
# 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_mappingObject



91
92
93
94
95
# 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) ⇒ Boolean



85
86
87
88
89
# 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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean



105
106
107
# 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) ⇒ Object



109
110
111
# File 'lib/context/base_context.rb', line 109

def whereis(method_name)
  context_method_mapping[method_name.to_sym]
end