Module: JsonAPIObjectMapper::Deserialize::DSL

Included in:
Resource
Defined in:
lib/jsonapi-object-mapper/deserialize/dsl.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_BLOCK =
proc { |value| value }
HAS_MANY_BLOCK =
proc { |values| values.is_a?(Collection) ? values : Array(values) }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



9
10
11
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 9

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

Instance Method Details

#attribute(attribute_name, &block) ⇒ Object



23
24
25
26
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 23

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

#attributes(*attributes_names) ⇒ Object



28
29
30
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 28

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

#has_many(relationship_name, **options, &block) ⇒ Object



39
40
41
42
43
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 39

def has_many(relationship_name, **options, &block)
  rel_options_process!(relationship_name, options)
  rel_has_many_blocks[relationship_name.to_s] = block || HAS_MANY_BLOCK
  define_method(relationship_name.to_sym) { fetch_relationship(relationship_name) }
end

#has_one(relationship_name, **options, &block) ⇒ Object Also known as: belongs_to



32
33
34
35
36
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 32

def has_one(relationship_name, **options, &block)
  rel_options_process!(relationship_name, options)
  rel_has_one_blocks[relationship_name.to_s] = block || DEFAULT_BLOCK
  define_method(relationship_name.to_sym) { fetch_relationship(relationship_name) }
end

#id(&block) ⇒ Object



13
14
15
16
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 13

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

#kind_of_resource?(klass) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 45

def kind_of_resource?(klass)
  !klass.nil? && klass < Resource
end

#type(&block) ⇒ Object



18
19
20
21
# File 'lib/jsonapi-object-mapper/deserialize/dsl.rb', line 18

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