Class: HeyDan::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/heydan/source_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder, name, variable = nil) ⇒ SourceFile

Returns a new instance of SourceFile.



11
12
13
14
15
16
17
18
# File 'lib/heydan/source_file.rb', line 11

def initialize(folder, name, variable=nil)
  @folder = folder
  @name = name
  @variable = variable
  @folder_path = File.join(HeyDan.folders[:sources],@folder)
  @file_path = File.join(@folder_path, @name)
  get_json
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path.



5
6
7
# File 'lib/heydan/source_file.rb', line 5

def file_path
  @file_path
end

#folderObject

Returns the value of attribute folder.



7
8
9
# File 'lib/heydan/source_file.rb', line 7

def folder
  @folder
end

#folder_pathObject

Returns the value of attribute folder_path.



6
7
8
# File 'lib/heydan/source_file.rb', line 6

def folder_path
  @folder_path
end

#jsonObject

Returns the value of attribute json.



4
5
6
# File 'lib/heydan/source_file.rb', line 4

def json
  @json
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/heydan/source_file.rb', line 8

def name
  @name
end

#variable(variable_name = nil) ⇒ Object

Returns the value of attribute variable.



9
10
11
# File 'lib/heydan/source_file.rb', line 9

def variable
  @variable
end

Instance Method Details

#add_variable(variable_name) ⇒ Object



36
37
38
39
# File 'lib/heydan/source_file.rb', line 36

def add_variable(variable_name)    
  @json['variables'][variable_name] = variable_json(variable_name) if variable(variable_name).nil?
  variable(variable_name)
end

#create_folderObject



28
29
30
# File 'lib/heydan/source_file.rb', line 28

def create_folder
  FileUtils.mkdir_p @folder_path if !Dir.exist?(@folder_path)
end

#create_script_file(variable_name) ⇒ Object



49
50
51
52
# File 'lib/heydan/source_file.rb', line 49

def create_script_file(variable_name)
  file = HeyDan::ScriptFile.new(@folder, @name, variable_name)
  file.save
end

#create_variable_scriptsObject



41
42
43
44
45
46
47
# File 'lib/heydan/source_file.rb', line 41

def create_variable_scripts
  if @json['variables'].keys.size > 0
    @json['variables'].keys.each do |variable|
      create_script_file(variable)
    end
  end
end

#exist?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/heydan/source_file.rb', line 32

def exist?
  File.exist?(@file_path)
end

#get_jsonObject



20
21
22
23
24
25
26
# File 'lib/heydan/source_file.rb', line 20

def get_json
  if exist?
    @json = JSON.parse(File.read(file_path))
  else
    @json = initial_json
  end
end

#initial_jsonObject



54
55
56
# File 'lib/heydan/source_file.rb', line 54

def initial_json
  {'name' => @name, 'short_description' => 'A short description', 'long_description' => 'a longer description', 'notes' => nil, 'depends' => nil, 'sourceUrl' => 'the website of the source', 'variables' => {}}
end

#saveObject



71
72
73
74
75
76
77
# File 'lib/heydan/source_file.rb', line 71

def save
  create_folder
  create_variable_scripts
  File.open(@file_path, 'w') do |f|
    f.write(JSON.pretty_generate(@json))
  end
end

#variable_json(variable_name) ⇒ Object



58
59
60
# File 'lib/heydan/source_file.rb', line 58

def variable_json(variable_name)
  {'id' => "#{@folder}_#{@name}_#{variable_name}",'name' => variable_name, 'short_description' => 'a short description', 'long_description' => 'a description of the variable', 'notes' => 'any notes about this variable', 'identifier' => 'open_civic_id or ansi_id or other', 'dates' => [2015], 'tags' => [], 'sourceUrl' => 'website for variables information if different than source', 'jurisdiction_types' => [], 'coverage' => {} }
end

#variablesObject



67
68
69
# File 'lib/heydan/source_file.rb', line 67

def variables
  @json['variables'].keys
end