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.
-
#precookings ⇒ Object
readonly
Returns the value of attribute precookings.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#recipes ⇒ Object
readonly
Returns the value of attribute recipes.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
-
#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
- #precooking ⇒ 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) @precookings = [] @properties = [] @variables = [] @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 |
#precookings ⇒ Object (readonly)
Returns the value of attribute precookings.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def precookings @precookings 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 |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
110 111 112 |
# File 'lib/rook/cookbook.rb', line 110 def variables @variables end |
Instance Method Details
#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 |
# File 'lib/rook/cookbook.rb', line 144 def load_ydoc(ydoc, parser=nil) ydoc = ydoc.dup # code = ydoc['precooking'] ydoc['precookings'] = [ { 'code'=>code } ] if code # [ 'properties', 'variables' ].each do |key| next unless ydoc[key] list = [] ydoc[key].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) vkey = name[-1] == ?* ? 'expr' : '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 } h.update(opts) list << h end end ydoc[key] = list end # @precookings += _load_section(ydoc['precookings'], 'code', parser, 1) { |h, i| "/precooking" } @recipes += _load_section(ydoc['recipes'], 'method', parser, 1) { |h, i| "/recipes/#{i}/method" } @properties += _load_section(ydoc['properties*'], 'expr', parser, 0) { |h, i| "/properties*/#{i}/expr" } @variables += _load_section(ydoc['variables*'], 'expr', parser, 0) { |h, i| "/variables*/#{i}/expr" } @properties += _load_section(ydoc['properties'], 'expr', parser, 0) { |h, i| "/properties/#{i}/#{h['name']}*" } @variables += _load_section(ydoc['variables'], 'expr', parser, 0) { |h, i| "/variables/#{i}/#{h['name']}*" } # list = ydoc['materials'] @materials += list if list end |
#precooking ⇒ Object
114 115 116 |
# File 'lib/rook/cookbook.rb', line 114 def precooking return @precookings.map { |hash| hash['code'] }.join("\n") end |