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



27
28
29
30
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 27

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



32
33
34
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 32

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



36
37
38
39
40
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 36

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
20
# File 'lib/jsonapi_object_mapper/deserializer/dsl.rb', line 17

def id(&block)
  self.id_block = block || DEFAULT_ID_BLOCK
  define_method(:id) { fetch_attribute(:id) }
end

#type(&block) ⇒ Object



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

def type(&block)
  self.type_block = block || DEFAULT_TYPE_BLOCK
  define_method(:type) { fetch_attribute(:type) }
end