Class: Huck::Generators::JsonGenerator

Inherits:
Huck::Generator show all
Defined in:
lib/huck/generators/json.rb

Overview

JSON generator reads json files for data

Instance Attribute Summary

Attributes inherited from Huck::Generator

#config

Instance Method Summary collapse

Methods inherited from Huck::Generator

#dump, factory

Constructor Details

#initializeJsonGenerator

Load required modules for json generator



9
10
11
# File 'lib/huck/generators/json.rb', line 9

def initialize
  require 'json'
end

Instance Method Details

#generateObject

Reads in a configured JSON file and returns the data

Returns:

A hash of facts read from the JSON file



27
28
29
30
# File 'lib/huck/generators/json.rb', line 27

def generate
  verify_config
  JSON.load IO.read(@config['json']['file'])
end

#verify_configObject

Ensure that all JSON config items are properly set



14
15
16
17
18
19
20
21
# File 'lib/huck/generators/json.rb', line 14

def verify_config
  if !@config.has_key? 'json'
    raise RuntimeError, 'missing json config'
  end
  if !@config['json'].has_key? 'file'
    raise RuntimeError, 'missing json config: file'
  end
end