Class: MiddlemanMdocs::Controller

Inherits:
Object
  • Object
show all
Includes:
TOC
Defined in:
lib/middleman-mdocs/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TOC

#table_of_contents

Constructor Details

#initialize(extension) ⇒ Controller

Returns a new instance of Controller.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/middleman-mdocs/controller.rb', line 7

def initialize(extension)
  @extension = extension

  @app = extension.app
  @sitemap = app.sitemap

  @guards = {}
  @dependencies = {}
  @metadata = {}
  @updated = SecureRandom.hex
  @resources = {}
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/middleman-mdocs/controller.rb', line 3

def app
  @app
end

#extensionObject (readonly)

Returns the value of attribute extension.



3
4
5
# File 'lib/middleman-mdocs/controller.rb', line 3

def extension
  @extension
end

Returns the value of attribute nav.



3
4
5
# File 'lib/middleman-mdocs/controller.rb', line 3

def nav
  @nav
end

#readyObject (readonly)

Returns the value of attribute ready.



3
4
5
# File 'lib/middleman-mdocs/controller.rb', line 3

def ready
  @ready
end

#sitemapObject (readonly)

Returns the value of attribute sitemap.



3
4
5
# File 'lib/middleman-mdocs/controller.rb', line 3

def sitemap
  @sitemap
end

Class Method Details

.tr(tag, scope: :tags) ⇒ Object



151
152
153
154
# File 'lib/middleman-mdocs/controller.rb', line 151

def self.tr(tag, scope: :tags)
  I18n.t(tag.to_s.upcase.presence, scope: [:mdocs, scope],
                                   default: I18n.t(tag.to_s.downcase.presence, scope: [:mdocs, scope], default: tag.to_s).presence)
end

Instance Method Details

#add_dependency(resource, child) ⇒ Object



45
46
47
48
# File 'lib/middleman-mdocs/controller.rb', line 45

def add_dependency(resource, child)
  @dependencies[resource.page_id] ||= {}
  @dependencies[resource.page_id][child.page_id] = child
end

#dependencies(resource) ⇒ Object



50
51
52
# File 'lib/middleman-mdocs/controller.rb', line 50

def dependencies(resource)
  @dependencies[resource.page_id]&.values || []
end

#has_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/middleman-mdocs/controller.rb', line 86

def has_tag?(tag)
  tags.include?(tag.to_s)
end

#init_resource(resource) ⇒ Object



68
69
70
71
72
# File 'lib/middleman-mdocs/controller.rb', line 68

def init_resource(resource)
  resource.extend(::MiddlemanMdocs::Resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
  resource.extension = extension
  resource.mdocs = self
end

#metadata(resource, key, default) ⇒ Object



54
55
56
57
# File 'lib/middleman-mdocs/controller.rb', line 54

def (resource, key, default)
  @metadata[resource.page_id] ||= {}
  @metadata[resource.page_id][key] ||= default
end

#page_idObject



64
65
66
# File 'lib/middleman-mdocs/controller.rb', line 64

def page_id
  self.class.to_s
end

#ready?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/middleman-mdocs/controller.rb', line 41

def ready?
  @ready
end

#refreshObject



30
31
32
33
# File 'lib/middleman-mdocs/controller.rb', line 30

def refresh
  @updated = SecureRandom.hex
  @ready = true
end

#register(resource) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/middleman-mdocs/controller.rb', line 20

def register(resource)
  if (old = @resources[resource.page_id])
    resource.copy_from(old)
  else
    resource.copy_from(resource)
  end

  @resources[resource.page_id] = resource
end

#select(tags, *meta) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/middleman-mdocs/controller.rb', line 130

def select(tags, *meta)
  tags = [tags].flatten.compact.uniq

  select_by_meta(*meta).select do |r|
    r.has_tags?(*tags)
  end
end

#select_by_meta(meta) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/middleman-mdocs/controller.rb', line 90

def select_by_meta(meta)
  with_ensure_resource do
    sitemap.resources.select do |r|
      next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore] && r.template?

      if meta.is_a? Hash
        r.meta_match?(meta)
      else
        r.has_meta?(*meta)
      end
    end
  end
end

#select_by_meta_force(meta) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/middleman-mdocs/controller.rb', line 104

def select_by_meta_force(meta)
  with_ensure_resource do
    sitemap.resources.select do |r|
      next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore]

      if meta.is_a? Hash
        r.meta_match?(meta)
      else
        r.has_meta?(*meta)
      end
    end
  end
end

#select_by_tags(*tags) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/middleman-mdocs/controller.rb', line 118

def select_by_tags(*tags)
  tags = [tags].flatten.compact.uniq

  with_ensure_resource do
    sitemap.resources.select do |r|
      next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore] && r.template?

      r.has_tags?(*tags)
    end
  end
end

#set_metadata(resource, key, data) ⇒ Object



59
60
61
62
# File 'lib/middleman-mdocs/controller.rb', line 59

def (resource, key, data)
  @metadata[resource.page_id] ||= {}
  @metadata[resource.page_id][key] = data
end

#tagsObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/middleman-mdocs/controller.rb', line 74

def tags
  with_cache(:tags, dependencies(self).map(&:timestamp).max) do
    sitemap.resources.select do |r|
      r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore] && r.html?
    end.map do |r|
      add_dependency(self, r)
      # r.options[:tags]
      r.tags
    end.flatten.compact.map(&:to_s).uniq.sort
  end
end

#tr(tag, scope: :tags) ⇒ Object



147
148
149
# File 'lib/middleman-mdocs/controller.rb', line 147

def tr(tag, scope: :tags)
  ::MiddlemanMdocs::Controller.tr(tag, scope: scope)
end

#with_ensure_resourceObject



138
139
140
141
142
143
144
145
# File 'lib/middleman-mdocs/controller.rb', line 138

def with_ensure_resource
  guard_recursive('with_ensure_resource', 'none', nil) do
    sitemap.instance_variable_get('@lock').synchronize do
      sitemap.ensure_resource_list_updated!
      yield
    end
  end
end