Sha256: 60229a3d0fb63fe549fa44ad6db15b090e8844f159be771263ccfc053105238a

Contents?: true

Size: 1014 Bytes

Versions: 2

Compression:

Stored size: 1014 Bytes

Contents

module StaticRecord
  # Loader  permits load and parse YAML file and use it as static database.
  module LoaderFile
    def init
      load true
    end

    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

    def database
      default_database
    end

    def loaded?
      @loaded ||= false
    end

    private

    def default_database
      File.join(StaticRecord::Base.file_path, "#{to_s.tableize}.yml")
    end

    def generate_records(data)
      raise StaticModel::YAMLFileError,
            'Data is not a valid array.' unless data.is_a? Array

      @records = data.dup.collect { |r| new(r, false) }
      @loaded = true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_static_record-0.1.1 lib/static_record/loader_file.rb
rails_static_record-0.1.0 lib/static_record/loader_file.rb