Module: Kvo

Defined in:
lib/kvo.rb,
lib/kvo/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



41
42
43
44
# File 'lib/kvo.rb', line 41

def self.included(receiver)
  receiver.extend         Publisher
  receiver.extend         ClassMethods
end

Instance Method Details

#kvo_bind_attr(attr_sym, target, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kvo.rb', line 24

def kvo_bind_attr(attr_sym, target, opts={})
  target_attr = opts[:to] || attr_sym
  change_event = "#{attr_sym}_changed".to_sym

  unless self.class.published_events.include? change_event
    raise "#{attr_sym} is not a kvo attr; use #{self.class}.kvo_attr_accessor #{attr_sym}" 
  end
  
  self.when change_event do |old, new_val|
    val = new_val
    # transform
    val = yield(new_val) if block_given?

    target.send "#{target_attr}=", val
  end
end