Class: ForgetPasswords::Template::Mapper
- Inherits:
-
Object
- Object
- ForgetPasswords::Template::Mapper
- Defined in:
- lib/forget-passwords/template.rb
Overview
XXX do we even need this?
Constant Summary collapse
- Type =
ForgetPasswords::Types.Constructor(self) do |input| # what we're gonna do is validate the input as a hash, then use it input = RawParams.(input) path = input.delete :path self.new(path, **input) end
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
Instance Method Summary collapse
-
#[](key, root = nil) ⇒ nil, ForgetPasswords::Template
Fetch the appropriate template, optionally relative to a given root.
- #[]=(key, path) ⇒ Object
-
#initialize(path = DEFAULT_PATH, base: nil, transform: nil, mapping: {}) ⇒ Mapper
constructor
A new instance of Mapper.
- #manifest ⇒ Object
-
#verify(*names) ⇒ true, false
Ensure that the mapper contains templates with the given names.
-
#verify!(*names) ⇒ true, false
Ensure that the mapper contains templates with the given names and raise an exception if it doesn’t.
Constructor Details
#initialize(path = DEFAULT_PATH, base: nil, transform: nil, mapping: {}) ⇒ Mapper
Returns a new instance of Mapper.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/forget-passwords/template.rb', line 58 def initialize path = DEFAULT_PATH, base: nil, transform: nil, mapping: {} @path = Pathname(path). @base = base @transform = transform @mapping = mapping @templates = { @path => mapping.map do |k, v| name = normalize k template = v.is_a?(ForgetPasswords::Template) ? v : ForgetPasswords::Template.new(self, k, @path + v) [name, template] end.to_h } end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
56 57 58 |
# File 'lib/forget-passwords/template.rb', line 56 def base @base end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
56 57 58 |
# File 'lib/forget-passwords/template.rb', line 56 def path @path end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
56 57 58 |
# File 'lib/forget-passwords/template.rb', line 56 def transform @transform end |
Instance Method Details
#[](key, root = nil) ⇒ nil, ForgetPasswords::Template
Fetch the appropriate template, optionally relative to a given root.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/forget-passwords/template.rb', line 81 def [] key, root = nil key = normalize key # bail early if we don't know the key return unless @mapping[key] # obtain optional root root = if root r = root.respond_to?(:env) ? root.env['DOCUMENT_ROOT'] : root r = (Pathname(r) + DEFAULT_DOCROOT). r.readable? ? r : nil end if root # get the full file path fp = root + @mapping[key] if fp.readable? mt = fp.mtime rootmap = @templates[root] ||= {} template = rootmap[key] # congratulations, you found it return template if template and mt <= template.modified # XXX this could explode obvs begin template = ForgetPasswords::Template.new(self, key, fp, mt) rootmap[key] = template return template rescue # XXX duhh what do we do here nil end end end # otherwise just return the default @templates[@path][key] end |
#[]=(key, path) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/forget-passwords/template.rb', line 120 def []= key, path name = normalize key # XXX do something less dumb here @templates[@path][name] = path.is_a?(ForgetPasswords::Template) ? path : ForgetPasswords::Template.new(self, key, @path + path) end |
#manifest ⇒ Object
127 128 129 |
# File 'lib/forget-passwords/template.rb', line 127 def manifest @mapping.keys end |
#verify(*names) ⇒ true, false
Ensure that the mapper contains templates with the given names.
137 138 139 140 141 |
# File 'lib/forget-passwords/template.rb', line 137 def verify *names names = names.first if names.first.is_a? Array # i dunno, is there a better way to do this? (names.map { |k| normalize k } - manifest).empty? end |
#verify!(*names) ⇒ true, false
Ensure that the mapper contains templates with the given names and raise an exception if it doesn’t.
150 151 152 |
# File 'lib/forget-passwords/template.rb', line 150 def verify! *names verify(*names) or raise "Could not verify names: #{names.join ?,}" end |