Class: Moka::SiteTree::SiteNode

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

Instance Attribute Summary

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

Returns a new instance of SiteNode.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/commands/lib/site_tree.rb', line 68

def initialize(name, params = {}, parent = nil)
  super(name, params, parent)
  if File.exist? File.expand_path("project/site/variables.yml", MOKA_ROOT)
    site_vars = YAML.load_file(File.expand_path("project/site/variables.yml", MOKA_ROOT)) || {}
    site_vars.each do |key, value|
      unless RESERVED_NAMES.include? key
        self.instance_variable_set "@#{key}", value
        @variables[key.to_s] = value
        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
  end

  params.each do |key, value|
    key.gsub!(/\s/, "_")
    if value.is_a?(Hash)
      self.instance_variable_set "@#{key}", GroupNode.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 Method Details

#find_group(group_name) ⇒ Object

Returns the group having the name equal to the string passed as argument or nil if such group does not exist



101
102
103
# File 'lib/commands/lib/site_tree.rb', line 101

def find_group(group_name)
  children.find(group_name)
end

#groupsObject

Returns a NodeArray object containing all the site’s groups (the children of the site node)



96
97
98
# File 'lib/commands/lib/site_tree.rb', line 96

def groups
  self.children
end

#saveObject

Save site variables in the variables.yml file



106
107
108
109
110
# File 'lib/commands/lib/site_tree.rb', line 106

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