Class: Waves::Foundations::REST::Application
- Defined in:
- lib/waves/foundations/rest.rb
Overview
Applications are formal, rather than ad-hoc Waves apps.
Class Attribute Summary collapse
-
.loading ⇒ Object
readonly
File that this Application is currently loading if any.
-
.resources ⇒ Object
readonly
Resources this application is composed of.
Class Method Summary collapse
-
.at(mountpoint, path) ⇒ Object
Associate mountpoint with a file path for resource.
-
.composed_of(&block) ⇒ Object
Resource composition block.
-
.layouts_for(*types) ⇒ Object
Declare and load layout rendering support.
-
.load(path) ⇒ Object
Override normal loading to access file being loaded.
-
.look_in(*prefixes) ⇒ Object
Path prefixes to look for resource files under.
-
.register(resource) ⇒ Object
Allow resource to register itself when loaded.
-
.url_for(resource, pathspec) ⇒ Object
Construct and possibly override URL for a resource.
Class Attribute Details
.loading ⇒ Object (readonly)
File that this Application is currently loading if any.
23 24 25 |
# File 'lib/waves/foundations/rest.rb', line 23 def loading @loading end |
.resources ⇒ Object (readonly)
Resources this application is composed of.
26 27 28 |
# File 'lib/waves/foundations/rest.rb', line 26 def resources @resources end |
Class Method Details
.at(mountpoint, path) ⇒ Object
Associate mountpoint with a file path for resource.
Path is stored expanded to absolute for matching. You can leave the .rb out if you really like.
37 38 39 |
# File 'lib/waves/foundations/rest.rb', line 37 def self.at(mountpoint, path) @composition << [path, mountpoint] end |
.composed_of(&block) ⇒ Object
Resource composition block.
In this block, the Application defines all of the resources it is composed of (and their prefixes or “mountpoints.”) The resources themselves are not defined here.
The order of composition is stored and used.
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 88 89 90 91 92 93 |
# File 'lib/waves/foundations/rest.rb', line 53 def self.composed_of(&block) @composition ||= [] instance_eval &block mounts = const_set :Mounts, Class.new(Waves::Resources::Base) # Only construct the Hash here to retain order for .on()s @resources ||= {} @composition.each {|path, mountpoint| path = path.to_s.snake_case if Symbol === path path << ".rb" unless path =~ /\.rb$/ found = if @look_in @look_in.find {|prefix| candidate = File. File.join(prefix, path) break candidate if File.exist? candidate } else path = File. path path if File.exist?(path) end raise ArgumentError, "Path #{path} does not exist!" unless found # Resource will register itself when loaded @resources[found] = OpenStruct.new :mountpoint => mountpoint, :actual => nil # This, ladies and gentlemen, is evil. mounts.on(true, mountpoint) { res = Waves.main.load(found) # TODO: This must be deterministically inserted as # a replacement of the old one. mounts.on(true, mountpoint) { to res } to res } } end |
.layouts_for(*types) ⇒ Object
Declare and load layout rendering support.
For each MIME type given, / are treated as directory separators and + are converted to spaces (just in case.)
Pattern for constant conversion is replacing any W
- with
-
and capitalising all resulting names.
TODO: Provide a way to give some root to load from.
Just as last parameter being hash probably OK. --rue
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/waves/foundations/rest.rb', line 106 def self.layouts_for(*types) @layouts ||= {} const_set :Layouts, Module.new unless const_defined? :Layouts basedir = if Hash === types.last types.pop[:in] else File.join Dir.pwd, "layouts" end types.each {|t| require File.(File.join(basedir, *t.split("/")) + ".rb") @layouts[t] = t.split(/\W+/).inject(const_get :Layouts) {|mod, name| mod.const_get name.capitalize } } end |
.load(path) ⇒ Object
Override normal loading to access file being loaded.
Used by the first-load hook, see .composed_of. Returns the newly loaded resource.
130 131 132 133 134 135 136 137 138 |
# File 'lib/waves/foundations/rest.rb', line 130 def self.load(path) @loading = path Kernel.load path @resources[path].actual ensure @loading = nil end |
.look_in(*prefixes) ⇒ Object
Path prefixes to look for resource files under.
Optional trailing /, one or more needed. Each is checked in the order given.
145 146 147 |
# File 'lib/waves/foundations/rest.rb', line 145 def self.look_in(*prefixes) @look_in = prefixes end |
.register(resource) ⇒ Object
Is there any point trying to add better failure if the path is unknown? Probably not. –rue
Allow resource to register itself when loaded.
The path-indexed entry is completed with the actual resource and a mirror version is created, indexed by the resource itself.
157 158 159 160 161 162 163 164 |
# File 'lib/waves/foundations/rest.rb', line 157 def self.register(resource) entry = @resources[@loading] entry.actual = resource # Mirror @resources[resource] = OpenStruct.new :mountpoint => entry.mountpoint, :path => @loading end |
.url_for(resource, pathspec) ⇒ Object
This may be obsolete, move to registration? –rue
Construct and possibly override URL for a resource.
170 171 172 173 |
# File 'lib/waves/foundations/rest.rb', line 170 def self.url_for(resource, pathspec) info = Waves.main.resources[resource] info.mountpoint + pathspec end |