Sha256: c96b21fec19d648e5fc18ab8e979fa2c792349b5eb4fd2a4b15360bd5dfe77d6

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'forwardable'

module SonJay
  include Enumerable

  def self.ModelArray(entry_class)
    Class.new(ModelArray).tap{ |c|
      c.send :entry_class=, entry_class
    }
  end

  class ModelArray
    include ActsAsModel
    extend Forwardable

    class << self
      attr_accessor :entry_class
      private :entry_class=
    end

    def initialize
      @entries = []
    end

    def model_content
      self
    end

    def additional
      entry = self.class.entry_class.new
      @entries << entry
      entry
    end

    def load_data(data)
      data.each do |entry_data|
        additional.model_content.load_data entry_data
      end
    end

    def freeze
      super
      @entries.freeze
      self
    end

    def dup
      new_copy = super
      new_copy.instance_variable_set :@entries, @entries.dup
      new_copy
    end

    def clone
      new_copy = super
      new_copy.instance_variable_set :@entries, @entries.clone unless new_copy.frozen?
      new_copy
    end

    def_delegators :@entries, *[
      :[] ,
      :at ,
      :choice,
      :collect,
      :count ,
      :cycle ,
      :drop ,
      :drop_while ,
      :each ,
      :each_index,
      :empty? ,
      :entries ,
      :fetch ,
      :find_index ,
      :first ,
      :include? ,
      :index ,
      :last ,
      :length ,
      :map ,
      :product ,
      :size ,
      :to_json ,
      :to_yaml ,
      :zip
    ]

    alias to_a entries

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
son_jay-0.5.2 lib/son_jay/model_array.rb
son_jay-0.5.1 lib/son_jay/model_array.rb
son_jay-0.5.0 lib/son_jay/model_array.rb