Module: TinySerializer::DSL

Included in:
TinySerializer
Defined in:
lib/tiny_serializer/dsl.rb

Overview

The TinySerializer class macros.

Instance Method Summary collapse

Instance Method Details

#_initialize_attributesObject

Private method to initialize inherited attributes.



143
144
145
146
147
148
149
150
# File 'lib/tiny_serializer/dsl.rb', line 143

def _initialize_attributes # :nodoc:
  @attributes ||=
    if superclass.respond_to?(:attributes)
      superclass.attributes.dup
    else
      []
    end
end

#_is_id?(name) ⇒ Boolean

Private method to check if an attribute name is an ID.

Returns:

  • (Boolean)


153
154
155
# File 'lib/tiny_serializer/dsl.rb', line 153

def _is_id?(name) # :nodoc:
  name == :id || name.to_s.end_with?("_id")
end

#_is_serializer?(klass) ⇒ Boolean

Private method to check if a class is a TinySerializer subclass

Returns:

  • (Boolean)


158
159
160
# File 'lib/tiny_serializer/dsl.rb', line 158

def _is_serializer?(klass)
  klass <= TinySerializer
end

#attribute(name, key: name, is_id: _is_id?(name), &block) ⇒ Object

Definite a new attribute to serialize. The value to serialize is retrieved in one of two ways:

  1. Default: Calls #public_send(name) on TinySerializer#object.

  2. Block: The return value of the block is used.

name

The name of the attribute

key

Optional. Defaults to name. The Hash key to assign the attribute’s value to.

is_id

Optional. Whether the attribute is a database ID. Guessed from its name by default.



55
56
57
58
59
60
61
# File 'lib/tiny_serializer/dsl.rb', line 55

def attribute(name, key: name, is_id: _is_id?(name), &block)
  _initialize_attributes
  name = name.to_sym
  attribute = [name, key, is_id, block]
  @attributes << attribute
  return attribute
end

#attribute_namesObject

Get the names of all attributes defined using #attribute.



76
77
78
# File 'lib/tiny_serializer/dsl.rb', line 76

def attribute_names
  attributes.map(&:first)
end

#attributes(*names) ⇒ Object

Define multiple attributes at once, using the defaults.



64
65
66
67
68
69
70
71
72
73
# File 'lib/tiny_serializer/dsl.rb', line 64

def attributes(*names)
  if names && !names.empty?
    names.each do |name|
      attribute(name)
    end
  else
    _initialize_attributes
  end
  return @attributes
end

#belongs_to(association_name, key: association_name, serializer: nil, &block) ⇒ Object

Alias of #sub_record



86
87
88
# File 'lib/tiny_serializer/dsl.rb', line 86

def belongs_to(association_name, key: association_name, serializer: nil, &block)
  sub_record(association_name, key: key, serializer: serializer, &block)
end

#collection(name, key: name, serializer: nil, &block) ⇒ Object

Define a serializer to use to serialize a collection of objects as an Array. Will call #public_send(name) on TinySerializer#object to get the items in the collection, or use the return value of the block.

name

The name of the collection.

key

Optional. Defaults to name. The Hash key to assign the serialized Array to.

serializer

Optional, inferred from collection_name. The serializer class to use to serialize each item in the collection. Must be a subclass of TinySerializer.



133
134
135
136
137
138
139
140
# File 'lib/tiny_serializer/dsl.rb', line 133

def collection(name, key: name, serializer: nil, &block)
  if serializer.nil?
    serializer = "#{name.to_s.singularize.camelize}Serializer".constantize
  elsif !_is_serializer?(serializer)
    raise ArgumentError, "#{serializer} does not appear to be a TinySerializer"
  end
  collections << [name, key, serializer, block]
end

#collection_namesObject

Get only the names of the collections definitions (created by #has_many).



36
37
38
# File 'lib/tiny_serializer/dsl.rb', line 36

def collection_names
  collections.map(&:first)
end

#collectionsObject

Get all the collection definitions (created by #has_many).



26
27
28
29
30
31
32
33
# File 'lib/tiny_serializer/dsl.rb', line 26

def collections
  @collections ||=
    if superclass.respond_to?(:collections)
      superclass.collections.dup
    else
      []
    end
end

#has_many(name, key: name, serializer: nil, &block) ⇒ Object

Alias of #collection



114
115
116
# File 'lib/tiny_serializer/dsl.rb', line 114

def has_many(name, key: name, serializer: nil, &block)
  collection(name, key: key, serializer: serializer, &block)
end

#has_one(association_name, key: association_name, serializer: nil, &block) ⇒ Object

Alias of #sub_record



81
82
83
# File 'lib/tiny_serializer/dsl.rb', line 81

def has_one(association_name, key: association_name, serializer: nil, &block)
  sub_record(association_name, key: key, serializer: serializer, &block)
end

#sub_record(name, key: name, serializer: nil, &block) ⇒ Object

Define a serializer to use for a sub-object of TinySerializer#object. If given a block: Will use the block to retrieve the object, instead of public_send(name).

name

The method name of the sub-object.

serializer

Optional. The serializer class to use. Inferred from name if blank. Must be a subclass of TinySerializer.

key

Optional. Defaults to name. The Hash key to assign the sub-record’s JSON to.



103
104
105
106
107
108
109
110
111
# File 'lib/tiny_serializer/dsl.rb', line 103

def sub_record(name, key: name, serializer: nil, &block)
  if serializer.nil?
    serializer = "#{name.to_s.camelize}Serializer".constantize
  elsif !_is_serializer?(serializer)
    raise ArgumentError, "#{serializer} does not appear to be a TinySerializer"
  end

  sub_records << [name, key, serializer, block]
end

#sub_record_namesObject

Get only the names of the sub-record attribute definitions (created by #belongs_to, #has_one, etc.)



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

def sub_record_names
  sub_records.map(&:first)
end

#sub_recordsObject

Get all the sub-record attribute definitions (created by #belongs_to, #has_one, etc.)



10
11
12
13
14
15
16
17
# File 'lib/tiny_serializer/dsl.rb', line 10

def sub_records
  @sub_records ||=
    if superclass.respond_to?(:sub_records)
      superclass.sub_records.dup
    else
      []
    end
end