Sha256: 495713844dd8c4e37557537c7c669558f7297fd66bb181a92659b3ee37382438

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Vedeu

  module Model

    # Convert an Array into an object which has some meaning in the context it
    # is being used.
    #
    # @private
    class Collection

      include Enumerable

      attr_accessor :parent, :name

      def self.coerce(collection = [], parent = nil, name = nil)
        if collection.kind_of?(Vedeu::Model::Collection)
          collection

        elsif collection.is_a?(Array)
          new(collection, parent, name)

        else
          new(Array(collection), parent, name)

        end
      end

      def initialize(collection = [], parent = nil, name = nil)
        @collection = collection
        @parent     = parent
        @name       = name
      end

      def [](value)
        @collection[value]
      end

      # @return [Vedeu::Model::Collection]
      def add(*other)
        self.class.new(@collection += other, parent, name)
      end
      alias_method :<<, :add

      def all
        @collection
      end

      def each(&block)
        @collection.each(&block)
      end

      # @return [Boolean]
      def empty?
        @collection.empty?
      end

      # @return [Fixnum]
      def size
        @collection.size
      end

      # @return [String]
      def to_s
        @collection.map(&:to_s).join
      end

    end # Collection

  end # Model

end # Vedeu

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.3.1 lib/vedeu/models/collection.rb
vedeu-0.3.0 lib/vedeu/models/collection.rb