Introduce a new concern LiquidDroppable for refactoring.

Akinori MUSHA 10 years ago
parent
commit
8596bd0f0d
4 changed files with 45 additions and 24 deletions
  1. 17 0
      app/concerns/liquid_droppable.rb
  2. 23 11
      app/models/agent.rb
  3. 1 1
      app/models/agents/event_formatting_agent.rb
  4. 4 12
      app/models/event.rb

+ 17 - 0
app/concerns/liquid_droppable.rb

@@ -0,0 +1,17 @@
1
+module LiquidDroppable
2
+  extend ActiveSupport::Concern
3
+
4
+  class Drop < Liquid::Drop
5
+    def initialize(object)
6
+      @object = object
7
+    end
8
+  end
9
+
10
+  included do
11
+    const_set :Drop, Kernel.const_set("#{name}Drop", Class.new(Drop))
12
+  end
13
+
14
+  def to_liquid(*args)
15
+    self.class::Drop.new(self, *args)
16
+  end
17
+end

+ 23 - 11
app/models/agent.rb

@@ -14,6 +14,7 @@ class Agent < ActiveRecord::Base
14 14
   include WorkingHelpers
15 15
   include LiquidInterpolatable
16 16
   include HasGuid
17
+  include LiquidDroppable
17 18
 
18 19
   markdown_class_attributes :description, :event_description
19 20
 
@@ -383,24 +384,35 @@ class Agent < ActiveRecord::Base
383 384
   end
384 385
 end
385 386
 
386
-class AgentDrop < Liquid::Drop
387
-  def initialize(object)
388
-    @object = object
389
-  end
390
-
387
+class AgentDrop
391 388
   def type
392 389
     @object.short_type
393 390
   end
394 391
 
395
-  %w[options memory name sources receivers schedule disabled keep_events_for propagate_immediately].each { |attr|
392
+  METHODS = [
393
+    :name,
394
+    :type,
395
+    :options,
396
+    :memory,
397
+    :sources,
398
+    :receivers,
399
+    :schedule,
400
+    :disabled,
401
+    :keep_events_for,
402
+    :propagate_immediately,
403
+  ]
404
+
405
+  METHODS.each { |attr|
396 406
     define_method(attr) {
397 407
       @object.__send__(attr)
398
-    }
408
+    } unless method_defined?(attr)
399 409
   }
400 410
 
401
-  class ::Agent
402
-    def to_liquid
403
-      AgentDrop.new(self)
404
-    end
411
+  def each(&block)
412
+    return to_enum(__method__) unless block
413
+
414
+    METHODS.each { |attr|
415
+      yield [attr, __sent__(attr)]
416
+    }
405 417
   end
406 418
 end

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

@@ -107,7 +107,7 @@ module Agents
107 107
     def receive(incoming_events)
108 108
       incoming_events.each do |event|
109 109
         payload = perform_matching(event.payload)
110
-        opts = interpolated(EventDrop.new(event, payload))
110
+        opts = interpolated(event.to_liquid(payload))
111 111
         formatted_event = opts['mode'].to_s == "merge" ? event.payload.dup : {}
112 112
         formatted_event.merge! opts['instructions']
113 113
         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'
5 5
 # fields.
6 6
 class Event < ActiveRecord::Base
7 7
   include JSONSerializedField
8
+  include LiquidDroppable
8 9
 
9 10
   attr_accessible :lat, :lng, :payload, :user_id, :user, :expires_at
10 11
 
@@ -42,9 +43,9 @@ class Event < ActiveRecord::Base
42 43
   end
43 44
 end
44 45
 
45
-class EventDrop < Liquid::Drop
46
+class EventDrop
46 47
   def initialize(event, payload = event.payload)
47
-    @event = event
48
+    super(event)
48 49
     @payload = payload
49 50
   end
50 51
 
@@ -54,22 +55,13 @@ class EventDrop < Liquid::Drop
54 55
     else
55 56
       case key
56 57
       when 'agent'
57
-        @event.agent
58
+        @object.agent
58 59
       end
59 60
     end
60 61
   end
61 62
 
62
-  # Allow iteration using a "for" loop.  Including Enumerable will
63
-  # enable methods like max, min and sort, but it does not make much
64
-  # sense since this is a hash-like object.
65 63
   def each(&block)
66 64
     return to_enum(__method__) unless block
67 65
     @payload.each(&block)
68 66
   end
69
-
70
-  class ::Event
71
-    def to_liquid
72
-      EventDrop.new(self)
73
-    end
74
-  end
75 67
 end