Class: ContentFS::Database

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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()

   = 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

#metadataObject (readonly)

Returns the value of attribute metadata.



21
22
23
# File 'lib/contentfs/database.rb', line 21

def 
  
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



21
22
23
# File 'lib/contentfs/database.rb', line 21

def namespace
  @namespace
end

#prefixObject (readonly)

Returns the value of attribute prefix.



21
22
23
# File 'lib/contentfs/database.rb', line 21

def prefix
  @prefix
end

#slugObject (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

#contentObject



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

#nestedObject



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

#renderObject



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_sObject



157
158
159
# File 'lib/contentfs/database.rb', line 157

def to_s
  @content&.to_s.to_s
end

#toplevelObject



132
133
134
# File 'lib/contentfs/database.rb', line 132

def toplevel
  @parent ? @parent.toplevel : self
end