class MotionKit::Parent

Simple class that returns data about the parent element for use while setting the styles of a child element.

Attributes

element[R]

Public Class Methods

new(element) click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 9
def initialize(element)
  @element = element
end

Public Instance Methods

center() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 49
def center
  CGPointMake(center_x, center_y) if width && height
end
center_x() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 41
def center_x
  width / 2.0 if width
end
center_y() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 45
def center_y
  height / 2.0 if height
end
frame() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 13
def frame
  try(:frame)
end
height() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 37
def height
  try(:frame, :size, :height)
end
origin() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 17
def origin
  try(:frame, :origin)
end
size() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 21
def size
  try(:frame, :size)
end
width() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 33
def width
  try(:frame, :size, :width)
end
x() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 25
def x
  try(:frame, :origin, :x)
end
y() click to toggle source
# File lib/motion-kit/helpers/parent.rb, line 29
def y
  try(:frame, :origin, :y)
end

Protected Instance Methods

try(*method_chain) click to toggle source

Convenience method that takes a list of method calls and tries them end-to-end, returning nil if any fail to respond to that method name. Very similar to ActiveSupport's `.try` method.

# File lib/motion-kit/helpers/parent.rb, line 59
def try(*method_chain)
  obj = self.element
  method_chain.each do |m|
    # We'll break out and return nil if any part of the chain
    # doesn't respond properly.
    (obj = nil) && break unless obj.respond_to?(m)
    obj = obj.send(m)
  end
  obj
end