Sha256: 451305ed7605a69d6fb8047a56352a4f9177e7a8c44de3602e959e134b6f5b38

Contents?: true

Size: 1.44 KB

Versions: 64

Compression:

Stored size: 1.44 KB

Contents

module Brief
  module Data
    class Queryable
      def initialize(array)
        @array = Array(array)
      end

      def where(*args, &block)
        Brief::DocumentMapper::Query.new(@array).send(:where, *args)
      end

      def method_missing(meth, *args, &block)
        @array.send(meth,*args,&block)
      end

    end

    class Wrapper
      attr_accessor :sources, :root

      def initialize(options={})
        @root = options.fetch(:root) { Pathname(Brief.pwd).join('data') }
        @sources = {}.to_mash

        load_files.each do |source, data|
          @sources[source] = Queryable.new(data)
        end
      end

      def method_missing(meth, *args, &block)
        return sources.send(meth, *args, &block) if sources.key?(meth)
        super
      end

      def load_files
        files = Dir[root.join("**/*.yml")] + Dir[root.join("**/*.json")] + Dir[root.join("**/*.yaml")]

        files.map! do |file|
          path = Pathname(file)

          if path.extname == ".json"
            key = "#{path.basename.to_s.gsub(/\.json$/i, '')}"
            data = JSON.parse(path.read)
          elsif path.extname == '.yml' || path.extname == ".yaml"
            key = "#{path.basename.to_s.gsub(/\.ya?ml$/i, '')}"
            data = YAML.load(path.read)
          else
            nil
          end

          {key => data} if key && data
        end

        files.compact.reduce({}) {|memo, hash| memo.merge(hash) }
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
brief-1.17.8 lib/brief/data.rb
brief-1.17.7 lib/brief/data.rb
brief-1.17.5 lib/brief/data.rb
brief-1.17.4 lib/brief/data.rb
brief-1.17.3 lib/brief/data.rb
brief-1.17.2 lib/brief/data.rb
brief-1.17.1 lib/brief/data.rb
brief-1.17.0 lib/brief/data.rb
brief-1.16.2 lib/brief/data.rb
brief-1.16.1 lib/brief/data.rb
brief-1.16.0 lib/brief/data.rb
brief-1.15.5 lib/brief/data.rb
brief-1.15.4 lib/brief/data.rb
brief-1.15.3 lib/brief/data.rb
brief-1.15.2 lib/brief/data.rb
brief-1.15.1 lib/brief/data.rb
brief-1.15.0 lib/brief/data.rb
brief-1.14.3 lib/brief/data.rb
brief-1.14.2 lib/brief/data.rb
brief-1.14.1 lib/brief/data.rb