Module: SimpleJsonApi::DSL

Included in:
Serializer
Defined in:
lib/simple_json_api/dsl.rb

Overview

Define the public API for creating serializers

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_associationsObject

Returns the value of attribute _associations.



11
12
13
# File 'lib/simple_json_api/dsl.rb', line 11

def _associations
  @_associations
end

#_attributesObject

Returns the value of attribute _attributes.



12
13
14
# File 'lib/simple_json_api/dsl.rb', line 12

def _attributes
  @_attributes
end

#_default_fieldsObject

Returns the value of attribute _default_fields.



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

def _default_fields
  @_default_fields
end

#_required_associationsObject

Returns the value of attribute _required_associations.



14
15
16
# File 'lib/simple_json_api/dsl.rb', line 14

def _required_associations
  @_required_associations
end

#_root_nameObject (readonly)

Returns the value of attribute _root_name.



15
16
17
# File 'lib/simple_json_api/dsl.rb', line 15

def _root_name
  @_root_name
end

Instance Method Details

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



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

def attribute(name, options = {})
  register_attribute(name, options)
end

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



44
45
46
# File 'lib/simple_json_api/dsl.rb', line 44

def belongs_to(name, options = {})
  register_association(name, :belongs_to, options)
end

#default_attributesObject



56
57
58
# File 'lib/simple_json_api/dsl.rb', line 56

def default_attributes
  @_attributes.map(&:first).join(',')
end

#default_fields(attrs) ⇒ Object

Attributes presented if no fields specified



35
36
37
# File 'lib/simple_json_api/dsl.rb', line 35

def default_fields(attrs)
  @_default_fields = attrs
end

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



48
49
50
# File 'lib/simple_json_api/dsl.rb', line 48

def has_many(name, options = {})
  register_association(name, :has_many, options)
end

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



52
53
54
# File 'lib/simple_json_api/dsl.rb', line 52

def has_one(name, options = {})
  register_association(name, :has_one, options)
end

#inherited(base) ⇒ Object



17
18
19
20
# File 'lib/simple_json_api/dsl.rb', line 17

def inherited(base)
  base._associations = []
  base._attributes = []
end

#required_associations(assocs) ⇒ Object

Associations that are always included



40
41
42
# File 'lib/simple_json_api/dsl.rb', line 40

def required_associations(assocs)
  @_required_associations = assocs
end

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



22
23
24
25
26
27
28
# File 'lib/simple_json_api/dsl.rb', line 22

def serializes(name, options = {})
  Serializer.register_serializer(
    { serializer: self, resource: name },
    options
  )
  @_root_name = name
end