chgk_rating 1.0.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -0
- data/Gemfile +3 -1
- data/LICENSE +1 -1
- data/README.md +10 -501
- data/Rakefile +4 -2
- data/chgk_rating.gemspec +23 -15
- data/lib/chgk_rating/attribute_mappings.rb +107 -80
- data/lib/chgk_rating/chgk_object.rb +6 -1
- data/lib/chgk_rating/client.rb +43 -10
- data/lib/chgk_rating/collections/base.rb +20 -15
- data/lib/chgk_rating/collections/players.rb +3 -1
- data/lib/chgk_rating/collections/ratings/player_ratings.rb +24 -0
- data/lib/chgk_rating/collections/{ratings.rb → ratings/team_ratings.rb} +5 -4
- data/lib/chgk_rating/collections/recaps.rb +4 -2
- data/lib/chgk_rating/collections/teams.rb +3 -1
- data/lib/chgk_rating/collections/tournaments/player_tournaments.rb +48 -0
- data/lib/chgk_rating/collections/{tournament_players.rb → tournaments/tournament_team_players.rb} +5 -3
- data/lib/chgk_rating/collections/{tournament_team_results.rb → tournaments/tournament_team_results.rb} +3 -1
- data/lib/chgk_rating/collections/{tournament_teams.rb → tournaments/tournament_teams.rb} +4 -1
- data/lib/chgk_rating/collections/{tournaments.rb → tournaments/tournaments.rb} +13 -8
- data/lib/chgk_rating/concerns/searching.rb +10 -9
- data/lib/chgk_rating/connection.rb +12 -7
- data/lib/chgk_rating/error.rb +7 -5
- data/lib/chgk_rating/models/base.rb +34 -15
- data/lib/chgk_rating/models/player.rb +28 -1
- data/lib/chgk_rating/models/rating/player_rating.rb +18 -0
- data/lib/chgk_rating/models/{tournament_player.rb → rating/rating.rb} +4 -2
- data/lib/chgk_rating/models/{rating.rb → rating/team_rating.rb} +4 -5
- data/lib/chgk_rating/models/recap.rb +3 -1
- data/lib/chgk_rating/models/team.rb +9 -7
- data/lib/chgk_rating/models/tournament/player_tournament.rb +10 -0
- data/lib/chgk_rating/models/{tournament.rb → tournament/tournament.rb} +7 -5
- data/lib/chgk_rating/models/{tournament_team.rb → tournament/tournament_team.rb} +6 -4
- data/lib/chgk_rating/models/tournament/tournament_team_player.rb +10 -0
- data/lib/chgk_rating/models/{tournament_team_result.rb → tournament/tournament_team_result.rb} +3 -1
- data/lib/chgk_rating/request.rb +10 -9
- data/lib/chgk_rating/utils/snakecase.rb +9 -7
- data/lib/chgk_rating/utils/transformations.rb +42 -35
- data/lib/chgk_rating/version.rb +4 -2
- data/lib/chgk_rating.rb +18 -11
- data/lib/ext/date.rb +6 -8
- data/lib/ext/date_time.rb +4 -2
- data/lib/ext/uri.rb +6 -6
- metadata +95 -90
- data/spec/lib/chgk_rating/client_spec.rb +0 -208
- data/spec/lib/chgk_rating/collections/players_spec.rb +0 -45
- data/spec/lib/chgk_rating/collections/ratings_spec.rb +0 -25
- data/spec/lib/chgk_rating/collections/recaps_spec.rb +0 -21
- data/spec/lib/chgk_rating/collections/teams_spec.rb +0 -42
- data/spec/lib/chgk_rating/collections/tournament_players_spec.rb +0 -18
- data/spec/lib/chgk_rating/collections/tournament_team_results_spec.rb +0 -17
- data/spec/lib/chgk_rating/collections/tournament_teams_spec.rb +0 -60
- data/spec/lib/chgk_rating/collections/tournaments_spec.rb +0 -62
- data/spec/lib/chgk_rating/models/base_spec.rb +0 -34
- data/spec/lib/chgk_rating/models/player_spec.rb +0 -35
- data/spec/lib/chgk_rating/models/rating_spec.rb +0 -27
- data/spec/lib/chgk_rating/models/recap_spec.rb +0 -35
- data/spec/lib/chgk_rating/models/team_spec.rb +0 -89
- data/spec/lib/chgk_rating/models/tournament_player_spec.rb +0 -21
- data/spec/lib/chgk_rating/models/tournament_spec.rb +0 -92
- data/spec/lib/chgk_rating/models/tournament_team_result_spec.rb +0 -19
- data/spec/lib/chgk_rating/models/tournament_team_spec.rb +0 -36
- data/spec/lib/chgk_rating/utils/snakecase_spec.rb +0 -12
- data/spec/lib/chgk_rating/utils/transformations_spec.rb +0 -16
- data/spec/lib/chgk_rating_spec.rb +0 -5
- data/spec/lib/ext/date_spec.rb +0 -7
- data/spec/lib/ext/date_time_spec.rb +0 -11
- data/spec/lib/ext/uri_spec.rb +0 -7
- data/spec/spec_helper.rb +0 -13
- data/spec/support/shared_examples.rb +0 -63
- data/spec/support/test_client.rb +0 -6
- data/spec/support/vcr.rb +0 -11
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Collections
|
3
5
|
class Tournaments < Base
|
@@ -7,18 +9,18 @@ module ChgkRating
|
|
7
9
|
@team = build_model params[:team]
|
8
10
|
|
9
11
|
@season_id = params[:season_id]
|
10
|
-
params
|
12
|
+
params[:lazy] = true if @team || @season_id
|
11
13
|
super
|
12
14
|
end
|
13
15
|
|
14
16
|
def revert_to_hash(key, values)
|
15
17
|
[
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
key,
|
19
|
+
{
|
20
|
+
'idteam' => @team&.id.to_s,
|
21
|
+
'idseason' => key,
|
22
|
+
'tournaments' => values.map(&:to_h)
|
23
|
+
}
|
22
24
|
]
|
23
25
|
end
|
24
26
|
|
@@ -35,13 +37,16 @@ module ChgkRating
|
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
40
|
+
# @return [String] Either `tournaments`, `teams/ID/tournaments`, or `teams/ID/tournaments/SEASON_ID`
|
38
41
|
def api_path
|
39
42
|
path = 'tournaments'
|
40
43
|
return path unless @team
|
44
|
+
|
41
45
|
path = "teams/#{@team.id}/#{path}"
|
42
46
|
return path unless @season_id
|
47
|
+
|
43
48
|
path + "/#{@season_id}"
|
44
49
|
end
|
45
50
|
end
|
46
51
|
end
|
47
|
-
end
|
52
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Concerns
|
3
5
|
module Searching
|
@@ -5,21 +7,20 @@ module ChgkRating
|
|
5
7
|
# Creates a Search child class of a given class that changes the URI where the
|
6
8
|
# GET request should be sent
|
7
9
|
def self.included(klass)
|
8
|
-
klass.const_set(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
)
|
10
|
+
klass.const_set(:Search,
|
11
|
+
Class.new(klass) do |*_args|
|
12
|
+
def api_path
|
13
|
+
"#{super}/search"
|
14
|
+
end
|
15
|
+
end)
|
15
16
|
|
16
17
|
# The actual method to perform searching
|
17
18
|
# Instantiates a Search class with the given search params
|
18
19
|
# and send a GET request to the proper URI (defined above)
|
19
20
|
klass.define_singleton_method :search do |params|
|
20
|
-
klass.const_get(
|
21
|
+
klass.const_get(:Search).new(params)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
25
|
-
end
|
26
|
+
end
|
@@ -1,19 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday/follow_redirects'
|
4
|
+
|
1
5
|
module ChgkRating
|
2
6
|
module Connection
|
3
|
-
BASE_URL = 'http://rating.chgk.info/api'
|
7
|
+
BASE_URL = 'http://rating.chgk.info/api'
|
4
8
|
|
5
9
|
def connection
|
6
10
|
options = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
headers: {
|
12
|
+
accept: 'application/json',
|
13
|
+
user_agent: "chgk_rating ruby gem/#{ChgkRating::VERSION}"
|
14
|
+
},
|
15
|
+
url: "#{BASE_URL}/"
|
12
16
|
}
|
13
17
|
|
14
18
|
Faraday.new options do |faraday|
|
19
|
+
faraday.use Faraday::FollowRedirects::Middleware
|
15
20
|
faraday.adapter Faraday.default_adapter
|
16
21
|
end
|
17
22
|
end
|
18
23
|
end
|
19
|
-
end
|
24
|
+
end
|
data/lib/chgk_rating/error.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
class Error < StandardError
|
3
5
|
ClientError = Class.new(self)
|
@@ -26,10 +28,10 @@ module ChgkRating
|
|
26
28
|
NotImplemented = Class.new(ServerError)
|
27
29
|
|
28
30
|
ERRORS = {
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
31
|
+
400 => ChgkRating::Error::BadRequest,
|
32
|
+
404 => ChgkRating::Error::NotFound,
|
33
|
+
501 => ChgkRating::Error::NotImplemented
|
34
|
+
}.freeze
|
33
35
|
|
34
36
|
class << self
|
35
37
|
# Create a new error from an HTTP response
|
@@ -43,4 +45,4 @@ module ChgkRating
|
|
43
45
|
super(message)
|
44
46
|
end
|
45
47
|
end
|
46
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class Base < ChgkObject
|
@@ -24,7 +26,8 @@ module ChgkRating
|
|
24
26
|
# Set `force` to reload data even if it is already present.
|
25
27
|
def eager_load!(force = false)
|
26
28
|
return unless @lazy || force
|
27
|
-
|
29
|
+
|
30
|
+
extract_from raw_by(id)
|
28
31
|
@lazy = false
|
29
32
|
self
|
30
33
|
end
|
@@ -36,26 +39,31 @@ module ChgkRating
|
|
36
39
|
end
|
37
40
|
|
38
41
|
def self.no_lazy_support!
|
39
|
-
|
42
|
+
const_set :NO_LAZY_SUPPORT, true
|
40
43
|
undef_method :lazy
|
41
44
|
end
|
42
45
|
|
43
46
|
def to_h
|
44
|
-
self.class.attribute_mapping.
|
45
|
-
data =
|
46
|
-
data = mapping[:transform_down].call(data) if mapping.
|
47
|
-
memo[
|
48
|
-
memo
|
47
|
+
self.class.attribute_mapping.each_with_object({}) do |(attr, mapping), memo|
|
48
|
+
data = send attr
|
49
|
+
data = mapping[:transform_down].call(data) if mapping.key?(:transform_down)
|
50
|
+
memo[mapping[:raw_name]] = data
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
52
|
-
|
54
|
+
class << self
|
55
|
+
# Grab the attribute mapping for the class and its superclass
|
56
|
+
# (as superclass may present common mappings for multiple classes)
|
57
|
+
def attribute_mapping
|
58
|
+
return nil unless name
|
53
59
|
|
54
|
-
|
55
|
-
|
56
|
-
|
60
|
+
ChgkRating::AttributeMappings.find(name).
|
61
|
+
merge(ChgkRating::AttributeMappings.find(superclass.name))
|
62
|
+
end
|
57
63
|
end
|
58
64
|
|
65
|
+
private
|
66
|
+
|
59
67
|
def lazy_load?(params)
|
60
68
|
self.class.const_defined?(:NO_LAZY_SUPPORT) ? false : params[:lazy]
|
61
69
|
end
|
@@ -80,11 +88,22 @@ module ChgkRating
|
|
80
88
|
|
81
89
|
def extract_from(raw_data)
|
82
90
|
self.class.attribute_mapping.each do |attr, mapping|
|
83
|
-
data = raw_data
|
84
|
-
|
85
|
-
|
91
|
+
data = get_data_from raw_data, attr, mapping
|
92
|
+
next unless data
|
93
|
+
|
94
|
+
instance_variable_set "@#{attr}", transform_up(data, mapping)
|
86
95
|
end
|
87
96
|
end
|
97
|
+
|
98
|
+
def get_data_from(raw, attr, mapping)
|
99
|
+
raw.is_a?(self.class) ? raw.send(attr) : raw[mapping[:raw_name]]
|
100
|
+
end
|
101
|
+
|
102
|
+
def transform_up(data, mapping)
|
103
|
+
return data unless mapping.key?(:transform_up)
|
104
|
+
|
105
|
+
mapping[:transform_up].call(data)
|
106
|
+
end
|
88
107
|
end
|
89
108
|
end
|
90
|
-
end
|
109
|
+
end
|
@@ -1,6 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class Player < Base
|
6
|
+
# Returns rating for the current Player in a given release
|
7
|
+
#
|
8
|
+
# @return [ChgkRating::Models::PlayerRating] The requested rating.
|
9
|
+
# @param release_id [String or Integer] Release to load rating for.
|
10
|
+
def rating(release_id)
|
11
|
+
ChgkRating::Models::PlayerRating.new release_id, player: self
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns an array-like ratings collection for the current Player.
|
15
|
+
#
|
16
|
+
# @return [ChgkRating::Collection::PlayerRatings] The collection of ratings.
|
17
|
+
def ratings
|
18
|
+
ChgkRating::Collections::PlayerRatings.new player: self
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a collection of Tournaments that the current player participated at based on the given criteria
|
22
|
+
#
|
23
|
+
# @raise [ChgkRating::Error::NotFound] Error raised when nothing can be found based on the given criteria.
|
24
|
+
# @return [ChgkRating::Collection::PlayerTournaments] The collection of tournaments.
|
25
|
+
# @param season_id [String or Integer] Season to load tournaments for
|
26
|
+
# @option params [String or Integer] :page The requested page. Default is 1
|
27
|
+
def tournaments(season_id = nil)
|
28
|
+
ChgkRating::Collections::PlayerTournaments.new player: self, season_id: season_id
|
29
|
+
end
|
30
|
+
|
4
31
|
private
|
5
32
|
|
6
33
|
def api_path
|
@@ -8,4 +35,4 @@ module ChgkRating
|
|
8
35
|
end
|
9
36
|
end
|
10
37
|
end
|
11
|
-
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChgkRating
|
4
|
+
module Models
|
5
|
+
class PlayerRating < Rating
|
6
|
+
def initialize(release_id_or_hash, params = {})
|
7
|
+
@player_id = extract_id_from params[:player], ChgkRating::Models::Player
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def api_path
|
14
|
+
"players/#{@player_id}/rating"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,9 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
|
-
class
|
4
|
-
no_eager_loading!
|
5
|
-
no_lazy_support!
|
6
|
-
|
5
|
+
class TeamRating < Rating
|
7
6
|
def initialize(release_id_or_hash, params = {})
|
8
7
|
@team_id = extract_id_from params[:team]
|
9
8
|
super
|
@@ -16,4 +15,4 @@ module ChgkRating
|
|
16
15
|
end
|
17
16
|
end
|
18
17
|
end
|
19
|
-
end
|
18
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class Team < Base
|
@@ -19,19 +21,19 @@ module ChgkRating
|
|
19
21
|
ChgkRating::Models::TournamentTeam.new self, tournament: tournament_or_id, lazy: true
|
20
22
|
end
|
21
23
|
|
22
|
-
# Returns
|
24
|
+
# Returns rating for the current Team in a given release
|
23
25
|
#
|
24
|
-
# @return [ChgkRating::Models::
|
26
|
+
# @return [ChgkRating::Models::TeamRating] The requested rating.
|
25
27
|
# @param release_id [String or Integer] Release to load rating for.
|
26
28
|
def rating(release_id)
|
27
|
-
ChgkRating::Models::
|
29
|
+
ChgkRating::Models::TeamRating.new release_id, team: self
|
28
30
|
end
|
29
31
|
|
30
|
-
# Returns an array-like
|
32
|
+
# Returns an array-like ratings collection for the current Team.
|
31
33
|
#
|
32
|
-
# @return [ChgkRating::Collection::
|
34
|
+
# @return [ChgkRating::Collection::TeamRatings] The collection of ratings.
|
33
35
|
def ratings
|
34
|
-
ChgkRating::Collections::
|
36
|
+
ChgkRating::Collections::TeamRatings.new team: self
|
35
37
|
end
|
36
38
|
|
37
39
|
# Returns an hash-like Recaps collection for the current team, grouped by seasons. Seasons act
|
@@ -59,4 +61,4 @@ module ChgkRating
|
|
59
61
|
end
|
60
62
|
end
|
61
63
|
end
|
62
|
-
end
|
64
|
+
end
|
@@ -1,13 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class Tournament < Base
|
4
|
-
# Returns an array-like
|
6
|
+
# Returns an array-like TournamentTeamPlayers collection containing roster for a team at the current tournament.
|
5
7
|
#
|
6
8
|
# @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
|
7
|
-
# @return [ChgkRating::Collection::
|
9
|
+
# @return [ChgkRating::Collection::TournamentTeamPlayers] The collection of results.
|
8
10
|
# @param team_or_id [String, Integer or ChgkRating::Models::Team] Team to load players for.
|
9
11
|
def team_players(team_or_id)
|
10
|
-
ChgkRating::Collections::
|
12
|
+
ChgkRating::Collections::TournamentTeamPlayers.new tournament: self, team: team_or_id
|
11
13
|
end
|
12
14
|
|
13
15
|
# Returns an array-like TournamentTeamResults collection with results for a given team in the current
|
@@ -32,7 +34,7 @@ module ChgkRating
|
|
32
34
|
# @raise [ChgkRating::Error::NotFound] Error raised when the requested Team cannot be found.
|
33
35
|
# @return [ChgkRating::Models::TournamentTeam] The requested TournamentTeam.
|
34
36
|
# @param team_or_id [String, Integer or Team] Team to search for.
|
35
|
-
def
|
37
|
+
def team_by(team_or_id)
|
36
38
|
ChgkRating::Models::TournamentTeam.new team_or_id, tournament: self, lazy: true
|
37
39
|
end
|
38
40
|
|
@@ -43,4 +45,4 @@ module ChgkRating
|
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
46
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Models
|
3
5
|
class TournamentTeam < Base
|
@@ -11,11 +13,11 @@ module ChgkRating
|
|
11
13
|
super extract_id_from(team_or_hash), params
|
12
14
|
end
|
13
15
|
|
14
|
-
# Returns an array-like
|
16
|
+
# Returns an array-like TournamentTeamPlayers collection containing roster for the current TournamentTeam
|
15
17
|
#
|
16
|
-
# @return [ChgkRating::Collection::
|
18
|
+
# @return [ChgkRating::Collection::TournamentTeamPlayers] The collection of results.
|
17
19
|
def players
|
18
|
-
ChgkRating::Collections::
|
20
|
+
ChgkRating::Collections::TournamentTeamPlayers.new tournament: @tournament, team: @team
|
19
21
|
end
|
20
22
|
|
21
23
|
# Returns an array-like TournamentTeamResults collection containing results for the current TournamentTeam
|
@@ -26,4 +28,4 @@ module ChgkRating
|
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
29
|
-
end
|
31
|
+
end
|
data/lib/chgk_rating/request.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Request
|
3
5
|
include ChgkRating::Connection
|
@@ -16,17 +18,16 @@ module ChgkRating
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def respond(response)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
21
|
+
body = MultiJson.load response.body
|
22
|
+
raise MultiJson::ParseError if body.respond_to?(:has_key?) && body.key?('error')
|
23
|
+
|
24
|
+
body
|
25
|
+
rescue MultiJson::ParseError
|
26
|
+
respond_with_error response.status, response.body
|
26
27
|
end
|
27
28
|
|
28
29
|
def respond_with_error(code, body)
|
29
|
-
|
30
|
+
raise ChgkRating::Error::ERRORS[code].from_response(body)
|
30
31
|
end
|
31
32
|
end
|
32
|
-
end
|
33
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Initial code taken from Facets gem by Rubyworks
|
2
4
|
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
|
3
5
|
|
@@ -6,11 +8,11 @@ class String
|
|
6
8
|
# replaced by underscores.
|
7
9
|
def snakecase_upcase
|
8
10
|
split('::').last.
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
13
|
+
tr('-', '_').
|
14
|
+
gsub(/\s/, '_').
|
15
|
+
gsub(/__+/, '_').
|
16
|
+
upcase
|
15
17
|
end
|
16
|
-
end
|
18
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ChgkRating
|
2
4
|
module Utils
|
3
5
|
module Transformations
|
@@ -6,10 +8,10 @@ module ChgkRating
|
|
6
8
|
up = 'integer' if up.nil? || up.empty?
|
7
9
|
down = 'string' if down.nil? || down.empty?
|
8
10
|
|
9
|
-
%i
|
11
|
+
%i[up down].reduce({}) do |result, t|
|
10
12
|
current_transformer = binding.local_variable_get t
|
11
13
|
result.merge({
|
12
|
-
|
14
|
+
"transform_#{t}".to_sym => send(current_transformer)
|
13
15
|
})
|
14
16
|
end
|
15
17
|
end
|
@@ -24,10 +26,12 @@ module ChgkRating
|
|
24
26
|
private
|
25
27
|
|
26
28
|
def chgk_object(namespace, type = 'Models')
|
27
|
-
|
28
|
-
opts = type == 'Models'
|
29
|
-
|
30
|
-
|
29
|
+
lambda do |d|
|
30
|
+
opts = if type == 'Models'
|
31
|
+
[d, {lazy: true}]
|
32
|
+
else
|
33
|
+
[{collection: d, lazy: true}]
|
34
|
+
end
|
31
35
|
|
32
36
|
Module.const_get("ChgkRating::#{type}::#{namespace}").new(*opts)
|
33
37
|
end
|
@@ -42,42 +46,45 @@ module ChgkRating
|
|
42
46
|
end
|
43
47
|
|
44
48
|
def to_star(method = :to_s, iterate = false)
|
45
|
-
|
46
|
-
iterate
|
47
|
-
|
48
|
-
|
49
|
+
lambda do |d|
|
50
|
+
if iterate
|
51
|
+
d.map { |obj| obj.send method }
|
52
|
+
else
|
53
|
+
d.send(method)
|
54
|
+
end
|
49
55
|
end
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
53
59
|
TRANSFORMERS = {
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
60
|
+
string: to_star,
|
61
|
+
integer: to_star(:to_i),
|
62
|
+
float: to_star(:to_f),
|
63
|
+
id: to_star(:id),
|
64
|
+
ids: to_star(:id, true),
|
65
|
+
sym: to_star(:to_sym),
|
66
|
+
strdate: to_star(:to_s_chgk),
|
67
|
+
uri: ->(d) { URI.parse_safely d },
|
68
|
+
boolean: to_boolean,
|
69
|
+
binboolean: to_binary_boolean,
|
70
|
+
date: ->(d) { Date.parse_safely d },
|
71
|
+
datetime: ->(d) { DateTime.parse_safely d },
|
72
|
+
splitboolean: lambda do |d|
|
73
|
+
d&.split('')&.map { |result| to_boolean.call(result) }
|
74
|
+
end,
|
75
|
+
arraystrboolean: lambda do |d|
|
76
|
+
d&.map { |result| to_binary_boolean.call(result) }
|
77
|
+
end,
|
78
|
+
arrayboolean: lambda do |d|
|
79
|
+
d&.map { |result| to_boolean.call(result) }
|
80
|
+
end,
|
81
|
+
team: chgk_object('Team'),
|
82
|
+
tournament: chgk_object('Tournament'),
|
83
|
+
player: chgk_object('Player'),
|
84
|
+
players: chgk_object('Players', 'Collections')
|
78
85
|
}.freeze
|
79
86
|
|
80
87
|
load_transformers!
|
81
88
|
end
|
82
89
|
end
|
83
|
-
end
|
90
|
+
end
|
data/lib/chgk_rating/version.rb
CHANGED