Class: Syntropy::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/syntropy/module.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Module

Returns a new instance of Module.



61
62
63
# File 'lib/syntropy/module.rb', line 61

def initialize(env)
  @env = env
end

Class Attribute Details

.__export_value__Object (readonly)

Returns the value of attribute export_value.



72
73
74
# File 'lib/syntropy/module.rb', line 72

def __export_value__
  @__export_value__
end

Class Method Details

.export(ref) ⇒ Object



78
79
80
# File 'lib/syntropy/module.rb', line 78

def export(ref)
  @__export_value__ = ref
end

.import(ref) ⇒ Object



74
75
76
# File 'lib/syntropy/module.rb', line 74

def import(ref)
  @loader.load(ref)
end

.page_list(ref) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/syntropy/module.rb', line 107

def page_list(ref)
  full_path = File.join(@env[:location], ref)
  raise 'Not a directory' if !File.directory?(full_path)

  Dir[File.join(full_path, '*.md')].sort.map {
    atts, markdown = Syntropy.parse_markdown_file(it, @env)
    { atts:, markdown: }
  }
end

.prepare(loader:, env:) ⇒ Object



66
67
68
69
70
# File 'lib/syntropy/module.rb', line 66

def prepare(loader:, env:)
  @loader = loader
  @env = env
  const_set(:MODULE, self)
end

.route_by_host(map = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/syntropy/module.rb', line 86

def route_by_host(map = nil)
  root = @env[:location]
  sites = Dir[File.join(root, '*')]
          .reject { File.basename(it) =~ /^_/ }
          .select { File.directory?(it) }
          .each_with_object({}) { |fn, h|
    name = File.basename(fn)
    opts = @env.merge(location: fn)
    h[name] = Syntropy::App.new(opts[:machine], opts[:location], opts[:mount_path], opts)
  }

  map&.each do |k, v|
    sites[k] = sites[v]
  end

  lambda { |req|
    site = sites[req.host]
    site ? site.call(req) : req.respond(nil, ':status' => Status::BAD_REQUEST)
  }
end

.template(&block) ⇒ Object



82
83
84
# File 'lib/syntropy/module.rb', line 82

def template(&block)
  Papercraft.html(&block)
end