Class: NuriGame::DataQueryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/nuri_game/data_query_handler.rb,
lib/nuri_game/data_query_handler/version.rb

Overview

Root class describing the basic data query handler to make data query in your game

Usage :

class YourDataQuery < NuriGame::DataQueryHandler
  def initialize(error_handler = nil)
    super(error_handler)
    # Do stuff here like @data = some_enumerable_data
  end
# [...]

You can custom #try2fetch_object and #handle_object_query to make more specific queries

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(error_handler = nil, data: nil) ⇒ DataQueryHandler

Create a new Data Query Handler

Parameters:

  • error_handler (Class, nil) (defaults to: nil)

    the error handler to use to display the errors (should respond to #warning #error with parameters klass, message)

  • data (Enumerable) (defaults to: nil)

    the preloaded data of the data query handler



20
21
22
23
24
# File 'lib/nuri_game/data_query_handler.rb', line 20

def initialize(error_handler = nil, data: nil)
  @sub_query_handlers = { self: nil }
  @data = data
  @error_handler = error_handler
end

Instance Method Details

#add_query_handler(name, query_handler) ⇒ Object

Function that adds a query handler to the sub_query_handlers

Parameters:



76
77
78
79
80
81
# File 'lib/nuri_game/data_query_handler.rb', line 76

def add_query_handler(name, query_handler)
  raise_error(TypeError, 'Expected name to be a Symbol, got %<klass>s (%<value>s)', klass: name.class, value: name) unless name.is_a?(Symbol)
  raise_error(TypeError, 'Expected query_handler to be a DataQueryHandler, got %<klass>s', klass: query_handler.class) unless query_handler.is_a?(DataQueryHandler)
  raise_error(ArgumentError, 'Query Handler %<name>s already added.', name: name) if @sub_query_handlers.has_key?(name)
  @sub_query_handlers[name] = query_handler
end

#query(handle, *args, **kwarg) ⇒ Object

Function that invoke the query handler (recursively and stricly) in order to perform a data query

When a query handle is not found but there’s a object in the kwarg, the properties of the object are returned

Examples:

Return the id, atk and dfe of a Monster object
  data_query_handle.query(:self, :id, :atk, :dfe, object: monster)
Get a monster from the :monster query_handle
  data_query_handle.query(:monster, :self, id: monster_id)

Parameters:

  • handle (Symbol)

    name of the query handler (:self means were not requesting a sub query)

  • args (Array)

    list of argument to send to the query handler including the sub query handlers

  • kwarg (Hash<Symbol => Object>)

    list of keyword arguments to explain the query

Returns:

  • (Object)

    result of the query



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nuri_game/data_query_handler.rb', line 39

def query(handle, *args, **kwarg)
  if (object = kwarg[:object])
    handle_object_query(object, args)
  elsif (query_handler = @sub_query_handlers[handle])
    query_handler.query(*args, **kwarg)
  elsif handle != :self
    raise_error(ArgumentError, 'Query Handler %<name>s not found!', name: handle)
  else
    perform_data_query(args, kwarg)
  end
end

#query!(*args, **kwarg) ⇒ Object

Function that invoke the query handler (recursively but not strictly) in order to perform a data query

When a query handle is not found but there’s a object in the kwarg, the properties of the object are returned

Examples:

Return the id, atk and dfe of a Monster object
  data_query_handle.query!(:id, :atk, :dfe, object: monster)
Get a monster from the :monster query_handle
  data_query_handle.query!(:monster, id: monster_id)

Parameters:

  • args (Array)

    list of argument to send to the query handler including the sub query handlers

  • kwarg (Hash<Symbol => Object>)

    list of keyword arguments to explain the query

Returns:

  • (Object)

    result of the query



63
64
65
66
67
68
69
70
71
# File 'lib/nuri_game/data_query_handler.rb', line 63

def query!(*args, **kwarg)
  if (object = kwarg[:object])
    handle_object_query(object, args)
  elsif (query_handler = @sub_query_handlers[args.first])
    query_handler.query!(*args[1..-1], **kwarg)
  else
    perform_data_query(args, kwarg)
  end
end