Class: Onsi::Params
- Inherits:
-
Object
- Object
- Onsi::Params
- Defined in:
- lib/onsi/params.rb
Overview
Used to handle parsing JSON-API formated params
Defined Under Namespace
Classes: MissingReqiredAttribute, RelationshipNotFound
Instance Attribute Summary collapse
-
#attributes ⇒ ActionController::Parameters
readonly
The attributes for the params.
-
#relationships ⇒ Hash
readonly
The relationships for the params.
Class Method Summary collapse
-
.parse(params, attributes = [], relationships = []) ⇒ Params
Parse a JSON-API formatted params object.
-
.parse_json(body, attributes = [], relationships = []) ⇒ Onsi::Params
Parse a JSON-API formatted JSON object.
Instance Method Summary collapse
-
#default(key, value) ⇒ Object
Set a default value for attributes.
-
#fetch(key, default = nil) ⇒ Any
Fetch a value from the attributes or return the passed default value.
-
#flatten ⇒ Hash
Flatten an merge the attributes & relationships into one hash.
-
#require(key) ⇒ Any
Make an attributes key required.
- #require_path(key_path) ⇒ Object
-
#safe_fetch(key) ⇒ Any
Handle finding a relationship’s object.
-
#transform(key, &block) ⇒ Any
Perform a transform on the value.
Instance Attribute Details
#attributes ⇒ ActionController::Parameters (readonly)
The attributes for the params.
106 107 108 |
# File 'lib/onsi/params.rb', line 106 def attributes @attributes end |
#relationships ⇒ Hash (readonly)
The relationships for the params.
112 113 114 |
# File 'lib/onsi/params.rb', line 112 def relationships @relationships end |
Class Method Details
.parse(params, attributes = [], relationships = []) ⇒ Params
Parse a JSON-API formatted params object.
74 75 76 77 78 79 80 81 |
# File 'lib/onsi/params.rb', line 74 def parse(params, attributes = [], relationships = []) parser = Onsi::ParamsParser.new(params, attributes, relationships) results = parser.parse! new( results.attributes, results.relationships ) end |
.parse_json(body, attributes = [], relationships = []) ⇒ Onsi::Params
Parse a JSON-API formatted JSON object.
94 95 96 97 98 99 |
# File 'lib/onsi/params.rb', line 94 def parse_json(body, attributes = [], relationships = []) content = body.respond_to?(:read) ? body.read : body json = JSON.parse(content) params = ActionController::Parameters.new(json) parse(params, attributes, relationships) end |
Instance Method Details
#default(key, value) ⇒ Object
Set a default value for attributes.
This value will only be used if the key is missing from the passed attributes
230 231 232 233 |
# File 'lib/onsi/params.rb', line 230 def default(key, value) @attrs_hash = nil defaults[key.to_sym] = value end |
#fetch(key, default = nil) ⇒ Any
Fetch a value from the attributes or return the passed default value
145 146 147 |
# File 'lib/onsi/params.rb', line 145 def fetch(key, default = nil) attrs_hash[key] || default end |
#flatten ⇒ Hash
Flatten an merge the attributes & relationships into one hash.
133 134 135 |
# File 'lib/onsi/params.rb', line 133 def flatten @flattened ||= attrs_hash.to_h.merge(relationships.to_h).with_indifferent_access end |
#require(key) ⇒ Any
Make an attributes key required.
157 158 159 160 161 162 163 164 |
# File 'lib/onsi/params.rb', line 157 def require(key) value = attrs_hash[key] if value.nil? raise MissingReqiredAttribute.new("Missing attribute #{key}", key) end value end |
#require_path(key_path) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/onsi/params.rb', line 166 def require_path(key_path) value = flatten.dig(*key_path.split('/')) if value.nil? raise MissingReqiredAttribute.new("Missing attribute at key_path #{key_path}", key_path) end value end |
#safe_fetch(key) ⇒ Any
Handle finding a relationship’s object.
189 190 191 192 193 |
# File 'lib/onsi/params.rb', line 189 def safe_fetch(key) yield(@relationships[key]) rescue ActiveRecord::RecordNotFound raise RelationshipNotFound.new("Can't find relationship #{key}", key) end |
#transform(key, &block) ⇒ Any
The values are memoized
Perform a transform on the value
Any getter will run the value through the transform block.
210 211 212 213 |
# File 'lib/onsi/params.rb', line 210 def transform(key, &block) @attrs_hash = nil transforms[key.to_sym] = block end |