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.



39
40
41
42
43
# File 'lib/gecko/ext/liquid_compat.rb', line 39

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



45
46
47
48
49
50
51
# File 'lib/gecko/ext/liquid_compat.rb', line 45

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



67
68
69
70
71
72
73
74
75
76
# File 'lib/gecko/ext/liquid_compat.rb', line 67

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



58
59
60
61
62
# File 'lib/gecko/ext/liquid_compat.rb', line 58

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)


53
54
55
# File 'lib/gecko/ext/liquid_compat.rb', line 53

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