Class: Cts::Mpx::Entries

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Creatable, Driver, Enumerable
Defined in:
lib/cts/mpx/entries.rb

Overview

Enumerable collection that can store an entry in it

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Driver

config_dir, gem_dir, parse_json

Constructor Details

#initializeVoid

Reset the entry array to a blank state



51
52
53
# File 'lib/cts/mpx/entries.rb', line 51

def initialize
  reset
end

Instance Attribute Details

#collection storage for the individual entry(storage) ⇒ Entry[]

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cts/mpx/entries.rb', line 6

class Entries
  include Enumerable
  include Driver
  include Creatable
  extend Forwardable

  attribute name: 'collection', kind_of: Array

  # Addressable method, indexed by entry object
  # @param [Entry] key the entry to return
  # @return [Self.collection,Entry,nil] Can return the collection, a single entry, or nil if nothing found
  def [](key = nil)
    return @collection unless key

    @collection.find { |e| e.id == key }
  end

  def +(other)
    Entries.create collection: @collection += other.collection
  end

  def -(other)
    Entries.create collection: @collection += other.collection
  end

  # Add an entry object to the collection
  # @param [Entry] entry instantiated Entry to include
  # @raise [ArgumentError] if entry is not an Entry
  # @return [Self]
  def add(entry)
    return self if @collection.include? entry

    Exceptions.raise_unless_argument_error? entry, Entry
    @collection.push entry
    self
  end

  # Iterator method for self
  # @return [Entry] next object in the list
  def each
    @collection.each { |c| yield c }
  end

  # Reset the entry array to a blank state
  # @return [Void]
  def initialize
    reset
  end

  # Remove a entry object from the collection
  # @param [Entry] argument instantiated Entry to remove
  # @return [Self]
  def remove(argument)
    @collection.delete_if { |f| f == argument }
  end

  # Reset the entry array to a blank state
  # @return [Void]
  def reset
    @collection = []
  end

  # A hash of all available entries
  # @return [Hash]
  def to_h
    output = { xmlns: {}, entries: [] }

    each do |entry|
      output[:entries].push entry.to_h[:entry]
      output[:xmlns].merge! entry.fields.xmlns
    end
    output
  end
end

Instance Method Details

#+(other) ⇒ Object



23
24
25
# File 'lib/cts/mpx/entries.rb', line 23

def +(other)
  Entries.create collection: @collection += other.collection
end

#-(other) ⇒ Object



27
28
29
# File 'lib/cts/mpx/entries.rb', line 27

def -(other)
  Entries.create collection: @collection += other.collection
end

#[](key = nil) ⇒ Self.collection, ...

Addressable method, indexed by entry object

Parameters:

  • key (Entry) (defaults to: nil)

    the entry to return

Returns:

  • (Self.collection, Entry, nil)

    Can return the collection, a single entry, or nil if nothing found



17
18
19
20
21
# File 'lib/cts/mpx/entries.rb', line 17

def [](key = nil)
  return @collection unless key

  @collection.find { |e| e.id == key }
end

#add(entry) ⇒ Self

Add an entry object to the collection

Parameters:

  • entry (Entry)

    instantiated Entry to include

Returns:

  • (Self)

Raises:

  • (ArgumentError)

    if entry is not an Entry



35
36
37
38
39
40
41
# File 'lib/cts/mpx/entries.rb', line 35

def add(entry)
  return self if @collection.include? entry

  Exceptions.raise_unless_argument_error? entry, Entry
  @collection.push entry
  self
end

#eachEntry

Iterator method for self

Returns:

  • (Entry)

    next object in the list



45
46
47
# File 'lib/cts/mpx/entries.rb', line 45

def each
  @collection.each { |c| yield c }
end

#remove(argument) ⇒ Self

Remove a entry object from the collection

Parameters:

  • argument (Entry)

    instantiated Entry to remove

Returns:

  • (Self)


58
59
60
# File 'lib/cts/mpx/entries.rb', line 58

def remove(argument)
  @collection.delete_if { |f| f == argument }
end

#resetVoid

Reset the entry array to a blank state

Returns:

  • (Void)


64
65
66
# File 'lib/cts/mpx/entries.rb', line 64

def reset
  @collection = []
end

#to_hHash

A hash of all available entries

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
# File 'lib/cts/mpx/entries.rb', line 70

def to_h
  output = { xmlns: {}, entries: [] }

  each do |entry|
    output[:entries].push entry.to_h[:entry]
    output[:xmlns].merge! entry.fields.xmlns
  end
  output
end