module TinySerializer::DSL

The TinySerializer class macros.

Public Instance Methods

_is_serializer?(klass) click to toggle source

Private method to check if a class is a TinySerializer subclass

# 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) click to toggle source

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.

# 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_names() click to toggle source

Get the names of all attributes defined using attribute.

# File lib/tiny_serializer/dsl.rb, line 76
def attribute_names
  attributes.map(&:first)
end
attributes(*names) click to toggle source

Define multiple attributes at once, using the defaults.

# 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) click to toggle source

Alias of sub_record

# 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) click to toggle source

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.

# 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_names() click to toggle source

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

# File lib/tiny_serializer/dsl.rb, line 36
def collection_names
  collections.map(&:first)
end
collections() click to toggle source

Get all the collection definitions (created by has_many).

# 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) click to toggle source

Alias of collection

# 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) click to toggle source

Alias of sub_record

# 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) click to toggle source

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.

# 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_names() click to toggle source

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

# File lib/tiny_serializer/dsl.rb, line 21
def sub_record_names
  sub_records.map(&:first)
end
sub_records() click to toggle source

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

# 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