class Attachs::Collection

Attributes

attachments[R]
options[R]
record[R]
record_attribute[R]

Public Class Methods

new(record, record_attribute, options, collection=[]) click to toggle source
# File lib/attachs/collection.rb, line 12
def initialize(record, record_attribute, options, collection=[])
  @record = record
  @record_attribute = record_attribute
  @options = options
  @attachments = build_attachments(collection)
end

Public Instance Methods

<<(value)
Alias for: append
[]=(index, value) click to toggle source
# File lib/attachs/collection.rb, line 40
def []=(index, value)
  if attachment = to_a[index]
    attachment.assign value
  else
    append value
  end
end
append(value) click to toggle source
# File lib/attachs/collection.rb, line 60
def append(value)
  attachment = new
  attachment.assign value
end
Also aliased as: <<
assign(values) click to toggle source
# File lib/attachs/collection.rb, line 48
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
build()
Alias for: new
find(id) click to toggle source
# File lib/attachs/collection.rb, line 34
def find(id)
  to_a.find do |attachment|
    attachment.id == id
  end
end
new() click to toggle source
# File lib/attachs/collection.rb, line 66
def new
  attachment = Attachment.new(record, record_attribute, options)
  attachments << attachment
  attachment
end
Also aliased as: build
to_a() click to toggle source
# File lib/attachs/collection.rb, line 27
def to_a
  attachments.sort_by do |attachment|
    [(attachment.position ? 0 : 1), (attachment.position || 0)]
  end
end
Also aliased as: to_ary
to_ary()
Alias for: to_a

Private Instance Methods

build_attachments(collection) click to toggle source
# File lib/attachs/collection.rb, line 77
def build_attachments(collection)
  collection.map do |attributes|
    Attachment.new record, record_attribute, options, attributes
  end
end