Class: ForgetPasswords::Template::Mapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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).expand_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

#baseObject (readonly)

Returns the value of attribute base.



56
57
58
# File 'lib/forget-passwords/template.rb', line 56

def base
  @base
end

#pathObject (readonly)

Returns the value of attribute path.



56
57
58
# File 'lib/forget-passwords/template.rb', line 56

def path
  @path
end

#transformObject (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.

Parameters:

  • key (Symbol, #to_sym)

    the template key.

  • root (nil, String) (defaults to: nil)

    an optional document root.

Returns:



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).expand_path
           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

#manifestObject



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.

Parameters:

  • *names (Array<#to_s, #to_sym>)

    the template names

Returns:

  • (true, false)


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.

Parameters:

  • *names (Array<#to_sym>)

    the template names

Returns:

  • (true, false)


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