Sha256: 89be27fa516256ba8ea7dc5f06e1e9791412dec803af9f8a9956e3d3647a4f54

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module TimelineFu
  module Fires
    def self.included(klass)
      klass.send(:extend, ClassMethods)
    end

    module ClassMethods
      def fires(event_type, opts)
        raise ArgumentError, "Argument :on is mandatory" unless opts.has_key?(:on)
        
        method_name = :"fire_#{event_type}_after_#{opts[:on]}"
        define_method(method_name) do
          create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
            memo[sym] = send(opts[sym]) if opts[sym] and opts[sym] != :self
            memo[sym] ||= self if opts[sym] == :self or sym == :subject
            memo
          end
          create_options[:event_type] = event_type.to_s
          
          if create_options[:secondary_subject].class == Array
            create_options_col = create_options[:secondary_subject].map do |secondary_subject|
              create_options.merge({:secondary_subject => secondary_subject})
            end            
            create_options_col.map {|create_options| TimelineEvent.create!(create_options)}
          else
            TimelineEvent.create!(create_options)
          end
        end
        send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mcfearsome-timeline_fu-0.2.0 lib/timeline_fu/fires.rb