Class: Rook::Cookbook
- Inherits:
-
Object
- Object
- Rook::Cookbook
- Includes:
- Assertion
- Defined in:
- lib/rook/cookbook.rb
Overview
represents cookbook
ex.
cookbooks, errors = Cookbook.load_yamlfile('Rookbook.yaml')
if errors && !errors.empty?
puts "%d: [%s] %s" % [errors.linenum, errors.path, errors.]
end
Constant Summary collapse
- @@validator =
CookbookValidator.new
Instance Attribute Summary collapse
-
#bookname ⇒ Object
Returns the value of attribute bookname.
-
#materials ⇒ Object
readonly
Returns the value of attribute materials.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#preparations ⇒ Object
readonly
Returns the value of attribute preparations.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#recipes ⇒ Object
readonly
Returns the value of attribute recipes.
Instance Method Summary collapse
- #_add_code(lines, hash, key) ⇒ Object
-
#initialize(bookname = nil, load_yaml = true, expand_tab = true) ⇒ Cookbook
constructor
A new instance of Cookbook.
- #load_yamlfile(bookname, tab_expand = true) ⇒ Object
- #load_yamlstr(yaml_str) ⇒ Object
- #load_ydoc(ydoc, parser = nil) ⇒ Object
- #preparation ⇒ Object
- #validate_rubycode ⇒ Object
Methods included from Assertion
Constructor Details
#initialize(bookname = nil, load_yaml = true, expand_tab = true) ⇒ Cookbook
Returns a new instance of Cookbook.
101 102 103 104 105 106 107 108 109 |
# File 'lib/rook/cookbook.rb', line 101 def initialize(bookname=nil, load_yaml=true, =true) @preparations = [] @properties = [] @parameters = [] @materials = [] @recipes = [] @bookname = bookname load_yamlfile(bookname, ) if bookname && load_yaml end |
Instance Attribute Details
#bookname ⇒ Object
Returns the value of attribute bookname.
111 112 113 |
# File 'lib/rook/cookbook.rb', line 111 def bookname @bookname end |
#materials ⇒ Object (readonly)
Returns the value of attribute materials.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def materials @materials end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def parameters @parameters end |
#preparations ⇒ Object (readonly)
Returns the value of attribute preparations.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def preparations @preparations end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def properties @properties end |
#recipes ⇒ Object (readonly)
Returns the value of attribute recipes.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def recipes @recipes end |
Instance Method Details
#_add_code(lines, hash, key) ⇒ Object
238 239 240 241 242 243 244 245 |
# File 'lib/rook/cookbook.rb', line 238 def _add_code(lines, hash, key) if hash.key?(key) code = hash[key] n = hash['linenum'] - 1 code.each_line { |line| lines[n += 1] = line.chomp } lines[n] = lines[n] + ';' end end |
#load_yamlfile(bookname, tab_expand = true) ⇒ Object
119 120 121 122 123 |
# File 'lib/rook/cookbook.rb', line 119 def load_yamlfile(bookname, =true) yaml_str = File.read(bookname) yaml_str = Util.untabify(yaml_str) if return load_yamlstr(yaml_str) end |
#load_yamlstr(yaml_str) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rook/cookbook.rb', line 126 def load_yamlstr(yaml_str) parser = Kwalify::YamlParser.new(yaml_str) errors = [] while parser.has_next? ydoc = parser.parse() next if ydoc.nil? errs = @@validator.validate(ydoc) if errs && !errs.empty? parser.set_errors_linenum(errs) errors += errs else load_ydoc(ydoc, parser) end end errors.empty? or raise CookbookError.new("invalid cookbook", errors) end |
#load_ydoc(ydoc, parser = nil) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rook/cookbook.rb', line 144 def load_ydoc(ydoc, parser=nil) ydoc = ydoc.dup # preparation if code = ydoc['preparation*'] linenum = parser.path_linenum('/preparation*') @preparations << { 'code'=>code, 'linenum'=>linenum+1 } end # properties, parameters [ 'properties', 'parameters' ].each do |part| next unless ydoc[part] list = part == 'properties' ? @properties : @parameters ydoc[part].each_with_index do |prop, i| opts = {} prop.keys.each do |k| opts[k.to_s] = prop[k] if k.is_a?(Symbol) end prop.each do |name, val| next unless name.is_a?(String) linenum = parser.path_linenum("/#{part}/#{i}/#{name}") vkey = name[-1] == ?* ? 'value*' : 'value' #name[-1,1] = '' if name[-1] == ?* # TypeError: can't modify frozen string name = name[0, name.length-1] if name[-1] == ?* h = { 'name'=>name, vkey=>val, 'linenum'=>linenum } h.update(opts) list << h end end end # materials if list = ydoc['materials'] @materials += list end # recipes if list = ydoc['recipes'] list.each_with_index do |hash, i| if hash['method*'] linenum = parser.path_linenum("/recipes/#{i}/method*") hash['linenum'] = linenum+1 end if hash['params'] list = [] hash['params'].each_with_index do |params, n| params.each do |name, val| linenum = parser.path_linenum("/recipes/#{i}/params/#{n}/#{name}") list << { 'name'=>name, 'value'=>val, 'linenum'=>linenum } end end hash['params'] = list end @recipes << hash end end end |
#preparation ⇒ Object
114 115 116 |
# File 'lib/rook/cookbook.rb', line 114 def preparation return @preparations.map { |hash| hash['code'] }.join("\n") end |
#validate_rubycode ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rook/cookbook.rb', line 197 def validate_rubycode lines = [] @preparations.each do |hash| _add_code(lines, hash, 'code') end if @preparations @properties.each do |hash| _add_code(lines, hash, 'value*') if hash.key?('value*') end if @properties @parameters.each do |hash| _add_code(lines, hash, 'value*') if hash.key?('value*') end if @parameters @recipes.each do |hash| _add_code(lines, hash, 'method*') if hash.key?('method*') hash['params'].each do |h| name = h['name'] _add_code(lines, h, 'value') if name[-1] == ?* end if hash.key?('params') end if @recipes # lines.shift # ignore first item code = '' lines.each do |line| code << (line || "") << "\n" end tmpfilename = "#{@bookname}." # or user tmpfile library begin File.open(tmpfilename, 'w') { |f| f.write(code) } #case RUBY_PLATFORM #when /linux/, /bsd/, /darwin/, /cygwin/ out = `ruby -wc #{tmpfilename} 2>&1` puts out.gsub(/^#{tmpfilename}/, bookname) #else # system "ruby -wc #{tmpfilename}" #end ensure File.unlink(tmpfilename) if test(?f, tmpfilename) end end |