Class: Brainstem::ApiDocs::AbstractCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Concerns::Formattable, Concerns::Optional, Enumerable
Defined in:
lib/brainstem/api_docs/abstract_collection.rb

Instance Attribute Summary collapse

Attributes included from Concerns::Formattable

#formatters

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Formattable

#formatted_as, #formatter_type, #valid_options

Methods included from Concerns::Optional

#valid_options

Constructor Details

#initialize(atlas, options = {}) ⇒ AbstractCollection

Returns a new instance of AbstractCollection.



22
23
24
25
26
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 22

def initialize(atlas, options = {})
  self.atlas   = atlas
  self.members = []
  super options
end

Instance Attribute Details

#atlasObject

Returns the value of attribute atlas.



28
29
30
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 28

def atlas
  @atlas
end

Class Method Details

.with_members(atlas, *members) ⇒ Object

Creates a new collection with all passed members. Very handy for reduce operations which should return a subset of members but retain the same utility.



18
19
20
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 18

def self.with_members(atlas, *members)
  new(atlas).tap {|n| members.flatten.each { |m| n << m } }
end

Instance Method Details

#<<(*objects) ⇒ Object

Appends a pre-existing object to the collection.



42
43
44
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 42

def <<(*objects)
  members.push(*objects.flatten)
end

#each(&block) ⇒ Object

Iterates over each controller in the collection.



49
50
51
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 49

def each(&block)
  members.each(&block)
end

#each_filename(format, &block) ⇒ Object



92
93
94
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 92

def each_filename(format, &block)
  filenames(format).each { |args| block.call(*args) }
end

#each_formatted(format, options = {}, &block) ⇒ Object



87
88
89
90
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 87

def each_formatted(format, options = {}, &block)
  formatted(format, options)
    .each { |args| block.call(*args) }
end

#each_formatted_with_filename(format, options = {}, &block) ⇒ Object



82
83
84
85
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 82

def each_formatted_with_filename(format, options = {}, &block)
  formatted_with_filename(format, options)
    .each { |args| block.call(*args) }
end

#filenames(format) ⇒ Object

Returns a list of each member’s filename.

We internally refer to ‘formatted_with_filename` here because we don’t want to include any filenames of empty files (i.e. nodoc).



67
68
69
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 67

def filenames(format)
  formatted_with_filename(format).map { |arr| arr[1] }
end

#formatted(format, options = {}) ⇒ Object

Returns a map of each member formatted as specified.



56
57
58
59
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 56

def formatted(format, options = {})
  map { |member| member.formatted_as(format, options) }
    .reject(&:empty?)
end

#formatted_with_filename(format, options = {}) ⇒ Object

Returns a map of each formatted member and its suggested filename.



74
75
76
77
78
79
80
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 74

def formatted_with_filename(format, options = {})
  map { |member| [
    member.formatted_as(format, options),
    member.suggested_filename(format)
  ] }
    .reject { |(buffer, _)| buffer.empty? }
end

#lastObject

Handy accessor for extracting the last member of the collection.



35
36
37
# File 'lib/brainstem/api_docs/abstract_collection.rb', line 35

def last
  members[-1]
end