Module: JsonAPIObjectMapper::Deserializer::DSL

Included in:
Resource
Defined in:
lib/jsonapi_object_mapper/deserializer/dsl.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_ID_BLOCK =
proc { |id| id }
DEFAULT_TYPE_BLOCK =
proc { |type| type }
DEFAULT_ATTR_BLOCK =
proc { |value| value }
DEFAULT_HAS_ONE_BLOCK =
proc { |value| value }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



13
14
15
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 13

def self.extended(klass)
  klass.include ClassMethods
end

Instance Method Details

#attribute(attribute_name, &block) ⇒ Object



25
26
27
28
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 25

def attribute(attribute_name, &block)
  attr_blocks[attribute_name.to_s] = block || DEFAULT_ATTR_BLOCK
  define_method(attribute_name.to_sym) { fetch_attribute(attribute_name) }
end

#attributes(*attributes_names) ⇒ Object



30
31
32
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 30

def attributes(*attributes_names)
  attributes_names.each(&method(:attribute))
end

#has_one(relationship_name, embed_with: nil, &block) ⇒ Object Also known as: has_many, belongs_to

rubocop:disable Naming/PredicateName



34
35
36
37
38
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 34

def has_one(relationship_name, embed_with: nil, &block) # rubocop:disable Naming/PredicateName
  rel_blocks[relationship_name.to_s]  = block || DEFAULT_HAS_ONE_BLOCK
  rel_options[relationship_name.to_s] = embed_with unless embed_with.nil?
  define_method(relationship_name.to_sym) { fetch_relationship(relationship_name) }
end

#id(&block) ⇒ Object



17
18
19
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 17

def id(&block)
  self.id_block = block || DEFAULT_ID_BLOCK
end

#type(&block) ⇒ Object



21
22
23
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 21

def type(&block)
  self.type_block = block || DEFAULT_TYPE_BLOCK
end