Class: Benoit::SiteContext::ContextObject

Inherits:
Cadenza::ContextObject
  • Object
show all
Defined in:
lib/benoit/site_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ ContextObject

Returns a new instance of ContextObject.



17
18
19
20
# File 'lib/benoit/site_context.rb', line 17

def initialize(site)
  @site = site
  @pages = site.pages
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args) ⇒ Object Also known as: missing_context_method



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/benoit/site_context.rb', line 50

def method_missing(msg,*args)
  name = msg.to_s
  parsed_name = Inflecto.singularize(name).sub(/paginated_/, "")

  valid_page = @pages.any? { |page| page.has_value?(parsed_name) }

  if valid_page and name.start_with? "paginated_"
    return paginated_collection(parsed_name)
  elsif valid_page
    return resource_collection(name)
  else
    # The key we are looking for has no content,
    # therefore we can return an empty array
    []
  end

end

Instance Method Details

#lookup_resource_collection(name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/benoit/site_context.rb', line 22

def lookup_resource_collection(name)
  singular_name = Inflecto.singularize(name)
  collection = @pages.select do |page|
    page["_type"] == singular_name
  end

  collection.map!(&:to_hash)
end

#paginated_collection(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/benoit/site_context.rb', line 31

def paginated_collection(name)
  potential_collection = @site.paginated_collections[name].last
  if potential_collection.respond_to? :at_end?
    potential_collection.rewind_list! if potential_collection.at_end?
    return potential_collection
  end

  per_page = @site.paginated_collections[name].first
  collection = lookup_resource_collection(name)

  collection.paginate(per_page).tap do |coll|
    @site.paginated_collections[name] << coll
  end
end

#resource_collection(name) ⇒ Object



46
47
48
# File 'lib/benoit/site_context.rb', line 46

def resource_collection(name)
  lookup_resource_collection(name)
end