module Loco::Observable
Public Class Methods
new(properties={})
click to toggle source
Create new instance from a hash of properties with values. @param [Hash] properties
Calls superclass method
# File lib/motion-loco/observable.rb, line 16 def initialize(properties={}) super initialize_bindings set_properties(properties) self end
Private Class Methods
included(base)
click to toggle source
# File lib/motion-loco/observable.rb, line 195 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
init()
click to toggle source
Used to create observable view controllers.
Calls superclass method
# File lib/motion-loco/observable.rb, line 7 def init super initialize_bindings set_properties({}) self end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/motion-loco/observable.rb, line 46 def method_missing(method, *args, &block) if method.end_with?('_binding=') || method.end_with?('Binding=') method = method.gsub('_binding=', '').gsub('Binding=', '') if args.first.is_a?(String) if args.first =~ /^[A-Z]/ split_args = args.first.split('.') target = split_args.slice!(0).constantize.instance key_path = split_args.join('.') else target = self key_path = args.first end else target = args.first.first key_path = args.first.last end self.setValue(target.valueForKeyPath(key_path), forKey:method) register_observer(target, key_path) do self.setValue(target.valueForKeyPath(key_path), forKey:method) end else super end end
register_observer(target, key_path, &block)
click to toggle source
# File lib/motion-loco/observable.rb, line 71 def register_observer(target, key_path, &block) unless observer_is_registered?(target, key_path) target.addObserver(self, forKeyPath:key_path.to_s, options:0, context:nil) end observers_for(target, key_path) << block end
remove_all_observers()
click to toggle source
# File lib/motion-loco/observable.rb, line 84 def remove_all_observers return if @observers.nil? @observers.each do |target, key_paths| key_paths.each_key do |key_path| target.removeObserver(self, forKeyPath:key_path) end end @observers.clear end
remove_observer(target, key_path)
click to toggle source
# File lib/motion-loco/observable.rb, line 78 def remove_observer(target, key_path) target.removeObserver(self, forKeyPath:key_path) observers = observers_for(target, key_path) observers[target].delete(key_path) if observers[target].has_key?(key_path) end
set_properties(properties_hash)
click to toggle source
Change one or many properties from a hash of properties with values. @param [Hash] properties_hash
# File lib/motion-loco/observable.rb, line 25 def set_properties(properties_hash) # Set the initial property values from the given hash properties_hash.each do |key, value| self.send("#{key}=", value) end end
Also aliased as: setProperties
update_attributes(properties_hash)
click to toggle source
Change one or many properties from a hash of properties with values. Only updates attributes defined with property. @param [Hash] properties_hash
# File lib/motion-loco/observable.rb, line 36 def update_attributes(properties_hash) self.class.get_class_properties.each do |property| key = property[:name].to_sym if properties_hash.has_key? key self.setValue(properties_hash[key], forKey:key) end end end
Also aliased as: updateAttributes
Private Instance Methods
dealloc()
click to toggle source
Calls superclass method
# File lib/motion-loco/observable.rb, line 128 def dealloc self.remove_all_observers super end
initialize_bindings()
click to toggle source
Create the bindings for the computed properties and observers
# File lib/motion-loco/observable.rb, line 97 def initialize_bindings bindings = self.class.get_class_bindings bindings.each do |binding| binding[:proc].observed_properties.each do |key_path| register_observer(self, key_path) do new_value = binding[:proc].call(self) if binding[:name] self.setValue(new_value, forKey:binding[:name]) end end end end end
observeValueForKeyPath(key_path, ofObject:target, change:change, context:context)
click to toggle source
# File lib/motion-loco/observable.rb, line 112 def observeValueForKeyPath(key_path, ofObject:target, change:change, context:context) observers_for(target, key_path).each do |proc| proc.call end end
observer_is_registered?(target, key_path)
click to toggle source
# File lib/motion-loco/observable.rb, line 118 def observer_is_registered?(target, key_path) return @observers && @observers[target] && @observers[target][key_path.to_s] end
observers_for(target, key_path)
click to toggle source
# File lib/motion-loco/observable.rb, line 122 def observers_for(target, key_path) @observers ||= {} @observers[target] ||= {} @observers[target][key_path.to_s] ||= [] end