class Ruhoh::Collections
Public Class Methods
load(resource)
click to toggle source
Load the CollectionView class for this resource. Used primarily to as the plugin interface to include modules for mustache.
# File lib/ruhoh/collections.rb, line 46 def self.load(resource) get_module_namespace_for(resource).const_get(:CollectionView) end
new(ruhoh)
click to toggle source
# File lib/ruhoh/collections.rb, line 17 def initialize(ruhoh) @ruhoh = ruhoh @collections = {} end
registered()
click to toggle source
# File lib/ruhoh/collections.rb, line 66 def self.registered Ruhoh::Resources.constants.map{ |a| a.to_s.downcase }.delete_if{ |a| a == "pages" } end
Protected Class Methods
camelize(name)
click to toggle source
# File lib/ruhoh/collections.rb, line 174 def self.camelize(name) name.to_s.split('_').map {|a| a.capitalize}.join end
get_module_namespace_for(resource, type=nil)
click to toggle source
# File lib/ruhoh/collections.rb, line 148 def self.get_module_namespace_for(resource, type=nil) if type if registered.include?(type) Ruhoh::Resources.const_get(camelize(type)) elsif base.include?(type) Ruhoh::Base.const_get(camelize(type)) else klass = camelize(type) Friend.say { red "#{resource} resource set to use:'#{type}' in config but Ruhoh::Resources::#{klass} does not exist." } abort end else if registered.include?(resource) Ruhoh::Resources.const_get(camelize(resource)) else Ruhoh::Resources.const_get(:Pages) end end end
Public Instance Methods
acting_as_pages()
click to toggle source
# File lib/ruhoh/collections.rb, line 86 def acting_as_pages pool = discover theme = @ruhoh.config['theme']['name'] rescue nil pool.delete(theme) pool.keep_if { |resource| config = @ruhoh.config[resource] if (config && config["use"]) config["use"] == "pages" else if resource == "pages" true else !registered.include?(resource) end end } end
all()
click to toggle source
# File lib/ruhoh/collections.rb, line 50 def all (discover + registered).to_a end
base()
click to toggle source
# File lib/ruhoh/collections.rb, line 54 def base Ruhoh::Base.constants.select{ |a| Ruhoh::Base.const_get(a).class == Module }.map{ |a| a.to_s.downcase } end
collection(resource)
click to toggle source
# File lib/ruhoh/collections.rb, line 22 def collection(resource) get_module_namespace_for(resource).const_get(:Collection) end
collection?(resource)
click to toggle source
# File lib/ruhoh/collections.rb, line 26 def collection?(resource) get_module_namespace_for(resource).const_defined?(:Collection) end
discover()
click to toggle source
discover all the resource mappings @return
# File lib/ruhoh/collections.rb, line 72 def discover results = Set.new @ruhoh.cascade.paths.each do |h| FileUtils.cd(h["path"]) do results += Dir['*'].select { |x| File.directory?(x) && !["plugins", 'compiled'].include?(x) } end end results end
exists?(name)
click to toggle source
# File lib/ruhoh/collections.rb, line 105 def exists?(name) all.include?(name) end
Also aliased as: exist?
load(resource)
click to toggle source
Load and cache a given resource collection. This allows you to work with single object instance and perform persistant mutations on it if necessary. Note the collection is always wrapped in its view. @returns[Class Instance] of the resource and class_name given.
# File lib/ruhoh/collections.rb, line 35 def load(resource) return @collections[resource] if @collections[resource] instance = collection?(resource) ? collection(resource).new(@ruhoh) : Ruhoh::Base::Collection.new(@ruhoh) instance.resource_name = resource @collections[resource] = instance.load_collection_view end
paginator_urls()
click to toggle source
# File lib/ruhoh/collections.rb, line 110 def paginator_urls data = {} all.each do |name| paginator = load(name).config['paginator']['url'] rescue nil next if paginator.nil? || paginator.empty? data[name] = @ruhoh.to_url(paginator) end data end
registered()
click to toggle source
# File lib/ruhoh/collections.rb, line 62 def registered self.class.registered end
url_endpoints()
click to toggle source
# File lib/ruhoh/collections.rb, line 122 def url_endpoints urls = {} all.each do |name| collection = load(name) next unless collection.respond_to?(:url_endpoint) urls[name] = @ruhoh.to_url(collection.url_endpoint) end urls end
url_endpoints_sorted()
click to toggle source
# File lib/ruhoh/collections.rb, line 133 def url_endpoints_sorted sorted_urls = url_endpoints.each.map {|k, v| {"name" => k, "url" => v} } sorted_urls.sort { |a, b| b["url"].length <=> a["url"].length } end
Protected Instance Methods
camelize(name)
click to toggle source
# File lib/ruhoh/collections.rb, line 170 def camelize(name) self.class.camelize(name) end
get_module_namespace_for(resource)
click to toggle source
Load the registered resource else default to Pages if not configured. @returns the resource’s module namespace
# File lib/ruhoh/collections.rb, line 142 def get_module_namespace_for(resource) type = @ruhoh.config[resource]["use"] rescue nil self.class.get_module_namespace_for(resource, type) end