Class: Attachs::Collection
- Inherits:
-
Object
- Object
- Attachs::Collection
- Defined in:
- lib/attachs/collection.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#record_attribute ⇒ Object
readonly
Returns the value of attribute record_attribute.
Instance Method Summary collapse
- #[]=(index, value) ⇒ Object
- #append(value) ⇒ Object (also: #<<)
- #assign(values) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(record, record_attribute, options, collection = []) ⇒ Collection
constructor
A new instance of Collection.
- #new ⇒ Object (also: #build)
- #to_a ⇒ Object (also: #to_ary)
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, , collection=[]) @record = record @record_attribute = record_attribute @options = @attachments = (collection) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/attachs/collection.rb', line 10 def @options end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
10 11 12 |
# File 'lib/attachs/collection.rb', line 10 def record @record end |
#record_attribute ⇒ Object (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 = to_a[index] .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) = new .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 = [index] .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 || .id == id end end |
#new ⇒ Object Also known as: build
66 67 68 69 70 |
# File 'lib/attachs/collection.rb', line 66 def new = Attachment.new(record, record_attribute, ) << end |
#to_a ⇒ Object Also known as: to_ary
27 28 29 30 31 |
# File 'lib/attachs/collection.rb', line 27 def to_a .sort_by do || [(.position ? 0 : 1), (.position || 0)] end end |