Class: Fragmentary::Subscriber
- Inherits:
-
Object
- Object
- Fragmentary::Subscriber
- Defined in:
- lib/fragmentary/subscriber.rb
Overview
Each fragment subclass has a unique Subscriber instance reponsible for handling subscriptions to publishers. Each subscriber maintains a hash of Subscriptions, one for each publisher it subscribes to. The ‘subscribe_to’ method instantiates each new Subscription in turn and executes its block against against the Subscriber in order to define handlers for each publisher event of interest. Any other method invoked within a handler is delegated to the client, i.e. the fragment subclass that the subscriber is reponsible for.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
-
#initialize(client) ⇒ Subscriber
constructor
A new instance of Subscriber.
- #method_missing(method, *args) ⇒ Object
- #subscribe_to(publisher, block) ⇒ Object
Constructor Details
#initialize(client) ⇒ Subscriber
Returns a new instance of Subscriber.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fragmentary/subscriber.rb', line 14 def initialize(client) @client = client @subscriptions = Hash.new do |h, key| if Object.const_defined?(key) and (publisher = key.constantize) < ActiveRecord::Base h[key] = Subscription.new(publisher, self) else nil end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
33 34 35 |
# File 'lib/fragmentary/subscriber.rb', line 33 def method_missing(method, *args) @client.send(method, *args) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/fragmentary/subscriber.rb', line 12 def client @client end |
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
12 13 14 |
# File 'lib/fragmentary/subscriber.rb', line 12 def subscriptions @subscriptions end |
Instance Method Details
#subscribe_to(publisher, block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/fragmentary/subscriber.rb', line 25 def subscribe_to(publisher, block) if subscriptions[publisher.name] mod = Module.new mod.module_exec(&block) self.extend mod end end |