jsonapi_parameters 0.0.4 → 0.1.0

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
  SHA256:
3
- metadata.gz: e3adf5c57a8cbc8326297a9de48e80790d8c40ee19f700684f55fddf09e032dd
4
- data.tar.gz: 95e53da4d7f1b29032e77e9943938b7edcd1d1d46965f838e437ceee81a3387d
3
+ metadata.gz: 140d60d07ed8488d9009e2c0e0757b1fbcf14596d79dc552c48c9212980985fd
4
+ data.tar.gz: 3480f41f044a29d8d2db32aa64ab10af535997fa5cb772ca1080372abb798e4c
5
5
  SHA512:
6
- metadata.gz: 43f61c0ed74c5361932b6176d6af51e1d2f7d5b3d3bd638d383323bfc2379393a5ccfb27f46a6422ca5928884c63bdb4390762f59748f61e486d0b9589f8fb5a
7
- data.tar.gz: e5cb09df6d9772d5cb854e82da2c8748d074c9413327a35bbb3ebc71d42eeac5f1eb63eb6f21fa4a45898f48c59f2b904556af82aa37e65330517ac40f96528d
6
+ metadata.gz: 8accb91454be6e686e29b4fbee76e9185df0dc2689ac50292d8098fa4f42fff9f4f8059a7c33aa389455776b7dcf8eb081ed0f472ef1e3fcd19a358f9875dac4
7
+ data.tar.gz: 39600f7081909c9bd5be6a16694e9db09f70b53e1e8d8126fa41fcacc9666232f68d1e16196993213cb943e7875fde0308ade0dfec889a625f254ff117ef2ad8
data/README.md CHANGED
@@ -56,9 +56,24 @@ With jsonapi_parameters, the difference is just the params:
56
56
 
57
57
  ```ruby
58
58
  def create_params
59
- params.to_jsonapi.require(:model).permit(:name)
59
+ params.from_jsonapi.require(:model).permit(:name)
60
60
  end
61
- ```
61
+ ```
62
+
63
+ #### Casing
64
+
65
+ If the input is in a different convention than `:snake`, you should specify that.
66
+
67
+ You can do it in two ways:
68
+ * in an initializer, simply create `initializers/jsonapi_parameters.rb` with contents similar to:
69
+ ```ruby
70
+ # config/initializers/jsonapi_parameters.rb
71
+
72
+ JsonApi::Parameters.ensure_underscore_translation = true
73
+
74
+ ```
75
+
76
+ * while calling `.from_jsonapi`, for instance: `.from_jsonapi(:camel)`. **The value does not really matter, as anything different than `:snake` will result in deep keys transformation provided by [ActiveSupport](https://apidock.com/rails/v4.1.8/Hash/deep_transform_keys).**
62
77
 
63
78
  ### Plain Ruby / outside Rails
64
79
 
@@ -76,5 +91,14 @@ translator = Translator.new
76
91
  translator.jsonapify(params)
77
92
  ```
78
93
 
94
+ #### Casing
95
+
96
+ If the input is in a different convention than `:snake`, you should specify that.
97
+
98
+ You can do it in two ways:
99
+
100
+ * by a global setting: `JsonApi::Parameters.ensure_underscore_translation = true`
101
+ * while calling `.jsonapify`, for instance: `.jsonapify(params, naming_convention: :camel)`. **The value does not really matter, as anything different than `:snake` will result in deep keys transformation provided by [ActiveSupport](https://apidock.com/rails/v4.1.8/Hash/deep_transform_keys).**
102
+
79
103
  ## License
80
104
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -3,7 +3,13 @@ require 'action_controller'
3
3
  class ActionController::Parameters
4
4
  include JsonApi::Parameters
5
5
 
6
- def to_jsonapi
7
- @to_jsonapi ||= self.class.new jsonapify(self)
6
+ def to_jsonapi(*args)
7
+ warn "WARNING: #to_jsonapi method is deprecated. Please use #from_jsonapi instead.\n#{caller(1..1).first}"
8
+
9
+ from_jsonapi(*args)
10
+ end
11
+
12
+ def from_jsonapi(naming_convention = :snake)
13
+ @from_jsonapi ||= self.class.new jsonapify(self, naming_convention: naming_convention)
8
14
  end
9
15
  end
@@ -1,4 +1,9 @@
1
1
  module JsonApi
2
2
  module Parameters
3
+ @ensure_underscore_translation = false
4
+
5
+ class << self
6
+ attr_accessor :ensure_underscore_translation
7
+ end
3
8
  end
4
9
  end
@@ -1,16 +1,20 @@
1
1
  module JsonApi::Parameters
2
- def jsonapify(params)
3
- jsonapi_translate(params)
2
+ def jsonapify(params, naming_convention: :snake)
3
+ jsonapi_translate(params, naming_convention: naming_convention)
4
4
  end
5
5
 
6
6
  private
7
7
 
8
- def jsonapi_translate(params)
8
+ def jsonapi_translate(params, naming_convention:)
9
9
  params = params.to_unsafe_h if params.is_a?(ActionController::Parameters)
10
10
 
11
11
  return params if params.nil? || params.empty?
12
12
 
13
- @jsonapi_unsafe_hash = params.deep_symbolize_keys
13
+ @jsonapi_unsafe_hash = if naming_convention != :snake || JsonApi::Parameters.ensure_underscore_translation
14
+ params.deep_transform_keys { |key| key.to_s.underscore.to_sym }
15
+ else
16
+ params.deep_symbolize_keys
17
+ end
14
18
 
15
19
  formed_parameters
16
20
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApi::Parameters
2
- VERSION = '0.0.4'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,29 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
+ - Visuality
7
8
  - marahin
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2019-01-17 00:00:00.000000000 Z
12
+ date: 2019-04-24 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- name: rails
15
+ name: activesupport
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
18
  - - ">="
18
19
  - !ruby/object:Gem::Version
19
- version: 4.2.11
20
+ version: 4.1.8
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - ">="
25
26
  - !ruby/object:Gem::Version
26
- version: 4.2.11
27
+ version: 4.1.8
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
27
42
  - !ruby/object:Gem::Dependency
28
43
  name: sqlite3
29
44
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +53,34 @@ dependencies:
38
53
  - - "~>"
39
54
  - !ruby/object:Gem::Version
40
55
  version: 1.3.13
56
+ - !ruby/object:Gem::Dependency
57
+ name: database_cleaner
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 1.7.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.7.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: rails
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 4.1.8
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 4.1.8
41
84
  - !ruby/object:Gem::Dependency
42
85
  name: rspec
43
86
  requirement: !ruby/object:Gem::Requirement
@@ -181,6 +224,7 @@ dependencies:
181
224
  description: JsonApi::Parameters allows you to easily translate JSON:API compliant
182
225
  parameters to a structure expected by Rails.
183
226
  email:
227
+ - contact@visuality.pl
184
228
  - me@marahin.pl
185
229
  executables: []
186
230
  extensions: []