Module: StaticRecord::LoaderFile

Included in:
Base
Defined in:
lib/static_record/loader_file.rb

Overview

Loader permits load and parse YAML file and use it as static database.

Instance Method Summary collapse

Instance Method Details

#databaseObject



23
24
25
# File 'lib/static_record/loader_file.rb', line 23

def database
  default_database
end

#initObject



4
5
6
# File 'lib/static_record/loader_file.rb', line 4

def init
  load true
end

#load(reload = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/static_record/loader_file.rb', line 8

def load(reload = false)
  return if loaded? && !reload

  # Read the StaticDB YAML fin in path.
  raw_data = File.open database, &:read
  # Parse embbebed Ruby in YAML with ERB
  erb_data = ERB.new raw_data
  data = YAML.load erb_data.result

  generate_records data
rescue
  raise YAMLFileError,
        "Can not open and parse #{database} static database."
end

#loaded?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/static_record/loader_file.rb', line 27

def loaded?
  @loaded ||= false
end

#reload!Object



31
32
33
# File 'lib/static_record/loader_file.rb', line 31

def reload!
  load true
end