Class: Kernel

Inherits:
Object
  • Object
show all
Defined in:
motion-prime/core_ext/kernel.rb

Instance Method Summary collapse

Instance Method Details

#allocate_strong_references(key = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'motion-prime/core_ext/kernel.rb', line 35

def allocate_strong_references(key = nil)
  unless self.respond_to?(:strong_references)
    Prime.logger.debug "User must define `strong_references` in `#{self.class.name}`"
    return false
  end

  refs = Array.wrap(self.strong_references).compact
  unless refs.present?
    Prime.logger.debug "`strong_references` are empty for `#{self.class.name}`"
    return false
  end

  @_strong_references ||= {}
  key ||= [@_strong_references.count, Time.now.to_i].join('_')
  @_strong_references[key] = refs.map(&:strong_ref)
  key
end

#allocated_references_released?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'motion-prime/core_ext/kernel.rb', line 63

def allocated_references_released?
  unless self.respond_to?(:strong_references)
    Prime.logger.debug "User must define `strong_references` in `#{self.class.name}`"
    return false
  end
  @_strong_references.all? { |key, ref| @_strong_references.count; ref.retainCount == @_strong_references.count }
end

#benchmark(key, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'motion-prime/core_ext/kernel.rb', line 2

def benchmark(key, &block)
  if Prime.env.development?
    t = Time.now
    result = block.call
    time = Time.now - t
    MotionPrime.benchmark_data[key] ||= {}
    MotionPrime.benchmark_data[key][:count] ||= 0
    MotionPrime.benchmark_data[key][:total] ||= 0
    MotionPrime.benchmark_data[key][:count] += 1
    MotionPrime.benchmark_data[key][:total] += time
    result
  else
    block.call
  end
end

#class_name_without_kvoObject



23
24
25
# File 'motion-prime/core_ext/kernel.rb', line 23

def class_name_without_kvo
  self.class.name.gsub(/^NSKVONotifying_/, '')
end

#clear_instance_variables(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'motion-prime/core_ext/kernel.rb', line 71

def clear_instance_variables(options = {})
  ivars = self.instance_variables
  excluded_ivars = Array.wrap(options[:except]).map(&:to_s)
  clear_block = proc { |ivar|
    next if excluded_ivars.include?(ivar[1..-1])
    self.instance_variable_set(ivar, nil)
  }.weak!
  ivars.each(&clear_block)
end

#pp(*attrs) ⇒ Object



18
19
20
21
# File 'motion-prime/core_ext/kernel.rb', line 18

def pp(*attrs)
  NSLog([*attrs].map(&:inspect).join(' '))
  attrs
end

#release_strong_references(key = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'motion-prime/core_ext/kernel.rb', line 53

def release_strong_references(key = nil)
  unless self.respond_to?(:strong_references)
    Prime.logger.debug "User must define `strong_references` in `#{self.class.name}`"
    return false
  end
  key ||= @_strong_references.keys.last
  @_strong_references.try(:delete, key)
  key
end

#strong_refObject



31
32
33
# File 'motion-prime/core_ext/kernel.rb', line 31

def strong_ref
  self
end

#weak_refObject



27
28
29
# File 'motion-prime/core_ext/kernel.rb', line 27

def weak_ref
  WeakRef.new(self)
end