nuri_game-data_query_handler 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nuri_game/data_query_handler.rb +149 -0
- data/lib/nuri_game/data_query_handler/version.rb +5 -0
- data/nuri_game-data_query_handler.gemspec +27 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7bb0994c4614fb254f5fd898a18df2c20a7863bd8c703454ddff2c06da0cb4fe
|
4
|
+
data.tar.gz: 26acea7cae600db7e4972c7d8da33b7f24f7f88c71290acde56b2dd1aafbfb02
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84b4c9bb2083245c1334a8d9e5eee5c9b88a2e7435b4ca18eb5ce68034698f944c2189f2fafd9cd8f1f505fd6aada9fadd176d1d6b7ebba5faa846e3667e0f86
|
7
|
+
data.tar.gz: 286355f433d0cbde53656fb782a6d08637cdc0338ecf509b62fde8bfeb4325d810ebda8cd16817dea35bd72408f8a5671bb370e56e764947b8972de65e7541be
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nuri_game-data_query_handler (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
x86-mingw32
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
nuri_game-data_query_handler!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.4
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# NuriGame::DataQueryHandler
|
2
|
+
|
3
|
+
A simple helper to make data queries on objects inside an Enumerable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nuri_game-data_query_handler'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nuri_game-data_query_handler
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Create your own DataQueryHandler
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "nuri_game/data_query_handler" #< I'm not sure, it's my first gem
|
27
|
+
|
28
|
+
class YourDataQuery < NuriGame::DataQueryHandler
|
29
|
+
def initialize(error_handler = nil)
|
30
|
+
super(error_handler)
|
31
|
+
# Do stuff here like @data = some_enumerable_data
|
32
|
+
end
|
33
|
+
|
34
|
+
# Overwrite handle_object_query or try2fetch_object if you want more specific queries
|
35
|
+
# You can also overwrite query or query! to make sure your handler doesn't do unexpected things (like calling a sub handler if you don't want to).
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Use a DataQueryHandler
|
40
|
+
|
41
|
+
Lets assume dqh, dq2 and dq3 are data query handlers
|
42
|
+
|
43
|
+
#### Add dq2 & dq3 to dqh
|
44
|
+
```ruby
|
45
|
+
dqh.add_query_handler(:first_handler, dq2)
|
46
|
+
dqh.add_query_handler(:second_handler, dq3)
|
47
|
+
```
|
48
|
+
Note : You cannot add two DataQueryHandler on the same symbol, it'll raise ArgumentError with message "Query Handler <name> already added."
|
49
|
+
|
50
|
+
#### Get an object of dq2
|
51
|
+
```ruby
|
52
|
+
object = dqh.query(:first_handler, id: id_in_the_enum)
|
53
|
+
```
|
54
|
+
Note : When an object is not found, a IndexError (if id is a number) or a KeyError (otherwise) is raised.
|
55
|
+
Note2 : The enumerator cannot contain nil values, they're considered as non existing object since nil contain **no data**.
|
56
|
+
|
57
|
+
#### Get a property of an object of dq3
|
58
|
+
```ruby
|
59
|
+
property_value = dqh.query(:second_handler, :self, :property, id: id_in_the_enum)
|
60
|
+
# or
|
61
|
+
property_value = dqh.query!(:second_handler, :property, id: id_in_the_enum)
|
62
|
+
```
|
63
|
+
Note : query! doesn't raise an ArgumentError when doesn't find the query_handler :property, it does the same as telling :self before :property in query.
|
64
|
+
|
65
|
+
#### Get a hash of properties of an object of dq2
|
66
|
+
```ruby
|
67
|
+
hash = dqh.query(:first_handler, :self, :prop1, :prop2, :propn, id: id_in_the_enum)
|
68
|
+
# pr
|
69
|
+
hash = dqh.query!(:first_handler, :prop1, :prop2, :propn, id: id_in_the_enum)
|
70
|
+
```
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
75
|
+
|
76
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/NuriYuri/nuri_game-data_query_handler.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nuri_game/data_query_handler"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require "nuri_game/data_query_handler/version"
|
2
|
+
|
3
|
+
# NuriGame namespace containing all the utility made by Nuri Yuri in order to make Games.
|
4
|
+
module NuriGame
|
5
|
+
# Root class describing the basic data query handler to make data query in your game
|
6
|
+
#
|
7
|
+
# Usage :
|
8
|
+
# class YourDataQuery < NuriGame::DataQueryHandler
|
9
|
+
# def initialize(error_handler = nil)
|
10
|
+
# super(error_handler)
|
11
|
+
# # Do stuff here like @data = some_enumerable_data
|
12
|
+
# end
|
13
|
+
# # [...]
|
14
|
+
#
|
15
|
+
# You can custom #try2fetch_object and #handle_object_query to make more specific queries
|
16
|
+
class DataQueryHandler
|
17
|
+
# Create a new Data Query Handler
|
18
|
+
# @param error_handler [Class, nil] the error handler to use to display the errors (should respond to #warning #error with parameters klass, message)
|
19
|
+
# @param data [Enumerable] the preloaded data of the data query handler
|
20
|
+
def initialize(error_handler = nil, data: nil)
|
21
|
+
@sub_query_handlers = { self: nil }
|
22
|
+
@data = data
|
23
|
+
@error_handler = error_handler
|
24
|
+
end
|
25
|
+
|
26
|
+
# Function that invoke the query handler (recursively and stricly) in order to perform a data query
|
27
|
+
#
|
28
|
+
# When a query handle is not found but there's a object in the kwarg, the properties of the object are returned
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
# Return the id, atk and dfe of a Monster object
|
32
|
+
# data_query_handle.query(:self, :id, :atk, :dfe, object: monster)
|
33
|
+
# Get a monster from the :monster query_handle
|
34
|
+
# data_query_handle.query(:monster, :self, id: monster_id)
|
35
|
+
# @param handle [Symbol] name of the query handler (:self means were not requesting a sub query)
|
36
|
+
# @param args [Array] list of argument to send to the query handler including the sub query handlers
|
37
|
+
# @param kwarg [Hash<Symbol => Object>] list of keyword arguments to explain the query
|
38
|
+
# @return [Object] result of the query
|
39
|
+
def query(handle, *args, **kwarg)
|
40
|
+
if (object = kwarg[:object])
|
41
|
+
handle_object_query(object, args)
|
42
|
+
elsif (query_handler = @sub_query_handlers[handle])
|
43
|
+
query_handler.query(*args, **kwarg)
|
44
|
+
elsif handle != :self
|
45
|
+
raise_error(ArgumentError, 'Query Handler %<name>s not found!', name: handle)
|
46
|
+
else
|
47
|
+
perform_data_query(args, kwarg)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Function that invoke the query handler (recursively but not strictly) in order to perform a data query
|
52
|
+
#
|
53
|
+
# When a query handle is not found but there's a object in the kwarg, the properties of the object are returned
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# Return the id, atk and dfe of a Monster object
|
57
|
+
# data_query_handle.query!(:id, :atk, :dfe, object: monster)
|
58
|
+
# Get a monster from the :monster query_handle
|
59
|
+
# data_query_handle.query!(:monster, id: monster_id)
|
60
|
+
# @param args [Array] list of argument to send to the query handler including the sub query handlers
|
61
|
+
# @param kwarg [Hash<Symbol => Object>] list of keyword arguments to explain the query
|
62
|
+
# @return [Object] result of the query
|
63
|
+
def query!(*args, **kwarg)
|
64
|
+
if (object = kwarg[:object])
|
65
|
+
handle_object_query(object, args)
|
66
|
+
elsif (query_handler = @sub_query_handlers[args.first])
|
67
|
+
query_handler.query!(*args[1..-1], **kwarg)
|
68
|
+
else
|
69
|
+
perform_data_query(args, kwarg)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Function that adds a query handler to the sub_query_handlers
|
74
|
+
# @param name [Symbol]
|
75
|
+
# @param query_handler [DataQueryHandler]
|
76
|
+
def add_query_handler(name, query_handler)
|
77
|
+
raise_error(TypeError, 'Expected name to be a Symbol, got %<klass>s (%<value>s)', klass: name.class, value: name) unless name.is_a?(Symbol)
|
78
|
+
raise_error(TypeError, 'Expected query_handler to be a DataQueryHandler, got %<klass>s', klass: query_handler.class) unless query_handler.is_a?(DataQueryHandler)
|
79
|
+
raise_error(ArgumentError, 'Query Handler %<name>s already added.', name: name) if @sub_query_handlers.has_key?(name)
|
80
|
+
@sub_query_handlers[name] = query_handler
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# Function that handle the property query on an object
|
86
|
+
# @param object [Object]
|
87
|
+
# @param properties [Array<Symbol>]
|
88
|
+
# @return [Hash, object] if no properties, returns the object, otherwise, return a Hash associating the properties to the values
|
89
|
+
def handle_object_query(object, properties)
|
90
|
+
if properties.empty?
|
91
|
+
object
|
92
|
+
elsif properties.size == 1
|
93
|
+
return object.public_send(properties.first)
|
94
|
+
else
|
95
|
+
hash = {}
|
96
|
+
properties.each { |key| hash[key] = object.public_send(key) }
|
97
|
+
hash
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Function that perform a query on the data held by the query handle (this one will only be able to handle id query)
|
102
|
+
# @param args [Array<Symbol>] list of properties
|
103
|
+
# @param kwarg [Hash<Symbol => Object>] list of keyword used to find the object
|
104
|
+
# @return [Object]
|
105
|
+
def perform_data_query(args, kwarg)
|
106
|
+
if @data && !@data.empty?
|
107
|
+
return query(:self, *args, object: try2fetch_object(args, kwarg))
|
108
|
+
end
|
109
|
+
raise_error(RuntimeError, 'Data is %<information>s.', information: @data ? 'empty' : 'not loaded')
|
110
|
+
end
|
111
|
+
|
112
|
+
# Function that tries to fetch the object from data
|
113
|
+
# @param args [Array<Symbol>] list of properties
|
114
|
+
# @param kwarg [Hash<Symbol => Object>] list of keyword used to find the object
|
115
|
+
# @return [Object]
|
116
|
+
def try2fetch_object(args, kwarg)
|
117
|
+
if (id = kwarg[:id])
|
118
|
+
unless (object = @data[id]).nil?
|
119
|
+
object
|
120
|
+
else
|
121
|
+
raise_warning(id.is_a?(Integer) ? IndexError : KeyError, 'data[%<key>s] does not exist', key: id.inspect)
|
122
|
+
end
|
123
|
+
else
|
124
|
+
keys = kwarg.keys.reject { |key| key == :id }.join(', ')
|
125
|
+
raise_warning(KeyError, '%<keys>s couldn\'t be understood for this request', keys: keys)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Function that raise a warning with a specific class, message and the format of the message
|
130
|
+
# @param klass [StandardError] the class to use to raise the error
|
131
|
+
# @param message [String] the error message (unformated)
|
132
|
+
# @param format_args [Array] the args to use in order to format message
|
133
|
+
# @param format_kwarg [Hash] the kwargs to use in order to format message
|
134
|
+
def raise_warning(klass, message, *format_args, **format_kwarg)
|
135
|
+
message = format(message, *format_args, **format_kwarg)
|
136
|
+
@error_handler ? @error_handler.warning(klass, message) : warn(format('[%<klass>s] : %<message>s', klass: klass, message: message))
|
137
|
+
end
|
138
|
+
|
139
|
+
# Function that raise an error with a specific class, message and the format of the message
|
140
|
+
# @param klass [StandardError] the class to use to raise the error
|
141
|
+
# @param message [String] the error message (unformated)
|
142
|
+
# @param format_args [Array] the args to use in order to format message
|
143
|
+
# @param format_kwarg [Hash] the kwargs to use in order to format message
|
144
|
+
def raise_error(klass, message, *format_args, **format_kwarg)
|
145
|
+
message = format(message, *format_args, **format_kwarg)
|
146
|
+
@error_handler ? @error_handler.error(klass, message) : raise(klass, message)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nuri_game/data_query_handler/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nuri_game-data_query_handler"
|
8
|
+
spec.version = NuriGame::DataQueryHandler::VERSION
|
9
|
+
spec.authors = ["Nuri Yuri"]
|
10
|
+
spec.email = ["hostmaster@pokemonworkshop.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple helper to make data queries on objects inside an Enumerable.}
|
13
|
+
spec.homepage = "https://gitlab.com/NuriYuri/nuri_game-data_query_handler#nurigamedataqueryhandler"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nuri_game-data_query_handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nuri Yuri
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- hostmaster@pokemonworkshop.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/nuri_game/data_query_handler.rb
|
72
|
+
- lib/nuri_game/data_query_handler/version.rb
|
73
|
+
- nuri_game-data_query_handler.gemspec
|
74
|
+
homepage: https://gitlab.com/NuriYuri/nuri_game-data_query_handler#nurigamedataqueryhandler
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.7.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A simple helper to make data queries on objects inside an Enumerable.
|
97
|
+
test_files: []
|