1
0
Эх сурвалжийг харах

Introduce a new concern LiquidDroppable for refactoring.

Akinori MUSHA 10 жил өмнө
parent
commit
8596bd0f0d

+ 17 - 0
app/concerns/liquid_droppable.rb

@@ -0,0 +1,17 @@
+module LiquidDroppable
+  extend ActiveSupport::Concern
+
+  class Drop < Liquid::Drop
+    def initialize(object)
+      @object = object
+    end
+  end
+
+  included do
+    const_set :Drop, Kernel.const_set("#{name}Drop", Class.new(Drop))
+  end
+
+  def to_liquid(*args)
+    self.class::Drop.new(self, *args)
+  end
+end

+ 23 - 11
app/models/agent.rb

@@ -14,6 +14,7 @@ class Agent < ActiveRecord::Base
   include WorkingHelpers
   include LiquidInterpolatable
   include HasGuid
+  include LiquidDroppable
 
   markdown_class_attributes :description, :event_description
 
@@ -383,24 +384,35 @@ class Agent < ActiveRecord::Base
   end
 end
 
-class AgentDrop < Liquid::Drop
-  def initialize(object)
-    @object = object
-  end
-
+class AgentDrop
   def type
     @object.short_type
   end
 
-  %w[options memory name sources receivers schedule disabled keep_events_for propagate_immediately].each { |attr|
+  METHODS = [
+    :name,
+    :type,
+    :options,
+    :memory,
+    :sources,
+    :receivers,
+    :schedule,
+    :disabled,
+    :keep_events_for,
+    :propagate_immediately,
+  ]
+
+  METHODS.each { |attr|
     define_method(attr) {
       @object.__send__(attr)
-    }
+    } unless method_defined?(attr)
   }
 
-  class ::Agent
-    def to_liquid
-      AgentDrop.new(self)
-    end
+  def each(&block)
+    return to_enum(__method__) unless block
+
+    METHODS.each { |attr|
+      yield [attr, __sent__(attr)]
+    }
   end
 end

+ 1 - 1
app/models/agents/event_formatting_agent.rb

@@ -107,7 +107,7 @@ module Agents
     def receive(incoming_events)
       incoming_events.each do |event|
         payload = perform_matching(event.payload)
-        opts = interpolated(EventDrop.new(event, payload))
+        opts = interpolated(event.to_liquid(payload))
         formatted_event = opts['mode'].to_s == "merge" ? event.payload.dup : {}
         formatted_event.merge! opts['instructions']
         formatted_event['created_at'] = event.created_at unless opts['skip_created_at'].to_s == "true"

+ 4 - 12
app/models/event.rb

@@ -5,6 +5,7 @@ require 'json_serialized_field'
 # fields.
 class Event < ActiveRecord::Base
   include JSONSerializedField
+  include LiquidDroppable
 
   attr_accessible :lat, :lng, :payload, :user_id, :user, :expires_at
 
@@ -42,9 +43,9 @@ class Event < ActiveRecord::Base
   end
 end
 
-class EventDrop < Liquid::Drop
+class EventDrop
   def initialize(event, payload = event.payload)
-    @event = event
+    super(event)
     @payload = payload
   end
 
@@ -54,22 +55,13 @@ class EventDrop < Liquid::Drop
     else
       case key
       when 'agent'
-        @event.agent
+        @object.agent
       end
     end
   end
 
-  # Allow iteration using a "for" loop.  Including Enumerable will
-  # enable methods like max, min and sort, but it does not make much
-  # sense since this is a hash-like object.
   def each(&block)
     return to_enum(__method__) unless block
     @payload.each(&block)
   end
-
-  class ::Event
-    def to_liquid
-      EventDrop.new(self)
-    end
-  end
 end