Sha256: f61d9848db598ddd0b16b8ca4a972988abb2e651f80d439064db110533164029

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

module Materialize
  class Repo
    include Utils

    attr_reader :token

    def initialize(token = nil)
      @token = token
    end

    def method_missing(query, *args, &block)
      data_source_class = args[0]
      options           = args[1] || {}
      args_to_pass      = options[:args]

      data, builder_class = process(data_source_class, query, args_to_pass)

      yield(data) if block_given?

      options.delete(:args)

      if data.is_a?(Hash) and !data['message'].nil?
			  Response.new data
      elsif data.is_a?(Array)
        builder_class.build_all(data, self, options)
      else
        builder_class.build(data, self, options)
      end
    end

    private

    def process(data_source_class, query, args_to_pass)
      data = get_data(data_source_class, query, args_to_pass)
      builder_class = builder_class_for builder_class_name_for base_class_name_for data_source_class
      return data, builder_class
    end

    def builder_class_for(builder_class_name)
      if class_exists?(builder_class_name)
        Module.const_get(builder_class_name)
      else
        Object.const_set(builder_class_name, Class.new(Materialize::BaseBuilder))
      end
    end

    def builder_class_name_for(base_class_name)
      "#{base_class_name}Builder"
    end

    def base_class_name_for(data_source_class)
      data_source_class.name.split('::').last
    end

    def get_data(data_source_class, query, args_to_pass)
      if token.nil?

        if args_to_pass.nil?
          data_source_class.send(query)
        elsif args_to_pass.is_a?(Array)
          data_source_class.send(query, *args_to_pass)
        else
          data_source_class.send(query, args_to_pass)
        end

      else

        if args_to_pass.nil?
          data_source_class.send(query, token)
        elsif args_to_pass.is_a?(Array)
          data_source_class.send(query, token, *args_to_pass)
        else
          data_source_class.send(query, token, args_to_pass)
        end

      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
materialize-0.4.0 lib/materialize/repo.rb
materialize-0.3.5 lib/materialize/repo.rb
materialize-0.3.4 lib/materialize/repo.rb
materialize-0.3.3 lib/materialize/repo.rb