Class: ContentFS::Database
- Inherits:
-
Object
- Object
- ContentFS::Database
- Defined in:
- lib/contentfs/database.rb
Overview
Structured content database, loaded from the filesystem.
Constant Summary collapse
- METADATA_FILE =
"_metadata.yml"
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
- #content ⇒ Object
- #filter(**filters) ⇒ Object
- #find(name, *nested) ⇒ Object
- #find_include(path) ⇒ Object
-
#initialize(path:, parent: nil, namespace: [], root: false, &block) ⇒ Database
constructor
A new instance of Database.
- #method_missing(name, *nested) ⇒ Object
- #nested ⇒ Object
- #render ⇒ Object
- #respond_to_missing?(name) ⇒ Boolean
- #to_s ⇒ Object
- #toplevel ⇒ Object
Constructor Details
#initialize(path:, parent: nil, namespace: [], root: false, &block) ⇒ Database
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 80 81 82 83 84 85 86 |
# File 'lib/contentfs/database.rb', line 23 def initialize(path:, parent: nil, namespace: [], root: false, &block) path = Pathname.new(path) name = path.basename(path.extname) prefix, remainder = Prefix.build(name) @prefix = prefix @namespace = namespace.dup @parent = parent unless root @slug = Slug.build(remainder) @namespace << @slug end = path.join(METADATA_FILE) = if .exist? YAML.safe_load(.read).to_h else {} end content_path = path.join.glob("_content.*")[0] @content = if content_path&.exist? Content.load(content_path, database: self, metadata: , namespace: @namespace, &block) end children, nested, includes = {}, {}, {} Pathname.new(path).glob("*") do |path| underscored = path.basename.to_s.start_with?("_") next if underscored && path.directory? if path.directory? database = Database.load(path, parent: self, namespace: @namespace, root: false, &block) nested[database.slug] = database elsif underscored content = Content.load(path, database: self, metadata: , namespace: @namespace, &block) includes[content.slug.to_s[1..].to_sym] = content else content = Content.load(path, database: self, metadata: , namespace: @namespace, &block) children[content.slug] = content end end @children = Hash[ children.sort_by { |key, content| (content.prefix || content.slug).to_s } ] @nested = Hash[ nested.sort_by { |key, database| (database.prefix || database.slug).to_s } ] @includes = Hash[ includes.sort_by { |key, content| (content.prefix || content.slug).to_s } ] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *nested) ⇒ Object
165 166 167 |
# File 'lib/contentfs/database.rb', line 165 def method_missing(name, *nested, **) find(name, *nested) || super end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
21 22 23 |
# File 'lib/contentfs/database.rb', line 21 def end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/contentfs/database.rb', line 21 def namespace @namespace end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
21 22 23 |
# File 'lib/contentfs/database.rb', line 21 def prefix @prefix end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
21 22 23 |
# File 'lib/contentfs/database.rb', line 21 def slug @slug end |
Class Method Details
.load(path, parent: nil, namespace: [], root: true, &block) ⇒ Object
14 15 16 |
# File 'lib/contentfs/database.rb', line 14 def load(path, parent: nil, namespace: [], root: true, &block) new(path: path, parent: parent, namespace: namespace, root: root, &block) end |
Instance Method Details
#content ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/contentfs/database.rb', line 88 def content return to_enum(:content) unless block_given? @children.each_value do |value| yield value end end |
#filter(**filters) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/contentfs/database.rb', line 104 def filter(**filters) return to_enum(:filter, **filters) unless block_given? filters = filters.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value } @children.each_value.select { |content| yield content if content..all? { |key, value| filters[key] == value } } end |
#find(name, *nested) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/contentfs/database.rb', line 118 def find(name, *nested) if @children.key?(name) @children[name] elsif @nested.key?(name) nested.inject(@nested[name]) { |database, next_nested| database.find(next_nested.to_sym) } end end |
#find_include(path) ⇒ Object
128 129 130 |
# File 'lib/contentfs/database.rb', line 128 def find_include(path) @includes[path.to_sym] || find_child_include(path) || find_parent_include(path) || find_include_from_toplevel(path) end |
#nested ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/contentfs/database.rb', line 96 def nested return to_enum(:nested) unless block_given? @nested.each_value do |value| yield value end end |
#render ⇒ Object
161 162 163 |
# File 'lib/contentfs/database.rb', line 161 def render @content&.render end |
#respond_to_missing?(name) ⇒ Boolean
169 170 171 |
# File 'lib/contentfs/database.rb', line 169 def respond_to_missing?(name, *) @children.key?(name) || super end |
#to_s ⇒ Object
157 158 159 |
# File 'lib/contentfs/database.rb', line 157 def to_s @content&.to_s.to_s end |
#toplevel ⇒ Object
132 133 134 |
# File 'lib/contentfs/database.rb', line 132 def toplevel @parent ? @parent.toplevel : self end |