Class: AdHocTemplate::RecipeManager
- Inherits:
-
Object
- Object
- AdHocTemplate::RecipeManager
- Includes:
- Utils
- Defined in:
- lib/ad_hoc_template/recipe_manager.rb
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#recipe ⇒ Object
readonly
Returns the value of attribute recipe.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#template_encoding ⇒ Object
readonly
Returns the value of attribute template_encoding.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(recipe_source) ⇒ RecipeManager
constructor
A new instance of RecipeManager.
- #load_records ⇒ Object
- #modified_after_last_output? ⇒ Boolean
- #parse_template ⇒ Object
- #prepare_block_data(block) ⇒ Object
- #read_recipe(recipe_source) ⇒ Object
- #update_output_file ⇒ Object
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_file ⇒ Object (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 |
#recipe ⇒ Object (readonly)
Returns the value of attribute recipe.
11 12 13 |
# File 'lib/ad_hoc_template/recipe_manager.rb', line 11 def recipe @recipe end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
11 12 13 |
# File 'lib/ad_hoc_template/recipe_manager.rb', line 11 def records @records end |
#template ⇒ Object (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_encoding ⇒ Object (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.(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_records ⇒ Object
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
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.(@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_template ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/ad_hoc_template/recipe_manager.rb', line 61 def parse_template template_path = File.(@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_file ⇒ Object
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.(@output_file), mode) {|file| file.print content } else STDOUT.print content end end |