Class: EfoNelfo::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/efo_nelfo/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, post_type) ⇒ Collection

Returns a new instance of Collection.



13
14
15
16
17
# File 'lib/efo_nelfo/collection.rb', line 13

def initialize(owner, post_type)
  @owner     = owner
  @post_type = post_type
  @list      = []
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



9
10
11
# File 'lib/efo_nelfo/collection.rb', line 9

def owner
  @owner
end

#post_typeObject (readonly)

Returns the value of attribute post_type.



9
10
11
# File 'lib/efo_nelfo/collection.rb', line 9

def post_type
  @post_type
end

Instance Method Details

#<<(obj) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/efo_nelfo/collection.rb', line 19

def <<(obj)
  obj = post_type_class.new(obj) if obj.is_a? Hash
  raise EfoNelfo::InvalidPostType if obj.nil? || (obj.is_a?(EfoNelfo::PostType) && obj.post_type != post_type)

  # Set the index if the post has an index property
  obj.index = size + 1 if obj.has_property?(:index)

  @list << obj
end

#delete(index) ⇒ Object



29
30
31
# File 'lib/efo_nelfo/collection.rb', line 29

def delete(index)
  @list.delete_at index
end

#find_by(args) ⇒ Object

find_by property_name: ‘test’



38
39
40
41
# File 'lib/efo_nelfo/collection.rb', line 38

def find_by(args)
  key = args.keys.first; value = args.values.first
  @list.select { |l| l.respond_to?(key) && l.public_send(key) == value }
end

#to_aObject



33
34
35
# File 'lib/efo_nelfo/collection.rb', line 33

def to_a
  map(&:to_a).flatten(1)
end