Module: Loco::Observable

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/motion-loco/observable.rb', line 60

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

#initObject

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.

Parameters:

  • properties (Hash) (defaults to: {})


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

#initWithFrame(frame) ⇒ Object

Used to create observable views.



24
25
26
27
28
29
# File 'lib/motion-loco/observable.rb', line 24

def initWithFrame(frame)
  super
  initialize_bindings
  set_properties({})
  self
end

#initWithStyle(style, reuseIdentifier: reuseIdentifier) ⇒ Object

Used to create observable table view cells



32
33
34
35
36
37
# File 'lib/motion-loco/observable.rb', line 32

def initWithStyle(style, reuseIdentifier:reuseIdentifier)
  super
  initialize_bindings
  set_properties({})
  self
end

#register_observer(target, key_path, &block) ⇒ Object



85
86
87
88
89
90
# File 'lib/motion-loco/observable.rb', line 85

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_observersObject



98
99
100
101
102
103
104
105
106
# File 'lib/motion-loco/observable.rb', line 98

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



92
93
94
95
96
# File 'lib/motion-loco/observable.rb', line 92

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

Change one or many properties from a hash of properties with values.

Parameters:

  • properties_hash (Hash)


41
42
43
44
45
46
# File 'lib/motion-loco/observable.rb', line 41

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

Change one or many properties from a hash of properties with values. Only updates attributes defined with #property.

Parameters:

  • properties_hash (Hash)


51
52
53
54
55
56
57
58
# File 'lib/motion-loco/observable.rb', line 51

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