Sha256: 839b70162613cd00193ce161adf6c58461ad31b7a91df895b95530baa9decee7

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Attachs
  class Collection

    delegate(
      :map, :each, :size, :length, :count, :reject, :select, :any?,
      :all?, :none?, :first, :second, :last, :[],
      to: :to_a
    )

    attr_reader :record, :record_attribute, :options

    def initialize(record, record_attribute, options, collection=[])
      @record = record
      @record_attribute = record_attribute
      @options = options
      @attachments = build_attachments(collection)
    end

    %i(save destroy persist unpersist).each do |name|
      define_method name do
        each do |attachment|
          attachment.send name
        end
      end
    end

    def to_a
      attachments.sort_by do |attachment|
        [(attachment.position ? 0 : 1), (attachment.position || 0)]
      end
    end
    alias_method :to_ary, :to_a

    def find(id)
      to_a.find do |attachment|
        attachment.id == id
      end
    end

    def []=(index, value)
      if attachment = to_a[index]
        attachment.assign value
      else
        append value
      end
    end

    def assign(values)
      if values.is_a?(Array)
        values.each.with_index do |value, index|
          if attachment = attachments[index]
            attachment.assign value
          else
            append value
          end
        end
      end
    end

    def append(value)
      attachment = new
      attachment.assign value
    end
    alias_method :<<, :append

    def new
      attachment = Attachment.new(record, record_attribute, options)
      attachments << attachment
      attachment
    end
    alias_method :build, :new

    private

    attr_reader :attachments

    def build_attachments(collection)
      collection.map do |attributes|
        Attachment.new record, record_attribute, options, attributes
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attachs-4.0.0.2 lib/attachs/collection.rb
attachs-4.0.0.1 lib/attachs/collection.rb
attachs-4.0.0.0 lib/attachs/collection.rb