onsi 0.4.0 → 0.5.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: 11cdb10f50e5e342639f7957e3e2cf867decc2773065232c3a880983936a5122
4
- data.tar.gz: f37f95aaddd5d3736a7f165b4362876ef7a7a60865bae5183b7b850ba4ad6746
3
+ metadata.gz: 298c09a10a76e2ae896cd679cc6db43ae6da637f6048c572722c1285d5616fd9
4
+ data.tar.gz: c58a5048a9795d2b8a260f615915837d240e59809864088d67900e2e290bf38c
5
5
  SHA512:
6
- metadata.gz: fce49cd88116a547bd44bdc835b5def7a2dc5d54b07360d0185bf99008ba66c02a7821dadcc77cf9909bc62549ce57af5d30fd27c1eed9068d67041bdd943638
7
- data.tar.gz: 452661c725d56fc43dd03a16499321614063830a4cfc6d4af29fb122d6c8349e71f4d122d8d56637fa80eaee80bc89be3a36e55a7aa6b20415db83ffaae6e2e4
6
+ metadata.gz: 8255c49dda505912b3378d72a1ecd0f568141b9551f11ede3ec5df78b5dcf3ceee7b07130b8d0573fda914e8affb2c92b9a75235b5423cf93d4cb1e141eda7e7
7
+ data.tar.gz: 9330c0a7162ac3100d0079d1cfc241449b83e715ca5591f8b7005c794a62c0671379a96a1f10f0bff7f8783fdc5e7f7b35a6f83efd3c024a1100b082e31546a5
@@ -1,6 +1,8 @@
1
1
  require 'active_support/concern'
2
2
 
3
3
  module Onsi
4
+ ##
5
+ # Handles default errors and builds JSON-API responses.
4
6
  module ErrorResponder
5
7
  extend ActiveSupport::Concern
6
8
 
@@ -99,6 +101,7 @@ module Onsi
99
101
  return nil unless Rails.configuration.consider_all_requests_local
100
102
  {
101
103
  exception: {
104
+ '_note' => '`exception` will be removed if Rails.configuration.consider_all_requests_local is false',
102
105
  class: error.class.name,
103
106
  message: error.message,
104
107
  backtrace: error.backtrace
data/lib/onsi/params.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  module Onsi
2
+ ##
3
+ # Used to handle parsing JSON-API formated params
2
4
  class Params
5
+ ##
6
+ # Raised when using `Params#safe_fetch`
7
+ #
8
+ # The ErrorResponder will rescue from this and return an appropriate
9
+ # error to the user
3
10
  class RelationshipNotFound < StandardError
4
11
  attr_reader :key
5
12
 
@@ -9,6 +16,11 @@ module Onsi
9
16
  end
10
17
  end
11
18
 
19
+ ##
20
+ # Raised when a required attribute has a nil value. `Params#require`
21
+ #
22
+ # The ErrorResponder will rescue from this and return an appropriate
23
+ # error to the user
12
24
  class MissingReqiredAttribute < StandardError
13
25
  attr_reader :attribute
14
26
 
@@ -68,14 +80,22 @@ module Onsi
68
80
  @relationships = relationships
69
81
  end
70
82
 
83
+ ##
84
+ # Flatten an merge the attributes & relationships into one hash.
71
85
  def flatten
72
- attributes.to_h.merge(relationships.to_h).with_indifferent_access
86
+ attrs_hash.to_h.merge(relationships.to_h).with_indifferent_access
73
87
  end
74
88
 
89
+ ##
90
+ # Fetch a value from the attributes or return the passed default value
75
91
  def fetch(key, default = nil)
76
92
  attrs_hash[key] || default
77
93
  end
78
94
 
95
+ ##
96
+ # Make an attributes key required.
97
+ #
98
+ # Throws MissingReqiredAttribute if the value is nil
79
99
  def require(key)
80
100
  value = attrs_hash[key]
81
101
  if value.nil?
@@ -84,16 +104,81 @@ module Onsi
84
104
  value
85
105
  end
86
106
 
107
+ ##
108
+ # Handle finding a relationship's object
109
+ #
110
+ # If an ActiveRecord::RecordNotFound is raised, a RelationshipNotFound error will
111
+ # be raised so the ErrorResponder can build an appropriate error message
112
+ #
113
+ # params.safe_fetch(:person) do |id|
114
+ # Person.find(id)
115
+ # end
87
116
  def safe_fetch(key)
88
117
  yield(@relationships[key])
89
118
  rescue ActiveRecord::RecordNotFound
90
119
  raise RelationshipNotFound.new("Can't find relationship #{key}", key)
91
120
  end
92
121
 
122
+ ##
123
+ # Perform a transform on the value
124
+ #
125
+ # Any getter will run the value through the transform block.
126
+ #
127
+ # (The values are memoized)
128
+ #
129
+ # `params.transform(:date) { |date| Time.parse(date) }`
130
+ def transform(key, &block)
131
+ @attrs_hash = nil
132
+ transforms[key.to_sym] = block
133
+ end
134
+
135
+ ##
136
+ # Set a default value.
137
+ #
138
+ # This value will only be used if the key is missing from the passed attributes
139
+ #
140
+ # Can take any object. If the object responds to call (Lambda) it will be called when
141
+ # parsing attributes
142
+ def default(key, value)
143
+ @attrs_hash = nil
144
+ defaults[key.to_sym] = value
145
+ end
146
+
93
147
  private
94
148
 
149
+ def transforms
150
+ @transforms ||= {}
151
+ end
152
+
153
+ def defaults
154
+ @defaults ||= {}
155
+ end
156
+
95
157
  def attrs_hash
96
- @attrs_hash ||= attributes.to_h.with_indifferent_access
158
+ @attrs_hash ||= transform_attributes.merge(default_attributes).with_indifferent_access
159
+ end
160
+
161
+ def transform_attributes
162
+ attributes.to_h.each_with_object({}) do |(key, value), object|
163
+ transform = transforms[key.to_sym]
164
+ if transform
165
+ object[key] = transform.call(value)
166
+ else
167
+ object[key] = value
168
+ end
169
+ end
170
+ end
171
+
172
+ def default_attributes
173
+ raw_attrs = attributes.to_h.symbolize_keys
174
+ defaults.each_with_object({}) do |(key, value), object|
175
+ next if raw_attrs.key?(key)
176
+ if value.respond_to?(:call)
177
+ object[key] = value.call
178
+ else
179
+ object[key] = value
180
+ end
181
+ end
97
182
  end
98
183
  end
99
184
  end
data/lib/onsi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Onsi
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/onsi.gemspec CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'onsi/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skylar Schipper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-18 00:00:00.000000000 Z
11
+ date: 2018-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails