Class: Onsi::Params
- Inherits:
-
Object
show all
- Defined in:
- lib/onsi/params.rb
Defined Under Namespace
Classes: MissingReqiredAttribute, RelationshipNotFound
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes, relationships) ⇒ Params
Returns a new instance of Params.
66
67
68
69
|
# File 'lib/onsi/params.rb', line 66
def initialize(attributes, relationships)
@attributes = attributes
@relationships = relationships
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
64
65
66
|
# File 'lib/onsi/params.rb', line 64
def attributes
@attributes
end
|
#relationships ⇒ Object
Returns the value of attribute relationships.
64
65
66
|
# File 'lib/onsi/params.rb', line 64
def relationships
@relationships
end
|
Class Method Details
.parse(params, attributes = [], relationships = []) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/onsi/params.rb', line 22
def parse(params, attributes = [], relationships = [])
data = params.require(:data)
data.require(:type)
attrs = permit_attributes(data, attributes)
relas = permit_relationships(data, relationships)
new(attrs, relas)
end
|
Instance Method Details
#fetch(key, default = nil) ⇒ Object
75
76
77
|
# File 'lib/onsi/params.rb', line 75
def fetch(key, default = nil)
attrs_hash[key] || default
end
|
#flatten ⇒ Object
71
72
73
|
# File 'lib/onsi/params.rb', line 71
def flatten
attributes.to_h.merge(relationships.to_h).with_indifferent_access
end
|
#require(key) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/onsi/params.rb', line 79
def require(key)
value = attrs_hash[key]
if value.nil?
raise MissingReqiredAttribute.new("Missing attribute #{key}", key)
end
value
end
|
#safe_fetch(key) ⇒ Object
87
88
89
90
91
|
# File 'lib/onsi/params.rb', line 87
def safe_fetch(key)
yield(@relationships[key])
rescue ActiveRecord::RecordNotFound
raise RelationshipNotFound.new("Can't find relationship #{key}", key)
end
|