class EfoNelfo::Collection

Attributes

list[R]
owner[R]
post_type[R]

Public Class Methods

new(owner, post_type) click to toggle source
# File lib/efo_nelfo/collection.rb, line 13
def initialize(owner, post_type)
  @owner     = owner
  @post_type = post_type
  @list      = []
end

Public Instance Methods

<<(obj) click to toggle source
# 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) click to toggle source
# File lib/efo_nelfo/collection.rb, line 29
def delete(index)
  @list.delete_at index
end
find_by(args) click to toggle source

find_by property_name: 'test'

# 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_a() click to toggle source
# File lib/efo_nelfo/collection.rb, line 33
def to_a
  map(&:to_a).flatten(1)
end

Private Instance Methods

post_type_class() click to toggle source
# File lib/efo_nelfo/collection.rb, line 46
def post_type_class
  Kernel.const_get("EfoNelfo::V#{owner.class.version_from_class}::#{post_type}")
end