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.



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

def initialize(env)
  @env = env
end

Class Attribute Details

.__export_value__Object (readonly)

Returns the value of attribute export_value.



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

def __export_value__
  @__export_value__
end

Class Method Details

.app(location = nil, mount_path = nil) ⇒ Object



124
125
126
127
128
129
# File 'lib/syntropy/module.rb', line 124

def app(location = nil, mount_path = nil)
  location ||= @env[:location]
  mount_path ||= @env[:mount_path]
  opts = @env.merge(location:, mount_path:)
  Syntropy::App.new(opts[:machine], opts[:location], opts[:mount_path], opts)
end

.export(ref) ⇒ Object



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

def export(ref)
  @__export_value__ = ref
end

.import(ref) ⇒ Object



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

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

.page_list(ref) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/syntropy/module.rb', line 114

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:, ref:) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/syntropy/module.rb', line 67

def prepare(loader:, env:, ref:)
  @loader = loader
  @env = env
  @machine = env[:machine]
  @ref = ref
  const_set(:MODULE, self)
end

.route_by_host(map = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/syntropy/module.rb', line 93

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(proc = nil, &block) ⇒ Object Also known as: html



85
86
87
88
89
90
# File 'lib/syntropy/module.rb', line 85

def template(proc = nil, &block)
  proc ||= block
  raise "No template block/proc given" if !proc
  
  P2::Template.new(proc)
end