Sha256: baa3f4a67af400ac4356dc81c837ce00ee5744a31fc87b68279a7d4a79507414

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# encoding: UTF-8


module Spontaneous::Collections
  class BoxSet < PrototypeSet

    attr_reader :owner

    def initialize(owner)
      super()
      @owner = owner
      initialize_from_prototypes
    end

    def initialize_from_prototypes
      owner.class.boxes.each do |box_prototype|
        box = box_prototype.get_instance(owner)
        add_box(box)
      end
    end

    alias_method :get_without_ranges, :[]

    def [](*args)
      if args.length == 1
        get_single(args[0])
      else
        args.map { |index| get_single(index) }.flatten
      end
    end

    def export
      self.map { |item| item.export }
    end

    def group(group_name)
      self.select { |box| box._prototype.group == group_name }
    end

    # A call to ${ content } within a layout template will call
    # this #render method. The obvious result of this should be
    # to just render each of the contained boxes.
    def render(format = :html, params = {}, *args)
      map { |box| box.render(format, params, *args) }.join("\n")
    end

    def render_using(renderer, format = :html, params = {}, *args)
      map { |box| box.render_using(renderer, format, params, *args) }.join("\n")
    end

    protected

    def get_single(index)
      case index
      when Range
        order[index].map { | name | named(name) }
      else
        get_without_ranges(index)
      end
    end

    def add_box(box)
      self[box._prototype.name] = box
      getter_name = box._prototype.name
      singleton_class.class_eval do
        define_method(getter_name) { |*args| box.tap { |b| b.template_params = args } }
      end
    end

    def box_groups
      owner.class.box_prototypes.map { |prototype| prototype.group }.compact
    end


    def method_missing(method, *args)
      # allow access by group name e.g. instance.boxes.group_name
      if box_groups.include?(method)
        group(method)
        # self.select { |box| box._prototype.group == method }
      else
        super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta4 lib/spontaneous/collections/box_set.rb
spontaneous-0.2.0.beta3 lib/spontaneous/collections/box_set.rb
spontaneous-0.2.0.beta2 lib/spontaneous/collections/box_set.rb
spontaneous-0.2.0.beta1 lib/spontaneous/collections/box_set.rb