Module: Streamit::DSL::ClassMethods
- Defined in:
- lib/streamit/dsl.rb
Overview
A class method for ActiveRecord models
Sample usage:
stream :create, :actor => :watcher,
:receiver => :item
stream :update, :attributes => :image_url,
Instance Method Summary collapse
Instance Method Details
#stream(action, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/streamit/dsl.rb', line 15 def stream(action, ={}) .assert_valid_keys(:actor, :subject, :receiver, :attributes) raise ArgumentError, ":create or :update required as first parameter" \ unless [:create, :update].any? {|sym| sym == action } table_name = self.model_name.downcase.pluralize attributes = [:attributes] attr_name = if action == :update case attributes when Array, nil :default when Symbol attributes end end stream_type = attr_name.nil? ? "streamit.#{table_name}.#{action}" : "streamit.#{table_name}.#{action}.#{attr_name}" method_name = :"_#{stream_type.gsub('.', '_')}" define_method(method_name) do = [:actor, :receiver, :subject].inject({}) do |memo, sym| memo[sym] = [sym] ? send([sym]) : (sym == :actor ? self : nil) memo end.merge(:stream_type => stream_type, :started_at => Time.now) if (action != :update || instance_variable_get("@attr_changed")) Streamit.save_stream!() end instance_variable_set("@attr_changed", false) end if action == :update before_update_method_name = "_before#{method_name}" define_method(before_update_method_name) do attr_changed = if action == :update case attributes when nil changed? when Array attributes.any? { |attr| send(:"#{attr}_changed?") } when Symbol send(:"#{attributes}_changed?") end end instance_variable_set("@attr_changed", attr_changed) end send(:"before_update", before_update_method_name) end send(:"after_#{action}", method_name) end |