jsonschema_serializer 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1eb0905f97429a33e3a48f272ac469ed4bd674c
4
- data.tar.gz: e5e879561a2e825f7a55c68046a388e735ca60ff
3
+ metadata.gz: 21bc2f5a22f0f4da198b981fd6be8aade090d4fa
4
+ data.tar.gz: 9d08bc5bf5c24b2bd5cec7ac1f7dfb8914374708
5
5
  SHA512:
6
- metadata.gz: 332c7b8572a005b88a2fcc81b422deef2fe02c7415b3d18771c2235f7f6b138ce075a7eea28185cf8d9f76f8974c5fe09021f2a9e9a5ef5cc9841350af1c04cd
7
- data.tar.gz: 9cb94fd3d015da0a8d0cbf5436be5de272cbe3c8334c67b37373fac51a0557b0330e1d0ee81addeb6bc2e85d132ea4e415cd830d3b0be883420c6a29a01e6c64
6
+ metadata.gz: 55d072167b46b7999861a0d6c152753ca46a490b3f3e0386867dfc6d084c9cfc287af7acf4248f6e6039d74ab84133c59f11e19ee79b2fe52caa0c259875f62f
7
+ data.tar.gz: 21b742e2afb0e3598667b4f3e9c75017cdf0096dc7a51808d99a1b4b669b256be42c9ebec4e7df479f47e92c3dcf552010ec3d0fa36dd3256428258e8026f0fc
data/CHANGELOG.md CHANGED
@@ -1,19 +1,27 @@
1
1
  # Changelog
2
2
 
3
- # v 0.0.4 (2018-05-22)
3
+ ## v 0.0.5 (2018-05-23)
4
+
5
+ - json pretty generation by default
6
+ - added a Rails rake task example in the README.md
7
+ - added schema `title` and `required` attributes
8
+ - refactored `from_active_record` method in `from_model`
9
+ - manipulate a dup of table columns
10
+
11
+ ## v 0.0.4 (2018-05-22)
4
12
 
5
13
  - corrected `ActiveRecord::ConnectionAdapters::SqlTypeMetadata` reference in `JsonschemaSerializer::ActiveRecord`
6
14
 
7
- # v 0.0.3 (2018-05-21)
15
+ ## v 0.0.3 (2018-05-21)
8
16
 
9
17
  - improved documentation on existing code base
10
18
  - refactored `JsonschemaSerializer::ActiveRecord` as a `class`
11
19
 
12
- # v 0.0.2 (2018-05-20)
20
+ ## v 0.0.2 (2018-05-20)
13
21
 
14
22
  - basic rDoc comments for documentation
15
23
 
16
- # v 0.0.1 (2018-05-20)
24
+ ## v 0.0.1 (2018-05-20)
17
25
 
18
26
  - basic implementation of `JsonschemaSerializer::Builder`
19
27
  - POC implementation of a `JsonschemaSerializer::ActiveRecord` module
data/README.md CHANGED
@@ -61,9 +61,9 @@ You can alternatively use an experimental builder for `ActiveRecord`
61
61
  ```ruby
62
62
  serializer = JsonschemaSerializer::ActiveRecord
63
63
 
64
- schema = serializer.from_active_record(MyActiveRecordClass)
65
- schema = serializer.from_active_record(MyActiveRecordClass, only: %[desired1 desired2])
66
- schema = serializer.from_active_record(MyActiveRecordClass, except: %[ignored1 ignored2])
64
+ schema = serializer.from_model(MyActiveRecordClass)
65
+ schema = serializer.from_model(MyActiveRecordClass, only: %[desired1 desired2])
66
+ schema = serializer.from_model(MyActiveRecordClass, except: %[ignored1 ignored2])
67
67
 
68
68
  # You can manipulate the resulting schema
69
69
 
@@ -75,6 +75,38 @@ end
75
75
  schema.to_json
76
76
  ```
77
77
 
78
+ ## Rails
79
+
80
+ At this stage, a first usage within a Rails application could be a rake task dumping the schemas inside the public folder.
81
+
82
+ ```rb
83
+ # lib/tasks/jsonschemas.rake
84
+ require 'fileutils'
85
+
86
+ task dump_jsonschemas: :environment do
87
+ Rails.application.eager_load!
88
+ models = ActiveRecord::Base.descendants
89
+
90
+ output_path = Rails.root.join('public', 'data')
91
+ # Ensure that the destination path exists
92
+ FileUtils.mkdir_p(output_path)
93
+
94
+ models.each do |model|
95
+ # Skip abstract classes
96
+ if model.table_name
97
+ file_path = output_path.join("#{model.model_name.param_key}.json")
98
+ schema = JsonschemaSerializer::ActiveRecord.from_model(model)
99
+
100
+ puts "Creating #{file_path} ..."
101
+ File.open(file_path, 'w') { |f| f.write(schema.to_json) }
102
+ else
103
+ puts "Skipping abstract class #{model.to_s} ..."
104
+ end
105
+ end
106
+ end
107
+
108
+ ```
109
+
78
110
  ## Development
79
111
 
80
112
  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.
@@ -84,4 +116,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
84
116
  ## Contributing
85
117
 
86
118
  Bug reports and pull requests are welcome on GitHub at https://github.com/mberlanda/jsonschema_serializer.
87
- ygy
@@ -1,8 +1,8 @@
1
1
  require_relative 'builder'
2
2
 
3
3
  module JsonschemaSerializer
4
- # The +JsonschemaSerializer::Activerecord+ class provides
5
- # a +from_active_record+ class method to serialize some
4
+ # The +JsonschemaSerializer::ActiveRecord+ class provides
5
+ # a +from_model+ class method to serialize some
6
6
  # ActiveRecord classes with the minimum effort
7
7
  class ActiveRecord
8
8
  class << self
@@ -14,36 +14,105 @@ module JsonschemaSerializer
14
14
  # +only+:: +Array+ columns as +String+
15
15
  # +except+:: +Array+ columns as +String+
16
16
 
17
- def from_active_record(klass, only: nil, except: nil)
17
+ def from_model(klass, only: nil, except: nil)
18
18
  validate_arguments(only, except)
19
19
  JsonschemaSerializer::Builder.build do |b|
20
- selected_columns(klass, only, except).each do |col|
21
- b.properties.tap do |prop|
22
- el = format_column_element(col)
23
- # Handle basic case of attribute type and attribute name
24
- prop.merge! b.send(el[:type], el[:name])
25
- end
26
- end
20
+ format_schema_attributes(klass, b)
21
+ format_schema_properties(selected_columns(klass), b)
27
22
  end
28
23
  end
29
24
 
30
- private
31
-
32
25
  # Raise if +only+ and +except+ are both provided
26
+ #
27
+ # Params:
28
+ # +only+:: +Array+ columns as +String+
29
+ # +except+:: +Array+ columns as +String+
30
+
33
31
  def validate_arguments(only, except)
34
32
  raise ArgumentError, 'only and except options both provided' if only && except
33
+ @only = only
34
+ @except = except
35
+ end
36
+
37
+ # Format JsonSchema general attributes such as title, required
38
+ #
39
+ # Params:
40
+ # +klass+:: +ActiveRecord::Base+ class name
41
+ # +builder+:: +JsonschemaSerializer::Builder+ an instance of the builder
42
+
43
+ def format_schema_attributes(klass, builder)
44
+ builder.title schema_title(klass)
45
+ required(klass).tap do |required|
46
+ builder.required required unless required.empty?
47
+ end
48
+ end
49
+
50
+ # Format JsonSchema properties
51
+ #
52
+ # Params:
53
+ # +columns+:: +Array+ array of formatted
54
+ # +builder+:: +JsonschemaSerializer::Builder+ an instance of the builder
55
+
56
+ def format_schema_properties(columns, builder)
57
+ builder.properties.tap do |prop|
58
+ columns.each do |col|
59
+ el = format_column_element(col)
60
+ # Handle basic case of attribute type and attribute name
61
+ prop.merge! builder.send(el[:type], el[:name])
62
+ end
63
+ end
64
+ end
65
+
66
+ # Extract schema title from ActiveRecord class
67
+ # This method can be overridden when inheriting from this class
68
+ #
69
+ # Params:
70
+ # +klass+:: +ActiveRecord::Base+ class name
71
+
72
+ def schema_title(klass)
73
+ klass.model_name.human
74
+ end
75
+
76
+ # Filter required ActiveRecord class implementation with only/except
77
+ #
78
+ # Params:
79
+ # +columns+:: +Array+ column names as +Symbol+
80
+
81
+ # Extract required attributes from ActiveRecord class implementation
82
+ # This method can be overridden when inheriting from this class
83
+ #
84
+ # Params:
85
+ # +klass+:: +ActiveRecord::Base+ class name
86
+ def required(klass)
87
+ required_from_class(klass).tap do |req|
88
+ return req & @only if @only
89
+ return req - @except if @except
90
+ end
91
+ end
92
+
93
+ def required_from_class(klass)
94
+ klass.validators.select do |validator|
95
+ validator.class.to_s == 'ActiveRecord::Validations::PresenceValidator'
96
+ end.map(&:attributes).flatten
35
97
  end
36
98
 
37
99
  # Retrieves the columns and keep/discard some elements if needed
38
- def selected_columns(klass, only, except)
39
- klass.columns.tap do |cols|
40
- cols.select! { |col| only.include?(col.name) } if only
41
- cols.reject! { |col| except.include?(col.name) } if except
100
+ #
101
+ # Params:
102
+ # +klass+:: +ActiveRecord::Base+ class name
103
+ # +only+:: +Array+ columns as +String+
104
+ # +except+:: +Array+ columns as +String+
105
+
106
+ def selected_columns(klass)
107
+ klass.columns.dup.tap do |cols|
108
+ cols.select! { |col| @only.include?(col.name) } if @only
109
+ cols.reject! { |col| @except.include?(col.name) } if @except
42
110
  end
43
111
  end
44
112
 
45
113
  # Mapping Ruby types on Jsonschema types.
46
114
  # This could be moved to a separate module later
115
+
47
116
  TYPE_CONVERSIONS = {
48
117
  boolean: :boolean,
49
118
  datetime: :string,
@@ -54,6 +123,10 @@ module JsonschemaSerializer
54
123
  }.freeze
55
124
 
56
125
  # Format a ActiveRecord::ConnectionAdapters::<Adapter>::Column as an Hash
126
+ #
127
+ # Params:
128
+ # +klass+:: +ActiveRecord::ConnectionAdapters::<Adapter>::Column+ column
129
+
57
130
  def format_column_element(col)
58
131
  {}.tap do |h|
59
132
  h[:name] = col.name
@@ -62,7 +135,11 @@ module JsonschemaSerializer
62
135
  end
63
136
  end
64
137
 
65
- # Format a ActiveRecord::ConnectionAdapters::SqlTypeMetadata as an Hash
138
+ # Retrieves type from ActiveRecord::ConnectionAdapters::SqlTypeMetadata
139
+ #
140
+ # Params:
141
+ # +col+:: +ActiveRecord::ConnectionAdapters::<Adapter>::Column+ column
142
+
66
143
  def sql_type(col)
67
144
  col.sql_type_metadata.type
68
145
  end
@@ -23,8 +23,13 @@ module JsonschemaSerializer
23
23
  end
24
24
 
25
25
  # The +to_json+ method exports the schema as a json string
26
- def to_json
27
- @schema.to_json
26
+ # By default it would exported with a pretty print
27
+ #
28
+ # Params:
29
+ # +pretty+:: +Boolean+
30
+
31
+ def to_json(pretty: true)
32
+ pretty ? JSON.pretty_generate(@schema) : @schema.to_json
28
33
  end
29
34
 
30
35
  # Assigns the +title+ to the root schema object
@@ -193,7 +198,7 @@ module JsonschemaSerializer
193
198
  # end
194
199
  # b.array :subscribers, items: subscriber
195
200
  # end
196
- #
201
+
197
202
  def array(name, items:, **opts)
198
203
  {
199
204
  name => { type: :array, items: items }.merge(opts)
@@ -1,3 +1,3 @@
1
1
  module JsonschemaSerializer
2
- VERSION = '0.0.4'.freeze # :nodoc:
2
+ VERSION = '0.0.5'.freeze # :nodoc:
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonschema_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mauro Berlanda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-22 00:00:00.000000000 Z
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler