Module: FunWithJsonApi::DeserializerClassMethods

Included in:
Deserializer
Defined in:
lib/fun_with_json_api/deserializer_class_methods.rb

Overview

Provides a basic DSL for defining a FunWithJsonApi::Deserializer

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Object

Attributes



33
34
35
36
37
38
39
40
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 33

def attribute(name, options = {})
  lock.synchronize do
    Attribute.create(name, options).tap do |attribute|
      add_parse_attribute_method(attribute)
      attributes << attribute
    end
  end
end

#attribute_namesObject



42
43
44
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 42

def attribute_names
  lock.synchronize { attributes.map(&:name) }
end

#belongs_to(name, deserializer_class_or_callable, options = {}) ⇒ Object

Relationships



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 57

def belongs_to(name, deserializer_class_or_callable, options = {})
  lock.synchronize do
    Attributes::Relationship.create(
      name,
      deserializer_class_or_callable,
      options
    ).tap do |relationship|
      add_parse_resource_method(relationship)
      relationships << relationship
    end
  end
end

#build_attributes(names) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 46

def build_attributes(names)
  lock.synchronize do
    names.map do |name|
      attribute = attributes.detect { |rel| rel.name == name }
      attribute.class.create(attribute.name, attribute.options)
    end
  end
end

#build_relationships(options) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 91

def build_relationships(options)
  lock.synchronize do
    options.map do |name, relationship_options|
      relationship = relationships.detect { |rel| rel.name == name }
      relationship.class.create(
        relationship.name,
        relationship.deserializer_class,
        relationship_options.reverse_merge(relationship.options)
      )
    end
  end
end

#has_many(name, deserializer_class_or_callable, options = {}) ⇒ Object

rubocop:disable Style/PredicateName



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 72

def has_many(name, deserializer_class_or_callable, options = {})
  lock.synchronize do
    Attributes::RelationshipCollection.create(
      name,
      deserializer_class_or_callable,
      options
    ).tap do |relationship|
      add_parse_resource_method(relationship)
      relationships << relationship
    end
  end
end

#id_param(id_param = nil, format: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 6

def id_param(id_param = nil, format: false)
  lock.synchronize do
    @id_param = id_param.to_sym if id_param
  end
  (@id_param || :id).tap do |param|
    if format
      attribute(:id, as: param, format: format) # Create a new id attribute
    end
  end
end

#relationship_namesObject

rubocop:enable Style/PredicateName



87
88
89
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 87

def relationship_names
  lock.synchronize { relationships.map(&:name) }
end

#resource_class(resource_class = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 24

def resource_class(resource_class = nil)
  lock.synchronize do
    @resource_class = resource_class if resource_class
  end
  @resource_class || type_from_class_name.singularize.classify.constantize
end

#type(type = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/fun_with_json_api/deserializer_class_methods.rb', line 17

def type(type = nil)
  lock.synchronize do
    @type = type if type
  end
  @type || type_from_class_name
end