Module: Loco::Observable
- Included in:
- Controller, Model, RecordArray, Resizable, UI::TableViewCell, ViewController
- Defined in:
- lib/motion-loco/observable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#init ⇒ Object
Used to create observable view controllers.
-
#initialize(properties = {}) ⇒ Object
Create new instance from a hash of properties with values.
- #method_missing(method, *args, &block) ⇒ Object
- #register_observer(target, key_path, &block) ⇒ Object
- #remove_all_observers ⇒ Object
- #remove_observer(target, key_path) ⇒ Object
-
#set_properties(properties_hash) ⇒ Object
(also: #setProperties)
Change one or many properties from a hash of properties with values.
-
#update_attributes(properties_hash) ⇒ Object
(also: #updateAttributes)
Change one or many properties from a hash of properties with values.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
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/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 |
Instance Method Details
#init ⇒ Object
Used to create observable view controllers.
7 8 9 10 11 12 |
# File 'lib/motion-loco/observable.rb', line 7 def init super initialize_bindings set_properties({}) self end |
#initialize(properties = {}) ⇒ Object
Create new instance from a hash of properties with values.
16 17 18 19 20 21 |
# File 'lib/motion-loco/observable.rb', line 16 def initialize(properties={}) super initialize_bindings set_properties(properties) self end |
#register_observer(target, key_path, &block) ⇒ Object
71 72 73 74 75 76 |
# 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 ⇒ Object
84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
78 79 80 81 82 |
# 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) ⇒ Object Also known as: setProperties
Change one or many properties from a hash of properties with values.
25 26 27 28 29 30 |
# 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 |
#update_attributes(properties_hash) ⇒ Object Also known as: updateAttributes
Change one or many properties from a hash of properties with values. Only updates attributes defined with #property.
36 37 38 39 40 41 42 43 |
# 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 |