Sha256: be6b9cdd0918d76c8631c24c0856fbae9c699cd0c87d5f6b929e907ac2455eb6

Contents?: true

Size: 1.51 KB

Versions: 11

Compression:

Stored size: 1.51 KB

Contents

require 'eucalypt/helpers/inflect'
require 'json'
require 'yaml'

module Eucalypt
  class Static
    include Eucalypt::Helpers

    FILE_TYPES = {yaml: %w[yml yaml], json: %w[json geojson]}

    def initialize(directory, symbolize: false)
      contents = Dir[File.join directory, '*']
      files = contents.select {|f| File.file? f}
      subdirectories = contents.select {|f| File.directory? f}
      files.each {|f| define_file_accessor f, symbolize}
      subdirectories.each {|s| define_subdirectory_accessor s, symbolize}
    end

    private

    def define_file_accessor(file, symbolize)
      file_name = File.basename file, '.*'
      extension = File.extname(file).sub /\A./, ''
      file_type = nil
      FILE_TYPES.each {|t, e| file_type = t if e.include? extension}
      raise TypeError.new("Unsupported extension: .#{extension}") if file_type.nil?
      define_singleton_method Inflect.resource_keep_inflection(file_name) do
        hash = parse(file_type, file)
        hash = hash ? hash : {}
        symbolize ? hash.deep_symbolize_keys : hash
      end
    end

    def define_subdirectory_accessor(subdirectory, symbolize)
      subdirectory_name = subdirectory.split(File::SEPARATOR).last
      define_singleton_method Inflect.resource_keep_inflection(subdirectory_name) do
        Static.new subdirectory, symbolize: symbolize
      end
    end

    def parse(file_type, file)
      case file_type
      when :yaml then YAML.load_file(file)
      when :json then JSON.parse(File.read file)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
eucalypt-0.3.3 lib/eucalypt/static.rb
eucalypt-0.3.2 lib/eucalypt/static.rb
eucalypt-0.3.1 lib/eucalypt/static.rb
eucalypt-0.3.0 lib/eucalypt/static.rb
eucalypt-0.2.2 lib/eucalypt/static.rb
eucalypt-0.2.1 lib/eucalypt/static.rb
eucalypt-0.2.0 lib/eucalypt/static.rb
eucalypt-0.1.3 lib/eucalypt/static.rb
eucalypt-0.1.2 lib/eucalypt/static.rb
eucalypt-0.1.1 lib/eucalypt/static.rb
eucalypt-0.1.0 lib/eucalypt/static.rb