Class: Gecko::BaseDecorator

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/gecko/ext/liquid_compat.rb

Instance Method Summary collapse

Constructor Details

#initialize(delegate) ⇒ BaseDecorator

Returns a new instance of BaseDecorator.



42
43
44
45
46
# File 'lib/gecko/ext/liquid_compat.rb', line 42

def initialize(delegate)
  raise 'Turtles all the way down' if delegate.is_a?(BaseDecorator)

  @delegate = delegate
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



48
49
50
51
52
53
54
# File 'lib/gecko/ext/liquid_compat.rb', line 48

def method_missing(method_name, *args, &block)
  if @delegate.respond_to?(method_name)
    @delegate.public_send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#invokable_methodsObject

Override Liquid::Drop#invokable_methods to add extra checks



70
71
72
73
74
75
76
77
78
79
# File 'lib/gecko/ext/liquid_compat.rb', line 70

def invokable_methods
  @invokable_methods ||= begin
    denylist = Gecko::Record::Base.public_instance_methods + Liquid::Drop.public_instance_methods
    denylist -= i[to_liquid id created_at updated_at]
    allowlist = public_methods + @delegate.public_methods
    available_methods = (allowlist - denylist).map(&:to_s)
    available_methods.reject! { |method_name| method_name.ends_with?("=") }
    Set.new(available_methods)
  end
end

#invoke_drop(method_or_key) ⇒ Object Also known as: []

Override Liquid::Drop#invoke_drop to also check method arity



61
62
63
64
65
# File 'lib/gecko/ext/liquid_compat.rb', line 61

def invoke_drop(method_or_key)
  return unless invokable_methods.include?(method_or_key.to_s) && method_arity(method_or_key.to_sym) <= 0

  public_send(method_or_key)
end

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/gecko/ext/liquid_compat.rb', line 56

def respond_to_missing?(method_name, include_private = false)
  @delegate.respond_to?(method_name) || super
end