rest_framework 0.0.16 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +16 -9
- data/lib/rest_framework.rb +3 -0
- data/lib/rest_framework/VERSION_STAMP +1 -1
- data/lib/rest_framework/controller_mixins.rb +4 -0
- data/lib/rest_framework/controller_mixins/base.rb +154 -166
- data/lib/rest_framework/controller_mixins/models.rb +215 -169
- data/lib/rest_framework/errors.rb +26 -0
- data/lib/rest_framework/filters.rb +53 -50
- data/lib/rest_framework/generators.rb +5 -0
- data/lib/rest_framework/generators/controller_generator.rb +26 -0
- data/lib/rest_framework/paginators.rb +76 -65
- data/lib/rest_framework/routers.rb +93 -44
- data/lib/rest_framework/serializers.rb +131 -93
- data/lib/rest_framework/version.rb +11 -9
- metadata +8 -5
@@ -1,125 +1,163 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
class RESTFramework::BaseSerializer
|
2
|
+
def initialize(object: nil, controller: nil, **kwargs)
|
3
|
+
@object = object
|
4
|
+
@controller = controller
|
5
|
+
end
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
@controller = controller
|
8
|
-
end
|
7
|
+
def serialize
|
8
|
+
raise NotImplementedError
|
9
9
|
end
|
10
|
+
end
|
10
11
|
|
11
|
-
# This serializer uses `.as_json` to serialize objects. Despite the name, `.as_json` is a Rails
|
12
|
-
# method which converts objects to Ruby primitives (with the top-level being either an array or a
|
13
|
-
# hash).
|
14
|
-
class NativeSerializer < BaseSerializer
|
15
|
-
class_attribute :config
|
16
|
-
class_attribute :singular_config
|
17
|
-
class_attribute :plural_config
|
18
|
-
class_attribute :action_config
|
19
|
-
|
20
|
-
def initialize(many: nil, **kwargs)
|
21
|
-
super(**kwargs)
|
22
|
-
@many = many
|
23
|
-
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
13
|
+
# This serializer uses `.serializable_hash` to convert objects to Ruby primitives (with the
|
14
|
+
# top-level being either an array or a hash).
|
15
|
+
class RESTFramework::NativeSerializer < RESTFramework::BaseSerializer
|
16
|
+
class_attribute :config
|
17
|
+
class_attribute :singular_config
|
18
|
+
class_attribute :plural_config
|
19
|
+
class_attribute :action_config
|
20
|
+
|
21
|
+
def initialize(many: nil, model: nil, **kwargs)
|
22
|
+
super(**kwargs)
|
23
|
+
|
24
|
+
if many.nil?
|
25
|
+
# Determine if we are dealing with many objects or just one.
|
26
|
+
@many = @object.is_a?(Enumerable)
|
27
|
+
else
|
28
|
+
@many = many
|
28
29
|
end
|
29
30
|
|
30
|
-
#
|
31
|
-
|
32
|
-
|
31
|
+
# Determine model either explicitly, or by inspecting @object or @controller.
|
32
|
+
@model = model
|
33
|
+
@model ||= @object.class if @object.is_a?(ActiveRecord::Base)
|
34
|
+
@model ||= @object[0].class if @many && @object[0].is_a?(ActiveRecord::Base)
|
35
|
+
@model ||= @controller.send(:get_model) if @controller
|
36
|
+
end
|
33
37
|
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
# Get controller action, if possible.
|
39
|
+
def get_action
|
40
|
+
return @controller&.action_name&.to_sym
|
41
|
+
end
|
37
42
|
|
38
|
-
|
39
|
-
|
43
|
+
# Get a locally defined native serializer configuration, if one is defined.
|
44
|
+
def get_local_native_serializer_config
|
45
|
+
action = self.get_action
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
if action && self.action_config
|
48
|
+
# Index action should use :list serializer config if :index is not provided.
|
49
|
+
action = :list if action == :index && !self.action_config.key?(:index)
|
44
50
|
|
45
|
-
|
46
|
-
return self.config
|
51
|
+
return self.action_config[action] if self.action_config[action]
|
47
52
|
end
|
48
53
|
|
49
|
-
#
|
50
|
-
|
51
|
-
|
52
|
-
if local_config = self.get_local_serializer_config
|
53
|
-
return local_config
|
54
|
-
end
|
54
|
+
# No action_config, so try singular/plural config if explicitly instructed to via @many.
|
55
|
+
return self.plural_config if @many == true && self.plural_config
|
56
|
+
return self.singular_config if @many == false && self.singular_config
|
55
57
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
58
|
+
# Lastly, try returning the default config, or singular/plural config in that order.
|
59
|
+
return self.config || self.singular_config || self.plural_config
|
60
|
+
end
|
60
61
|
|
61
|
-
|
62
|
-
|
62
|
+
# Helper to get a native serializer configuration from the controller.
|
63
|
+
def get_controller_native_serializer_config
|
64
|
+
return nil unless @controller
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
return @object.as_json(self.get_serializer_config)
|
69
|
-
end
|
70
|
-
return nil
|
66
|
+
if @many == true
|
67
|
+
controller_serializer = @controller.try(:native_serializer_plural_config)
|
68
|
+
elsif @many == false
|
69
|
+
controller_serializer = @controller.try(:native_serializer_singular_config)
|
71
70
|
end
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
72
|
+
return controller_serializer || @controller.try(:native_serializer_config)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Get a configuration passable to `serializable_hash` for the object.
|
76
|
+
def get_serializer_config
|
77
|
+
# Return a locally defined serializer config if one is defined.
|
78
|
+
if local_config = self.get_local_native_serializer_config
|
79
|
+
return local_config
|
79
80
|
end
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
return @_nested_config[key] = value
|
81
|
+
|
82
|
+
# Return a serializer config if one is defined on the controller.
|
83
|
+
if serializer_config = get_controller_native_serializer_config
|
84
|
+
return serializer_config
|
85
85
|
end
|
86
86
|
|
87
|
-
#
|
88
|
-
|
89
|
-
|
90
|
-
|
87
|
+
# If the config wasn't determined, build a serializer config from model fields.
|
88
|
+
fields = @controller.send(:get_fields) if @controller
|
89
|
+
if fields
|
90
|
+
if @model
|
91
|
+
columns, methods = fields.partition { |f| f.in?(@model.column_names) }
|
92
|
+
else
|
93
|
+
columns = fields
|
94
|
+
methods = []
|
91
95
|
end
|
92
|
-
|
96
|
+
|
97
|
+
return {only: columns, methods: methods}
|
93
98
|
end
|
94
|
-
|
95
|
-
|
96
|
-
|
99
|
+
|
100
|
+
# By default, pass an empty configuration, allowing the serialization of all columns.
|
101
|
+
return {}
|
102
|
+
end
|
103
|
+
|
104
|
+
# Convert the object (record or recordset) to Ruby primitives.
|
105
|
+
def serialize
|
106
|
+
if @object
|
107
|
+
begin
|
108
|
+
if @object.is_a?(Enumerable)
|
109
|
+
return @object.map { |r| r.serializable_hash(self.get_serializer_config) }
|
110
|
+
end
|
111
|
+
return @object.serializable_hash(self.get_serializer_config)
|
112
|
+
rescue NoMethodError
|
97
113
|
end
|
98
|
-
return @_nested_config[key] = value
|
99
114
|
end
|
115
|
+
|
116
|
+
# Raise an error if we cannot serialize the object.
|
117
|
+
raise RESTFramework::UnserializableError.new(@object)
|
100
118
|
end
|
101
119
|
|
102
|
-
#
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
120
|
+
# Allow a serializer instance to be used as a hash directly in a nested serializer config.
|
121
|
+
def [](key)
|
122
|
+
unless instance_variable_defined?(:@_nested_config)
|
123
|
+
@_nested_config = self.get_serializer_config
|
124
|
+
end
|
125
|
+
return @_nested_config[key]
|
126
|
+
end
|
127
|
+
def []=(key, value)
|
128
|
+
unless instance_variable_defined?(:@_nested_config)
|
129
|
+
@_nested_config = self.get_serializer_config
|
108
130
|
end
|
131
|
+
return @_nested_config[key] = value
|
132
|
+
end
|
109
133
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
134
|
+
# Allow a serializer class to be used as a hash directly in a nested serializer config.
|
135
|
+
def self.[](key)
|
136
|
+
unless instance_variable_defined?(:@_nested_config)
|
137
|
+
@_nested_config = self.new.get_serializer_config
|
138
|
+
end
|
139
|
+
return @_nested_config[key]
|
140
|
+
end
|
141
|
+
def self.[]=(key, value)
|
142
|
+
unless instance_variable_defined?(:@_nested_config)
|
143
|
+
@_nested_config = self.new.get_serializer_config
|
144
|
+
end
|
145
|
+
return @_nested_config[key] = value
|
146
|
+
end
|
147
|
+
end
|
114
148
|
|
115
|
-
# If the config wasn't determined, build a serializer config from model fields.
|
116
|
-
fields = @controller.try(:get_fields) if @controller
|
117
|
-
unless fields.blank?
|
118
|
-
columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) }
|
119
|
-
return {only: columns, methods: methods}
|
120
|
-
end
|
121
149
|
|
122
|
-
|
123
|
-
|
150
|
+
# :nocov:
|
151
|
+
# Alias NativeModelSerializer -> NativeSerializer.
|
152
|
+
class RESTFramework::NativeModelSerializer < RESTFramework::NativeSerializer
|
153
|
+
def initialize(**kwargs)
|
154
|
+
super
|
155
|
+
ActiveSupport::Deprecation.warn(
|
156
|
+
<<~MSG.split("\n").join(' ')
|
157
|
+
RESTFramework::NativeModelSerializer is deprecated and will be removed in future versions of
|
158
|
+
REST Framework; you should use RESTFramework::NativeSerializer instead.
|
159
|
+
MSG
|
160
|
+
)
|
124
161
|
end
|
125
162
|
end
|
163
|
+
# :nocov:
|
@@ -2,19 +2,21 @@ module RESTFramework
|
|
2
2
|
module Version
|
3
3
|
@_version = nil
|
4
4
|
|
5
|
-
def self.get_version
|
5
|
+
def self.get_version(skip_git: false)
|
6
6
|
# Return cached @_version, if available.
|
7
7
|
return @_version if @_version
|
8
8
|
|
9
9
|
# First, attempt to get the version from git.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
unless skip_git
|
11
|
+
begin
|
12
|
+
version = `git describe 2>/dev/null`.strip
|
13
|
+
raise "blank version" if version.nil? || version.match(/^\w*$/)
|
14
|
+
# Check for local changes.
|
15
|
+
changes = `git status --porcelain 2>/dev/null`
|
16
|
+
version << '.localchanges' if changes.strip.length > 0
|
17
|
+
return version
|
18
|
+
rescue
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
22
|
# Git failed, so try to find a VERSION_STAMP.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_framework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory N. Schmit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -43,16 +43,19 @@ files:
|
|
43
43
|
- lib/rest_framework/controller_mixins/base.rb
|
44
44
|
- lib/rest_framework/controller_mixins/models.rb
|
45
45
|
- lib/rest_framework/engine.rb
|
46
|
+
- lib/rest_framework/errors.rb
|
46
47
|
- lib/rest_framework/filters.rb
|
48
|
+
- lib/rest_framework/generators.rb
|
49
|
+
- lib/rest_framework/generators/controller_generator.rb
|
47
50
|
- lib/rest_framework/paginators.rb
|
48
51
|
- lib/rest_framework/routers.rb
|
49
52
|
- lib/rest_framework/serializers.rb
|
50
53
|
- lib/rest_framework/version.rb
|
51
|
-
homepage: https://
|
54
|
+
homepage: https://rails-rest-framework.com
|
52
55
|
licenses:
|
53
56
|
- MIT
|
54
57
|
metadata:
|
55
|
-
homepage_uri: https://
|
58
|
+
homepage_uri: https://rails-rest-framework.com
|
56
59
|
source_code_uri: https://github.com/gregschmit/rails-rest-framework
|
57
60
|
post_install_message:
|
58
61
|
rdoc_options: []
|
@@ -70,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
73
|
- !ruby/object:Gem::Version
|
71
74
|
version: '0'
|
72
75
|
requirements: []
|
73
|
-
rubygems_version: 3.0.
|
76
|
+
rubygems_version: 3.0.9
|
74
77
|
signing_key:
|
75
78
|
specification_version: 4
|
76
79
|
summary: A framework for DRY RESTful APIs in Ruby on Rails.
|