Sha256: b433ff833770815dc3122ede5c52ea534bae9d1c7ae931a327d41d7c5f8ea84d

Contents?: true

Size: 1.27 KB

Versions: 25

Compression:

Stored size: 1.27 KB

Contents

# @provides MotionKit::Parent
module MotionKit
  # Simple class that returns data about the parent element
  # for use while setting the styles of a child element.
  class Parent

    attr_reader :element

    def initialize(element)
      @element = element
    end

    def origin
      try(:frame, :origin)
    end

    def size
      try(:frame, :size)
    end

    def x
      try(:frame, :origin, :x)
    end

    def y
      try(:frame, :origin, :y)
    end

    def width
      try(:frame, :size, :width)
    end

    def height
      try(:frame, :size, :height)
    end

    def center_x
      width / 2.0 if width
    end

    def center_y
      height / 2.0 if height
    end

    def center
      CGPointMake(center_x, center_y) if width && height
    end

  protected

    # 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.
    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

  end

end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
motion-kit-0.9.4 lib/motion-kit/layouts/parent.rb
motion-kit-0.9.3 lib/motion-kit/layouts/parent.rb
motion-kit-0.9.2 lib/motion-kit/layouts/parent.rb
motion-kit-0.9.1 lib/motion-kit/layouts/parent.rb
motion-kit-0.9.0 lib/motion-kit/layouts/parent.rb