Class: PolicyManager::JsonExporterView

Inherits:
Object
  • Object
show all
Defined in:
lib/policy_manager/exporter/json_view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}, options) ⇒ JsonExporterView

Returns a new instance of JsonExporterView.



7
8
9
10
11
12
# File 'lib/policy_manager/exporter/json_view.rb', line 7

def initialize(vars={}, options)
  self.folder = options[:folder]
  self.assigns = options[:assigns]
  @template = options.fetch(:template) #, self.class.template)
  return self
end

Instance Attribute Details

#assignsObject

Returns the value of attribute assigns.



5
6
7
# File 'lib/policy_manager/exporter/json_view.rb', line 5

def assigns
  @assigns
end

#folderObject

Returns the value of attribute folder.



5
6
7
# File 'lib/policy_manager/exporter/json_view.rb', line 5

def folder
  @folder
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/policy_manager/exporter/json_view.rb', line 5

def template
  @template
end

Instance Method Details

#handled_templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/policy_manager/exporter/json_view.rb', line 31

def handled_template
  begin
    if URI.parse(@template)
      return {template: @template}
    end
  rescue URI::InvalidURIError
  end

  if @template.is_a?(String) 
    return {inline: @template}
  elsif @template.is_a?(Pathname)
    return {file: @template }
  end
end

#render_jsonObject



24
25
26
27
28
29
# File 'lib/policy_manager/exporter/json_view.rb', line 24

def render_json
  ac = PolicyManager::ExporterController.new()
  options = handled_template.merge!({assigns: self.assigns })
  content = ac.render_to_string(options)
  save_json("#{folder}/data.json", content)
end

#saveObject



14
15
16
# File 'lib/policy_manager/exporter/json_view.rb', line 14

def save
  render_json
end

#save_json(file, data) ⇒ Object



18
19
20
21
22
# File 'lib/policy_manager/exporter/json_view.rb', line 18

def save_json(file, data)
  File.open(file, "w") do |f|
    f.write(data)
  end
end