Class: Awestruct::Context

Inherits:
AStruct
  • Object
show all
Defined in:
lib/awestruct/context.rb

Constant Summary

Constants included from AStructMixin

AStructMixin::UNTRACKED_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AStruct

#hash

Methods included from AStructMixin

#[], #cascade_for_nils!, extended, #key?, #method_missing, #transform_entry

Constructor Details

#initialize(hash) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/awestruct/context.rb', line 9

def initialize(hash)
  super
  @page = hash[:page]
  @site = hash[:site]

  # Update the front matter from the handler chain
  @page.handler.front_matter.each do |k,v|
    k_sym = k.to_sym
    if @page.key? k_sym
      if @page[k_sym].is_a?(Array) && v.is_a?(Array)
        page_values = @page[k_sym].collect {|value| value.class.to_s}.sort
        front_matter_values = v.collect {|value| value.class.to_s}.sort
        @page[k_sym] = v if page_values.eql? front_matter_values
      elsif @page[k_sym].is_a?(Hash) && v.is_a?(Hash)
        page_values = @page[k_sym].collect {|key, value| value.class.to_s}.sort
        front_matter_values = v.collect {|key, value| value.class.to_s}.sort
        @page[k_sym] = v if page_values.eql? front_matter_values
      else
        @page[k_sym] = v if @page.key?(k_sym) && (@page[k_sym].class == v.class)
      end
    else # new key, just add it
      @page[k_sym] = v
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Awestruct::AStructMixin

Instance Attribute Details

#pageObject

Returns the value of attribute page.



7
8
9
# File 'lib/awestruct/context.rb', line 7

def page
  @page
end

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/awestruct/context.rb', line 6

def site
  @site
end

Instance Method Details

#inspectObject



35
36
37
# File 'lib/awestruct/context.rb', line 35

def inspect
  "Awestruct::Context{:page=>#{self.page.inspect}}"
end