Class: SimpleJsonapi::Parameters::IncludeSpec
- Inherits:
-
Object
- Object
- SimpleJsonapi::Parameters::IncludeSpec
- Defined in:
- lib/simple_jsonapi/parameters/include_spec.rb
Overview
Represents the include
parameter as defined by the JSONAPI spec.
Class Method Summary collapse
-
.wrap(specs) ⇒ Object
Wraps an
include
parameter in an IncludeSpec instance.
Instance Method Summary collapse
- #[](relationship_name) ⇒ IncludeSpec
- #include?(relationship_name) ⇒ Boolean
-
#initialize(*specs) ⇒ IncludeSpec
constructor
A new instance of IncludeSpec.
- #merge(*specs) ⇒ self
- #to_h ⇒ Hash
Constructor Details
#initialize(*specs) ⇒ IncludeSpec
Returns a new instance of IncludeSpec.
16 17 18 19 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 16 def initialize(*specs) @data = {} merge(*specs) if specs.any? end |
Class Method Details
.wrap(specs) ⇒ Object
Wraps an include
parameter in an SimpleJsonapi::Parameters::IncludeSpec instance.
6 7 8 9 10 11 12 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 6 def self.wrap(specs) if specs.is_a?(IncludeSpec) specs else IncludeSpec.new(specs) end end |
Instance Method Details
#[](relationship_name) ⇒ IncludeSpec
41 42 43 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 41 def [](relationship_name) @data[relationship_name.to_sym] end |
#include?(relationship_name) ⇒ Boolean
46 47 48 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 46 def include?(relationship_name) @data.key?(relationship_name.to_sym) end |
#merge(*specs) ⇒ self
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 24 def merge(*specs) paths = specs.flatten.flat_map { |s| s.to_s.split(",") } paths.each do |path| terms = path.split(".") nested_spec = @data[terms.first.to_sym] ||= IncludeSpec.new if terms.size > 1 nested_spec.merge(terms.drop(1).join(".")) end end self end |
#to_h ⇒ Hash
51 52 53 54 55 |
# File 'lib/simple_jsonapi/parameters/include_spec.rb', line 51 def to_h @data.each_with_object({}) do |(name, spec), hash| hash[name] = spec.to_h end end |