Class: Attachs::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/attachs/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, record_attribute, options, collection = []) ⇒ Collection

Returns a new instance of Collection.



12
13
14
15
16
17
# 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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/attachs/collection.rb', line 10

def options
  @options
end

#recordObject (readonly)

Returns the value of attribute record.



10
11
12
# File 'lib/attachs/collection.rb', line 10

def record
  @record
end

#record_attributeObject (readonly)

Returns the value of attribute record_attribute.



10
11
12
# File 'lib/attachs/collection.rb', line 10

def record_attribute
  @record_attribute
end

Instance Method Details

#[]=(index, value) ⇒ Object



40
41
42
43
44
45
46
# 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) ⇒ Object Also known as: <<



60
61
62
63
# File 'lib/attachs/collection.rb', line 60

def append(value)
  attachment = new
  attachment.assign value
end

#assign(values) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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

#find(id) ⇒ Object



34
35
36
37
38
# File 'lib/attachs/collection.rb', line 34

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

#newObject Also known as: build



66
67
68
69
70
# File 'lib/attachs/collection.rb', line 66

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

#to_aObject Also known as: to_ary



27
28
29
30
31
# File 'lib/attachs/collection.rb', line 27

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