Class: Orthor

Inherits:
Object
  • Object
show all
Defined in:
lib/orthor.rb,
lib/orthor/templates.rb

Defined Under Namespace

Classes: Templates

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cacheObject

Returns the value of attribute cache.



13
14
15
# File 'lib/orthor.rb', line 13

def cache
  @cache
end

.cache_expiryObject

Returns the value of attribute cache_expiry.



13
14
15
# File 'lib/orthor.rb', line 13

def cache_expiry
  @cache_expiry
end

.site_idObject

Returns the value of attribute site_id.



13
14
15
# File 'lib/orthor.rb', line 13

def site_id
  @site_id
end

Class Method Details

.caching(file, expiry = nil, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/orthor.rb', line 55

def self.caching(file, expiry = nil, options = {})
  expiry, options = nil, expiry if expiry.is_a?(Hash)

  klass = file.to_s.split('_').collect { |e| e.capitalize }.join
  Moneta.autoload(klass.to_sym, "moneta/#{file}")
  self.cache = Moneta.const_get(klass).new(options)
  self.cache_expiry = expiry
end

.category(id, options = {}) ⇒ Object



24
25
26
27
# File 'lib/orthor.rb', line 24

def self.category(id, options = {})
  id = options[:page] && options[:page].to_i > 1 ? "#{id}-#{options[:page]}" : id
  Templates.render(get(:categories, id), options[:template])
end

.content(id, options = {}) ⇒ Object



16
17
18
# File 'lib/orthor.rb', line 16

def self.content(id, options = {})
  Templates.render(get(:content_items, id), options[:template])
end

.feed(id) ⇒ Object



29
30
31
# File 'lib/orthor.rb', line 29

def self.feed(id)
  get(:feeds, id)
end

.load_contentObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/orthor.rb', line 33

def self.load_content
  raise "No point loading content when you don't have a cache setup" unless cache

  load_category = lambda do |id|
    cat = category(id)
    cat["children"].each { |child| load_category[child["id"]] } if cat["children"]
    cat["content"].each  { |item| content(item["id"]) } if cat["content"]
  end

  JSON.parse(get_response("/#{site_id}.json")).each do |item|
    if item["type"] == "category"
      load_category[item["id"]]
    else
      content(item["id"])
    end
  end
end

.query(id, options = {}) ⇒ Object



20
21
22
# File 'lib/orthor.rb', line 20

def self.query(id, options = {})
  Templates.render(get(:queries, id), options[:template])
end

.render(items, template) ⇒ Object



51
52
53
# File 'lib/orthor.rb', line 51

def self.render(items, template)
  Templates.render(items, template)
end

.setup(&block) ⇒ Object

Raises:

  • (ArgumentError)


68
69
70
71
72
73
# File 'lib/orthor.rb', line 68

def self.setup(&block)
  raise ArgumentError, "Block required" unless block_given?
  self.cache = false # default

  class_eval &block
end

.site(id) ⇒ Object



64
65
66
# File 'lib/orthor.rb', line 64

def self.site(id)
  self.site_id = id
end