Sha256: 062880663caeb95c8bcf7a10142b7409c3c96b68d7a7eb7d044e788c2f858110

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 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 frame
      try(:frame)
    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

11 entries across 11 versions & 1 rubygems

Version Path
motion-kit-1.1.1 lib/motion-kit/helpers/parent.rb
motion-kit-1.1.0 lib/motion-kit/helpers/parent.rb
motion-kit-1.0.3 lib/motion-kit/helpers/parent.rb
motion-kit-1.0.2 lib/motion-kit/helpers/parent.rb
motion-kit-1.0.1 lib/motion-kit/helpers/parent.rb
motion-kit-1.0.0 lib/motion-kit/helpers/parent.rb
motion-kit-0.18.0 lib/motion-kit/helpers/parent.rb
motion-kit-0.17.0 lib/motion-kit/helpers/parent.rb
motion-kit-0.16.0 lib/motion-kit/helpers/parent.rb
motion-kit-0.15.0 lib/motion-kit/helpers/parent.rb
motion-kit-0.14.2 lib/motion-kit/helpers/parent.rb