Class: Moka::SiteTree::GroupNode

Inherits:
TreeNode show all
Defined in:
lib/commands/lib/site_tree.rb

Instance Attribute Summary collapse

Attributes inherited from TreeNode

#name, #parent

Instance Method Summary collapse

Methods inherited from TreeNode

#children, #find_child, #params, #variables, #vars

Constructor Details

#initialize(name, params = {}, parent = nil) ⇒ GroupNode

Returns a new instance of GroupNode.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/commands/lib/site_tree.rb', line 118

def initialize(name, params = {}, parent = nil)
  super(name, params, parent)
  @site = parent

  if File.exist? File.expand_path("project/site/#{name}/variables.yml", MOKA_ROOT)
    group_vars = YAML.load_file(File.expand_path("project/site/#{name}/variables.yml", MOKA_ROOT)) || {}
  else
    group_vars = {}
  end
  @site.variables.merge(group_vars).each do |key, value|
    unless RESERVED_NAMES.include? key
      self.instance_variable_set "@#{key}", value
      @variables[key.to_s] = value if group_vars.keys.include? key.to_s
      self.create_method(key) do self.instance_variable_get("@#{key}") end
      self.create_method("#{key}=") do |v| self.instance_variable_set("@#{key}", v); @variables[key.to_s] = v end
    end
  end

  params.each do |key, value|
    key.gsub!(/\s/, "_")
    if value.is_a?(Hash)
      self.instance_variable_set "@#{key}", PageNode.new(key, value, self)
      @children << instance_eval("@#{key}")
    else
      self.instance_variable_set "@#{key}", value
      @params[key.to_s] = value
    end
    self.create_method(key) do self.instance_variable_get("@#{key}") end
  end
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



116
117
118
# File 'lib/commands/lib/site_tree.rb', line 116

def site
  @site
end

Instance Method Details

#find_page(page_name) ⇒ Object

Looks for a page in the group having the name equal to the string passed as argument. Returns the page or nil if no such page was found.



155
156
157
# File 'lib/commands/lib/site_tree.rb', line 155

def find_page(page_name)
  children.find(page_name)
end

#pagesObject

Returns a NodeArray object containing all the pages in the group (its children nodes)



150
151
152
# File 'lib/commands/lib/site_tree.rb', line 150

def pages
  self.children
end

#saveObject

Save group variables in the variables.yml file



160
161
162
163
164
# File 'lib/commands/lib/site_tree.rb', line 160

def save
  f = File.open( File.expand_path("project/site/#{self.name}/variables.yml", MOKA_ROOT), 'w' ) do |out|
    YAML.dump( variables(false), out )
  end
end