Class: AdHocTemplate::RecipeManager

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/ad_hoc_template/recipe_manager.rb

Constant Summary

Constants included from Utils

Utils::FILE_EXTENTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#guess_file_format, #if_any_regex_match

Constructor Details

#initialize(recipe_source) ⇒ RecipeManager

Returns a new instance of RecipeManager.



24
25
26
27
# File 'lib/ad_hoc_template/recipe_manager.rb', line 24

def initialize(recipe_source)
  @default = {}
  read_recipe(recipe_source)
end

Instance Attribute Details

#output_fileObject (readonly)

Returns the value of attribute output_file.



10
11
12
# File 'lib/ad_hoc_template/recipe_manager.rb', line 10

def output_file
  @output_file
end

#recipeObject (readonly)

Returns the value of attribute recipe.



11
12
13
# File 'lib/ad_hoc_template/recipe_manager.rb', line 11

def recipe
  @recipe
end

#recordsObject (readonly)

Returns the value of attribute records.



11
12
13
# File 'lib/ad_hoc_template/recipe_manager.rb', line 11

def records
  @records
end

#templateObject (readonly)

Returns the value of attribute template.



10
11
12
# File 'lib/ad_hoc_template/recipe_manager.rb', line 10

def template
  @template
end

#template_encodingObject (readonly)

Returns the value of attribute template_encoding.



10
11
12
# File 'lib/ad_hoc_template/recipe_manager.rb', line 10

def template_encoding
  @template_encoding
end

Class Method Details

.update_output_files_in_recipe(recipe_file, force_update = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/ad_hoc_template/recipe_manager.rb', line 13

def self.update_output_files_in_recipe(recipe_file, force_update=false)
  recipe_source = File.open(File.expand_path(recipe_file), &:read)
  recipes = YAML.load_stream(recipe_source)
  recipes.each do |recipe|
    manager = new(recipe)
    if manager.modified_after_last_output? || force_update
      manager.update_output_file
    end
  end
end

Instance Method Details

#load_recordsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/ad_hoc_template/recipe_manager.rb', line 41

def load_records
  @records = prepare_block_data(@recipe).tap do |main_block|
    @recipe['blocks'].each do |block_source|
      block = prepare_block_data(block_source)
      block.keys.each do |key|
        main_block[key] ||= block[key]
      end
    end
  end
end

#modified_after_last_output?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
# File 'lib/ad_hoc_template/recipe_manager.rb', line 83

def modified_after_last_output?
  return true unless @output_file
  output_path = File.expand_path(@output_file)
  return true unless File.exist? output_path
  output_time = File.mtime(output_path)
  return true if modified_time(@recipe['template']) >= output_time
  data_files = @recipe['blocks'].map {|block| block['data'] }
  data_files.unshift(@recipe['data'])
  data_files.any? {|data_file| modified_time(data_file) >= output_time }
end

#parse_templateObject



61
62
63
64
65
66
67
68
# File 'lib/ad_hoc_template/recipe_manager.rb', line 61

def parse_template
  template_path = File.expand_path(@recipe['template'])
  template_source = File.open(template_path,
                              open_mode(@template_encoding), &:read)
  tag_type = @recipe['tag_type'] || :default
  tag_type = tag_type.to_sym unless tag_type.kind_of? Symbol
  @template = Parser.parse(template_source, tag_type)
end

#prepare_block_data(block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ad_hoc_template/recipe_manager.rb', line 52

def prepare_block_data(block)
  determine_data_format!(block)
  return {} unless block['data']
  data_source = read_file(block['data'],
                          block['data_encoding'])
  data_format = prepare_data_format(block)
  RecordReader.read_record(data_source, data_format)
end

#read_recipe(recipe_source) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ad_hoc_template/recipe_manager.rb', line 29

def read_recipe(recipe_source)
  @recipe = if recipe_source.kind_of? String
              RecordReader::YAMLReader.read_record(recipe_source)
            else
              recipe_source
            end
  setup_default!(@recipe)
  @template_encoding = @default['template_encoding']
  @output_file = @default['output_file']
  @recipe
end

#update_output_fileObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ad_hoc_template/recipe_manager.rb', line 70

def update_output_file
  @records ||= load_records
  parse_template
  content = AdHocTemplate::DataLoader.format(@template, @records)
  mode = @template_encoding ? "wb:#{@template_encoding}" : 'wb'
  if @output_file
    File.open(File.expand_path(@output_file),
              mode) {|file| file.print content }
  else
    STDOUT.print content
  end
end