Sha256: fcaeb563b29ab2f30595343f56c716a992e37872080ba7539980729da227f561

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# @provides MotionKit::BaseLayoutClassMethods
module MotionKit
  module BaseLayoutClassMethods
    def target_klasses
      # We don't want subclasses, just BaseLayout
      return BaseLayout.target_klasses unless self == BaseLayout
      @target_klasses ||= {}
    end

    def targets(klass=nil)
      return nil if klass.nil? && self == BaseLayout
      return @targets || superclass.targets if klass.nil?
      @targets = klass
      BaseLayout.target_klasses[klass] = self
      nil
    end

    # Instantiates a new Layout instance using `layout` as the root-level layout
    def layout_for(layout, klass)
      memoized_klass = memoize(klass)
      memoized_klass.new_child(layout) if memoized_klass
    end

    # Cache registered classes
    def memoize(klass)
      @memoize ||= {}
      @memoize[klass] ||= begin
        while klass
          break if registered_class = target_klasses[klass]
          klass = klass.superclass
        end
        @memoize[klass] = registered_class if registered_class
      end
      @memoize[klass]
    end

    def new_child(layout=nil)
      child = self.new
      child.set_layout(layout)
      child
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
motion-kit-0.10.0 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.6 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.4 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.3 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.2 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.1 lib/motion-kit/layouts/base_layout_class_methods.rb
motion-kit-0.9.0 lib/motion-kit/layouts/base_layout_class_methods.rb