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
87
# 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)

  @metadata = if .exist?
    YAML.safe_load(.read).to_h
  else
    {}
  end

  content_path = path.join.glob("_content.*")[0]

  if content_path&.exist?
    @content = Content.load(content_path, database: self, metadata: @metadata, namespace: @namespace, &block)
    @metadata = @content..dup
  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: @metadata, namespace: @namespace, &block)

      includes[content.slug.to_s[1..].to_sym] = content
    else
      content = Content.load(path, database: self, metadata: @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



166
167
168
# File 'lib/contentfs/database.rb', line 166

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 
  @metadata
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



89
90
91
92
93
94
95
# File 'lib/contentfs/database.rb', line 89

def content
  return to_enum(:content) unless block_given?

  @children.each_value do |value|
    yield value
  end
end

#filter(**filters) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/contentfs/database.rb', line 105

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



119
120
121
122
123
124
125
126
127
# File 'lib/contentfs/database.rb', line 119

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



129
130
131
# File 'lib/contentfs/database.rb', line 129

def find_include(path)
  @includes[path.to_sym] || find_child_include(path) || find_parent_include(path) || find_include_from_toplevel(path)
end

#nestedObject



97
98
99
100
101
102
103
# File 'lib/contentfs/database.rb', line 97

def nested
  return to_enum(:nested) unless block_given?

  @nested.each_value do |value|
    yield value
  end
end

#renderObject



162
163
164
# File 'lib/contentfs/database.rb', line 162

def render
  @content&.render
end

#respond_to_missing?(name) ⇒ Boolean



170
171
172
# File 'lib/contentfs/database.rb', line 170

def respond_to_missing?(name, *)
  @children.key?(name) || super
end

#to_sObject



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

def to_s
  @content&.to_s.to_s
end

#toplevelObject



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

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