Class: WritersRoom::ElementCollection
- Inherits:
-
Object
- Object
- WritersRoom::ElementCollection
- Defined in:
- lib/writers_room/element_collection.rb
Overview
Manages all elements of a given type within a directory.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#element_type ⇒ Object
readonly
Returns the value of attribute element_type.
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #create(name, metadata: {}, body: "") ⇒ Object
- #empty? ⇒ Boolean
- #find_by_name_or_alias(query) ⇒ Object
- #find_by_slug(slug) ⇒ Object
-
#initialize(dir, element_type: nil) ⇒ ElementCollection
constructor
A new instance of ElementCollection.
- #list ⇒ Object
- #search(query) ⇒ Object
- #slugs ⇒ Object
Constructor Details
#initialize(dir, element_type: nil) ⇒ ElementCollection
Returns a new instance of ElementCollection.
8 9 10 11 |
# File 'lib/writers_room/element_collection.rb', line 8 def initialize(dir, element_type: nil) @dir = dir @element_type = element_type end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
6 7 8 |
# File 'lib/writers_room/element_collection.rb', line 6 def dir @dir end |
#element_type ⇒ Object (readonly)
Returns the value of attribute element_type.
6 7 8 |
# File 'lib/writers_room/element_collection.rb', line 6 def element_type @element_type end |
Instance Method Details
#all ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/writers_room/element_collection.rb', line 13 def all return [] unless Dir.exist?(@dir) Dir.glob(File.join(@dir, "*.md")).sort.reject { |p| File.basename(p) =~ /_v\d+\.md\z/ }.map do |path| Element.load(path) end end |
#count ⇒ Object
46 47 48 49 50 51 |
# File 'lib/writers_room/element_collection.rb', line 46 def count return 0 unless Dir.exist?(@dir) Dir.glob(File.join(@dir, "*.md")).reject { |p| File.basename(p) =~ /_v\d+\.md\z/ }.count end |
#create(name, metadata: {}, body: "") ⇒ Object
37 38 39 40 |
# File 'lib/writers_room/element_collection.rb', line 37 def create(name, metadata: {}, body: "") = .merge("element_type" => @element_type&.to_s) Element.create(@dir, name, metadata: , body: body) end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/writers_room/element_collection.rb', line 53 def empty? count.zero? end |
#find_by_name_or_alias(query) ⇒ Object
29 30 31 |
# File 'lib/writers_room/element_collection.rb', line 29 def find_by_name_or_alias(query) all.find { |el| el.matches_name?(query) } end |
#find_by_slug(slug) ⇒ Object
23 24 25 26 27 |
# File 'lib/writers_room/element_collection.rb', line 23 def find_by_slug(slug) path = File.join(@dir, "#{slug}.md") return nil unless File.exist?(path) Element.load(path) end |
#list ⇒ Object
42 43 44 |
# File 'lib/writers_room/element_collection.rb', line 42 def list all.map(&:to_h) end |
#search(query) ⇒ Object
33 34 35 |
# File 'lib/writers_room/element_collection.rb', line 33 def search(query) all.select { |el| el.matches_name?(query) } end |
#slugs ⇒ Object
57 58 59 |
# File 'lib/writers_room/element_collection.rb', line 57 def slugs all.map(&:slug) end |