Class: Moka::SiteTree::PageNode

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) ⇒ PageNode

Returns a new instance of PageNode.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/commands/lib/site_tree.rb', line 172

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

  if File.exist? File.expand_path("project/site/#{@group.name}/#{name}/variables.yml", MOKA_ROOT)
    page_vars = YAML.load_file(File.expand_path("project/site/#{@group.name}/#{name}/variables.yml", MOKA_ROOT)) || {}
  else
    page_vars = {}
  end
  @group.variables.merge(page_vars).each do |key, value|
    unless RESERVED_NAMES.include? key
      self.instance_variable_set("@#{key}", value)
      @variables[key.to_s] = value if page_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/, "_")
    self.instance_variable_set "@#{key}", value
    @params[key.to_s] = value
    self.create_method(key) do self.instance_variable_get("@#{key}") end
  end
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



170
171
172
# File 'lib/commands/lib/site_tree.rb', line 170

def group
  @group
end

Instance Method Details

#saveObject

Save page variables in the variables.yml file



199
200
201
202
203
# File 'lib/commands/lib/site_tree.rb', line 199

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