Module: Replay::EventDeclarations

Defined in:
lib/replay/event_declarations.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



18
19
20
# File 'lib/replay/event_declarations.rb', line 18

def method_missing(name, *args)
  declare_event(self, name, args.first)
end

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/replay/event_declarations.rb', line 3

def self.included(base)
  base.extend(Replay::Events)
end

Instance Method Details

#declare_event(base, name, props) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/replay/event_declarations.rb', line 22

def declare_event(base, name, props)
  klass = Class.new do
    include Replay::EventDecorator
    attribute :published_at, Time, default: lambda{|p,a| Time.now}
    values do
      props.keys.each do |prop|
        attribute prop, props[prop]
      end
    end
    include Virtus::Equalizer.new("#{name.to_s} equalizer", (self.attribute_set.map(&:name) - [:published_at]).map(&:to_s))
  end
  base.const_set name, klass
end

#included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/replay/event_declarations.rb', line 7

def included(base)
  self.constants.each do |c| 
    base.const_set(c, const_get(c).dup)
    klass = base.const_get(c)
    base.class_eval do
      define_method c do |props|
        klass.new props
      end
    end
  end
end