Class: MiddlemanMdocs::Controller
- Inherits:
-
Object
- Object
- MiddlemanMdocs::Controller
- Includes:
- TOC
- Defined in:
- lib/middleman-mdocs/controller.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#nav ⇒ Object
readonly
Returns the value of attribute nav.
-
#ready ⇒ Object
readonly
Returns the value of attribute ready.
-
#sitemap ⇒ Object
readonly
Returns the value of attribute sitemap.
Class Method Summary collapse
Instance Method Summary collapse
- #add_dependency(resource, child) ⇒ Object
- #dependencies(resource) ⇒ Object
- #has_tag?(tag) ⇒ Boolean
- #init_resource(resource) ⇒ Object
-
#initialize(extension) ⇒ Controller
constructor
A new instance of Controller.
- #metadata(resource, key, default) ⇒ Object
- #page_id ⇒ Object
- #ready? ⇒ Boolean
- #refresh ⇒ Object
- #register(resource) ⇒ Object
- #select(tags, *meta) ⇒ Object
- #select_by_meta(meta) ⇒ Object
- #select_by_meta_force(meta) ⇒ Object
- #select_by_tags(*tags) ⇒ Object
- #set_metadata(resource, key, data) ⇒ Object
- #tags ⇒ Object
- #tr(tag, scope: :tags) ⇒ Object
- #with_ensure_resource ⇒ Object
Methods included from TOC
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
#app ⇒ Object (readonly)
Returns the value of attribute app.
3 4 5 |
# File 'lib/middleman-mdocs/controller.rb', line 3 def app @app end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
3 4 5 |
# File 'lib/middleman-mdocs/controller.rb', line 3 def extension @extension end |
#nav ⇒ Object (readonly)
Returns the value of attribute nav.
3 4 5 |
# File 'lib/middleman-mdocs/controller.rb', line 3 def nav @nav end |
#ready ⇒ Object (readonly)
Returns the value of attribute ready.
3 4 5 |
# File 'lib/middleman-mdocs/controller.rb', line 3 def ready @ready end |
#sitemap ⇒ Object (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
86 87 88 |
# File 'lib/middleman-mdocs/controller.rb', line 86 def has_tag?(tag) .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_id ⇒ Object
64 65 66 |
# File 'lib/middleman-mdocs/controller.rb', line 64 def page_id self.class.to_s end |
#ready? ⇒ Boolean
41 42 43 |
# File 'lib/middleman-mdocs/controller.rb', line 41 def ready? @ready end |
#refresh ⇒ Object
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(, *) = [].flatten.compact.uniq (*).select do |r| r.(*) 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 () with_ensure_resource do sitemap.resources.select do |r| next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore] && r.template? if .is_a? Hash r.() else r.(*) 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 () with_ensure_resource do sitemap.resources.select do |r| next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.[:ignore] if .is_a? Hash r.() else r.(*) 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 (*) = [].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.(*) 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 |
#tags ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/middleman-mdocs/controller.rb', line 74 def 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. 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_resource ⇒ Object
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 |