Module: Lifestreamable::LifestreamableClassMethods

Defined in:
lib/lifestreamable/lifestreamable.rb

Constant Summary collapse

@@lifestream_options =
{}

Instance Method Summary collapse

Instance Method Details

#filter(lifestream) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/lifestreamable/lifestreamable.rb', line 187

def filter(lifestream)
  option = self.lifestream_options[:filter]
  lifestream = case option
    when Proc
      option.call(self, lifestream)
    when String, Symbol
      send(option.to_s, lifestream) if respond_to?(option.to_s)
    else
      lifestream
  end 
  lifestream
end

#lifestream_optionsObject



153
154
155
# File 'lib/lifestreamable/lifestreamable.rb', line 153

def lifestream_options
  @@lifestream_options
end

#lifestreamable(options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/lifestreamable/lifestreamable.rb', line 157

def lifestreamable(options)
  include LifestreamableInstanceMethods
  options.to_options

  unknown_keys = options.keys - ACCEPTABLE_OPTIONS
  unless unknown_keys.blank?
    raise LifestreamableException.new("Unknown keys for lifestreamable #{unknown_keys.inspect}") 
  end

  required_keys = REQUIRED_OPTIONS - options.keys
  unless required_keys.blank?
    raise LifestreamableException.new("Some requires option for lifestreamable are missing #{required_keys.inspect}") 
  end

  options[:on].to_a.each do |option_on|
    case option_on
      when :update
        Lifestreamable::UpdateObserver.instance.add_class_observer self.class_name.constantize
      when :create
        Lifestreamable::CreateObserver.instance.add_class_observer self.class_name.constantize
      when :destroy
        Lifestreamable::DestroyObserver.instance.add_class_observer self.class_name.constantize
      else
        raise LifestreamableException.new("option \"#{option_on}\" is not supported for Lifestreamable")
    end
  end
  ACCEPTABLE_OPTIONS.each {|k| @@lifestream_options[k]=options[k] } 

end