|
| 1 | +module Elasticsearch |
| 2 | + module Model |
| 3 | + |
| 4 | + # This module provides a proxy interfacing between the including class and |
| 5 | + # Elasticsearch::Model, preventing polluting the including class namespace. |
| 6 | + # |
| 7 | + # The only "gateway" between the model and Elasticsearch::Model is the |
| 8 | + # `__elasticsearch__` class and instance method. |
| 9 | + # |
| 10 | + # The including class must be compatible with |
| 11 | + # [ActiveModel](https://github.com/rails/rails/tree/master/activemodel). |
| 12 | + # |
| 13 | + # @example Include the {Elasticsearch::Model} module into an `Article` model |
| 14 | + # |
| 15 | + # class Article < ActiveRecord::Base |
| 16 | + # include Elasticsearch::Model |
| 17 | + # end |
| 18 | + # |
| 19 | + # Article.__elasticsearch__.respond_to?(:search) |
| 20 | + # # => true |
| 21 | + # |
| 22 | + # article = Article.first |
| 23 | + # |
| 24 | + # article.respond_to? :as_indexed_json |
| 25 | + # # => false |
| 26 | + # |
| 27 | + # article.__elasticsearch__.respond_to?(:as_indexed_json) |
| 28 | + # # => true |
| 29 | + # |
| 30 | + module Proxy |
| 31 | + |
| 32 | + # Define the `__elasticsearch__` class and instance methods |
| 33 | + # in including class. |
| 34 | + # |
| 35 | + def self.included(base) |
| 36 | + base.class_eval do |
| 37 | + # {ClassMethodsProxy} instance, accessed as `MyModel.__elasticsearch__` |
| 38 | + # |
| 39 | + def self.__elasticsearch__ &block |
| 40 | + @__elasticsearch__ ||= ClassMethodsProxy.new(self) |
| 41 | + @__elasticsearch__.instance_eval(&block) if block_given? |
| 42 | + @__elasticsearch__ |
| 43 | + end |
| 44 | + |
| 45 | + # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__` |
| 46 | + # |
| 47 | + def __elasticsearch__ &block |
| 48 | + @__elasticsearch__ ||= InstanceMethodsProxy.new(self) |
| 49 | + @__elasticsearch__.instance_eval(&block) if block_given? |
| 50 | + @__elasticsearch__ |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # A proxy interfacing between Elasticsearch::Model class methods and model class methods |
| 56 | + # |
| 57 | + # TODO: Inherit from BasicObject and make Pry's `ls` command behave |
| 58 | + # |
| 59 | + class ClassMethodsProxy |
| 60 | + attr_reader :klass |
| 61 | + |
| 62 | + def initialize(klass) |
| 63 | + @klass = klass |
| 64 | + end |
| 65 | + |
| 66 | + # Delegate methods to `@klass` |
| 67 | + # |
| 68 | + def method_missing(method_name, *arguments, &block) |
| 69 | + klass.respond_to?(method_name) ? klass.__send__(method_name, *arguments, &block) : super |
| 70 | + end |
| 71 | + |
| 72 | + # Respond to methods from `@klass` |
| 73 | + # |
| 74 | + def respond_to?(method_name, include_private = false) |
| 75 | + klass.respond_to?(method_name) || super |
| 76 | + end |
| 77 | + |
| 78 | + def inspect |
| 79 | + "[PROXY] #{klass.inspect}" |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + # A proxy interfacing between Elasticsearch::Model instance methods and model instance methods |
| 84 | + # |
| 85 | + # TODO: Inherit from BasicObject and make Pry's `ls` command behave |
| 86 | + # |
| 87 | + class InstanceMethodsProxy |
| 88 | + attr_reader :instance |
| 89 | + |
| 90 | + def initialize(instance) |
| 91 | + @instance = instance |
| 92 | + end |
| 93 | + |
| 94 | + # Return the class of the target (instance class object) |
| 95 | + # |
| 96 | + def klass |
| 97 | + instance.class |
| 98 | + end |
| 99 | + |
| 100 | + # Return the `ClassMethodsProxy` instance (instance class' `__elasticsearch__` object) |
| 101 | + # |
| 102 | + def class |
| 103 | + klass.__elasticsearch__ |
| 104 | + end |
| 105 | + |
| 106 | + def inspect |
| 107 | + "[PROXY] #{instance.inspect}" |
| 108 | + end |
| 109 | + |
| 110 | + def as_json(options={}) |
| 111 | + instance.as_json(options) |
| 112 | + end |
| 113 | + |
| 114 | + # Delegate methods to `@instance` |
| 115 | + # |
| 116 | + def method_missing(method_name, *arguments, &block) |
| 117 | + instance.respond_to?(method_name) ? instance.__send__(method_name, *arguments, &block) : super |
| 118 | + end |
| 119 | + |
| 120 | + # Respond to methods from `@instance` |
| 121 | + # |
| 122 | + def respond_to?(method_name, include_private = false) |
| 123 | + instance.respond_to?(method_name) || super |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments