rm-extensions 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d64ca280c2914d4e0e8ebda0af7b8900c4a71d0
4
- data.tar.gz: 422760a452010841d394d296a59090c5967b43cd
3
+ metadata.gz: def362607872dfcd691017faa171e66967d0630c
4
+ data.tar.gz: bbeb76a150653503536aa8e2cf02565d48d0bca9
5
5
  SHA512:
6
- metadata.gz: 613afb58ed64e974e51cdfc117e8627ecc381084c2d1cf6e571b2d0df6a95ba071fab76efeda59cf9eb0e6a203a3d9cce1908a5e3cdf05be8b9179e8c947c4dd
7
- data.tar.gz: 41375da6369ebe8921c5f47d2576ea774cc353b8701774e092ffaf7df6948e6b65b87bf203f10c2a055603e324c93b8ee4689205f76f5597ee267ee38a45f86d
6
+ metadata.gz: 3fc8a7624f1ecfe03c8af4bcea0f7c31092c3403091bc99754071efde2d98b2927531c361ca809c817b4a70753357ca0395e9e2c5563c3c8506856e20cda9de6
7
+ data.tar.gz: 608c671f2a9d9656961005e4e86d2e21a0d14e050dfbea5eb5bc759404ac992bc1d486507936bc149d9a0c4147e0a4937152d3b4ddebe90c027e66dc46c4f852
@@ -0,0 +1,56 @@
1
+ module RMExtensions
2
+
3
+ module ObjectExtensions
4
+
5
+ module Deallocation
6
+
7
+ # perform a block before +self+ will dealloc.
8
+ # the block given should have one argument, the object about to be deallocated.
9
+ def rmext_on_dealloc(&block)
10
+ internalObject = ::RMExtensions::OnDeallocInternalObject.create("#{self.class.name}:#{object_id}", self, block)
11
+ @rmext_on_dealloc_blocks ||= {}
12
+ @rmext_on_dealloc_blocks[internalObject] = internalObject
13
+ nil
14
+ end
15
+
16
+ # removes a previously added block from the deallocation callback list
17
+ def rmext_cancel_on_dealloc(block)
18
+ @rmext_on_dealloc_blocks ||= {}
19
+ if internalObject = @rmext_on_dealloc_blocks[block]
20
+ internalObject.block = nil
21
+ @rmext_on_dealloc_blocks.delete(block)
22
+ end
23
+ nil
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ # Used internally by +rmext_on_dealloc+. The idea is this object is added to the
31
+ # object we want to watch for deallocation. When the object we want to watch
32
+ # is about to dealloc, this object will dealloc first, so we can execute the block.
33
+ # the object it follows is kept only as a weak reference to not create
34
+ # a retain cycle.
35
+ class OnDeallocInternalObject
36
+ attr_accessor :description, :block
37
+ rmext_weak_attr_accessor :obj
38
+ def self.create(description, obj, block)
39
+ x = new
40
+ x.description = description
41
+ x.obj = obj
42
+ x.block = block
43
+ x
44
+ end
45
+ def dealloc
46
+ # p "dealloc OnDeallocInternalObject #{description}"
47
+ if block
48
+ block.call(obj)
49
+ self.block = nil
50
+ end
51
+ super
52
+ end
53
+ end
54
+
55
+ end
56
+ Object.send(:include, ::RMExtensions::ObjectExtensions::Deallocation)
data/lib/motion/layout.rb CHANGED
@@ -46,24 +46,27 @@ module RMExtensions
46
46
  end
47
47
  end
48
48
 
49
- def view(view=nil)
50
- if view
51
- @view = view
52
- end
53
- @view
49
+ def view(view)
50
+ @view = view
54
51
  end
55
52
 
56
- def subviews(subviews=nil)
57
- if subviews
58
- @subviews = subviews
59
- @subviews.values.each do |subview|
60
- subview.translatesAutoresizingMaskIntoConstraints = false
61
- @view.addSubview(subview)
62
- end
53
+ def view=(v)
54
+ view(v)
55
+ end
56
+
57
+ def subviews(subviews)
58
+ @subviews = subviews
59
+ @subviews.values.each do |subview|
60
+ subview.translatesAutoresizingMaskIntoConstraints = false
61
+ @view.addSubview(subview)
63
62
  end
64
63
  @subviews
65
64
  end
66
65
 
66
+ def subviews=(views)
67
+ subviews(views)
68
+ end
69
+
67
70
  def eqs(str)
68
71
  str.split("\n").map(&:strip).select { |x| !x.empty? }.map do |line|
69
72
  eq(line)
@@ -143,16 +146,8 @@ module RMExtensions
143
146
 
144
147
  # normalize
145
148
 
146
- res_item = item == "view" ? @view : @subviews[item]
147
- res_item_attribute = ATTRIBUTE_LOOKUP[item_attribute]
148
- res_related_by = RELATED_BY_LOOKUP[related_by]
149
- res_to_item = if to_item
150
- to_item == "view" ? @view : @subviews[to_item]
151
- end
152
- res_to_item_attribute = ATTRIBUTE_LOOKUP[to_item_attribute]
153
- res_multiplier = multiplier ? Float(multiplier) : 1.0
149
+ res_item = view_for_item(item)
154
150
  res_constant = constant ? Float(PRIORITY_LOOKUP[constant] || constant) : 0.0
155
- res_priority = priority ? Integer(PRIORITY_LOOKUP[priority] || priority) : nil
156
151
 
157
152
  if res_item
158
153
  case item_attribute
@@ -167,6 +162,13 @@ module RMExtensions
167
162
  end
168
163
  end
169
164
 
165
+ res_item_attribute = ATTRIBUTE_LOOKUP[item_attribute]
166
+ res_related_by = RELATED_BY_LOOKUP[related_by]
167
+ res_to_item = to_item ? view_for_item(to_item) : nil
168
+ res_to_item_attribute = ATTRIBUTE_LOOKUP[to_item_attribute]
169
+ res_multiplier = multiplier ? Float(multiplier) : 1.0
170
+ res_priority = priority ? Integer(PRIORITY_LOOKUP[priority] || priority) : nil
171
+
170
172
  errors = []
171
173
  errors.push("Invalid view1: #{item}") unless res_item
172
174
  errors.push("Invalid attr1: #{item_attribute}") unless res_item_attribute
@@ -226,5 +228,15 @@ module RMExtensions
226
228
  "#{item}.#{item_attribute} #{related_by} #{to_item}.#{to_item_attribute} * #{multiplier} + #{constant} @ #{priority}"
227
229
  end
228
230
 
231
+ private
232
+
233
+ def view_for_item(item)
234
+ if item == "view"
235
+ @view
236
+ elsif v = (@subviews && @subviews[item])
237
+ v
238
+ end
239
+ end
240
+
229
241
  end
230
242
  end
@@ -5,7 +5,7 @@ module RMExtensions
5
5
  module Observation
6
6
 
7
7
  def rmext_observation_proxy
8
- @rmext_observation_proxy ||= ObservationProxy.new(self.inspect)
8
+ @rmext_observation_proxy ||= ObservationProxy.new(self)
9
9
  end
10
10
 
11
11
  # observe an object.key. takes a block that will be called with the
@@ -82,21 +82,30 @@ module RMExtensions
82
82
  COLLECTION_OPERATIONS = [ NSKeyValueChangeInsertion, NSKeyValueChangeRemoval, NSKeyValueChangeReplacement ]
83
83
  DEFAULT_OPTIONS = NSKeyValueObservingOptionNew
84
84
 
85
- def initialize(desc)
86
- @desc = desc
85
+ def initialize(obj)
86
+ obj.rmext_on_dealloc do |x|
87
+ cleanup
88
+ end
89
+ @desc = obj.inspect
87
90
  @events = {}
88
91
  @targets = {}
89
- # p "created #{self.inspect} for #{@desc}"
92
+ if ::RMExtensions.debug?
93
+ p "created ObservationProxy for #{@desc}"
94
+ end
90
95
  end
91
96
 
92
- # clean up on dellocation
93
97
  def dealloc
94
- # p "dealloc #{self.inspect} for #{@desc}"
95
- cleanup
98
+ @did_dealloc = true
99
+ if ::RMExtensions.debug?
100
+ p "dealloc ObservationProxy for #{@desc}"
101
+ end
96
102
  super
97
103
  end
98
104
 
99
105
  def cleanup
106
+ if ::RMExtensions.debug?
107
+ p "cleanup #{@desc}"
108
+ end
100
109
  unobserve_all
101
110
  off_all
102
111
  true
@@ -150,13 +159,16 @@ module RMExtensions
150
159
  # NSKeyValueObserving Protocol
151
160
 
152
161
  def observeValueForKeyPath(key_path, ofObject:target, change:change, context:context)
153
- return if target.nil?
154
- key_paths = @targets[target] || {}
155
- blocks = key_paths[key_path] || []
156
- blocks.each do |block|
157
- args = [ change[NSKeyValueChangeNewKey] ]
158
- args << change[NSKeyValueChangeIndexesKey] if collection?(change)
159
- rmext_on_main_q { block.call(*args) }
162
+ rmext_on_main_q do
163
+ next if @did_dealloc
164
+ next if target.nil?
165
+ key_paths = @targets[target] || {}
166
+ blocks = key_paths[key_path] || []
167
+ blocks.each do |block|
168
+ args = [ change[NSKeyValueChangeNewKey] ]
169
+ args << change[NSKeyValueChangeIndexesKey] if collection?(change)
170
+ block.call(*args)
171
+ end
160
172
  end
161
173
  end
162
174
 
data/lib/motion/util.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  module RMExtensions
2
2
 
3
+ def self.debug?
4
+ !!@debug
5
+ end
6
+
7
+ def self.debug!(bool=true)
8
+ @debug = bool
9
+ end
10
+
3
11
  module ObjectExtensions
4
12
 
5
13
  module Util
@@ -1,3 +1,3 @@
1
1
  module RMExtensions
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/rm-extensions.rb CHANGED
@@ -9,6 +9,7 @@ Motion::Project::App.setup do |app|
9
9
  layout
10
10
  util
11
11
  accessors
12
+ deallocation
12
13
  observation
13
14
  queues
14
15
  ).reverse.each do |x|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rm-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Noon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-19 00:00:00.000000000 Z
11
+ date: 2013-08-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Extensions and helpers for dealing with various areas of rubymotion
14
14
  email:
@@ -24,6 +24,7 @@ files:
24
24
  - Rakefile
25
25
  - app/app_delegate.rb
26
26
  - lib/motion/accessors.rb
27
+ - lib/motion/deallocation.rb
27
28
  - lib/motion/layout.rb
28
29
  - lib/motion/observation.rb
29
30
  - lib/motion/queues.rb