Class: Seatbelt::Collections::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/seatbelt/collections/collection.rb

Overview

Public: The base collection attribute type that is used in ‘has_many’ associations.

Needs a corrosponding model. See Seatbelt::Dcoument::Associations for further details.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initialize_primitive(klass) ⇒ Object

Public: Initializes the collections primitive to allow later type checking.Adds a method ‘acceptable_item_name’ to the primitive, that is later called at the time of type checking to match the allowed class.

klass - The associatzed klass that is insertable into the collection.



22
23
24
25
26
27
28
# File 'lib/seatbelt/collections/collection.rb', line 22

def self.initialize_primitive(klass)
  class_eval <<-RUBY, __FILE__, __LINE__
    def acceptable_item_name
      return "#{klass.name}"
    end
  RUBY
end

Instance Method Details

#<<(item) ⇒ Object

Public: Adds an object to the ‘has_many’ association.

item - An instance of the model class used within the collection

or an Hash with attribute key/value pairs.

Example

region.hotels << {:name => "Radisson London Stansted"}
region.hotels << super_hotel_in_dubai

Raises Seatbelt::Errors::TypeMissmatchError if unexpected object is passed.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/seatbelt/collections/collection.rb', line 41

def <<(item)
  case item.class.name
  when acceptable_item_name then super(item)
  when "Hash" then
    begin
      super(Module.const_get(acceptable_item_name).new(item))
    end
  else
    raise Seatbelt::Errors::TypeMissmatchError.
                            new(acceptable_item_name,
                                item.class.name)
  end
end