Class: Rook::Cookbook

Inherits:
Object
  • Object
show all
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.message]
end

Constant Summary collapse

@@validator =
CookbookValidator.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assertion

#assert, #assert_failed

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, expand_tab=true)
  @precookings = []
  @properties  = []
  @variables   = []
  @materials   = []
  @recipes     = []
  @bookname    = bookname
  load_yamlfile(bookname, expand_tab) if bookname && load_yaml
end

Instance Attribute Details

#booknameObject

Returns the value of attribute bookname.



111
112
113
# File 'lib/rook/cookbook.rb', line 111

def bookname
  @bookname
end

#materialsObject (readonly)

Returns the value of attribute materials.



110
111
112
# File 'lib/rook/cookbook.rb', line 110

def materials
  @materials
end

#precookingsObject (readonly)

Returns the value of attribute precookings.



110
111
112
# File 'lib/rook/cookbook.rb', line 110

def precookings
  @precookings
end

#propertiesObject (readonly)

Returns the value of attribute properties.



110
111
112
# File 'lib/rook/cookbook.rb', line 110

def properties
  @properties
end

#recipesObject (readonly)

Returns the value of attribute recipes.



110
111
112
# File 'lib/rook/cookbook.rb', line 110

def recipes
  @recipes
end

#variablesObject (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, tab_expand=true)
  yaml_str = File.read(bookname)
  yaml_str = Util.untabify(yaml_str) if tab_expand
  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

#precookingObject



114
115
116
# File 'lib/rook/cookbook.rb', line 114

def precooking
  return @precookings.map { |hash| hash['code'] }.join("\n")
end