class EasySerializer::Base

Attributes

object[R]
options[R]

Public Class Methods

attribute(name, opts = {}, &block) click to toggle source
# File lib/easy_serializer/base.rb, line 23
def attribute(name, opts = {}, &block)
  @__serializable_attributes ||= []
  @__serializable_attributes << Attribute.new(name, opts, block)
end
attributes(*args) click to toggle source
# File lib/easy_serializer/base.rb, line 34
def attributes(*args)
  args.each do |input|
    attribute(*input)
  end
end
cache(bool, opts = {}, &block) click to toggle source
# File lib/easy_serializer/base.rb, line 28
def cache(bool, opts = {}, &block)
  if bool
    @__cache = opts.merge(block: block)
  end
end
call(*args) click to toggle source
# File lib/easy_serializer/base.rb, line 16
def call(*args)
  new(*args).serialize
end
Also aliased as: serialize, to_hash, to_h
collection(name, opts = {}, &block) click to toggle source
# File lib/easy_serializer/base.rb, line 40
def collection(name, opts = {}, &block)
  @__serializable_attributes ||= []
  @__serializable_attributes << Collection.new(name, opts, block)
end
new(object, options = {}) click to toggle source
# File lib/easy_serializer/base.rb, line 9
def initialize(object, options = {})
  @object = object
  @options = options
end
serialize(*args)
Alias for: call
to_h(*args)
Alias for: call
to_hash(*args)
Alias for: call

Public Instance Methods

__cache() click to toggle source
# File lib/easy_serializer/base.rb, line 59
def __cache
  self.class.instance_variable_get(:@__cache)
end
send_to_serializer(serializer, value) click to toggle source
# File lib/easy_serializer/base.rb, line 54
def send_to_serializer(serializer, value)
  return unless value
  option_to_value(serializer, value).call(value)
end
serialize() click to toggle source
# File lib/easy_serializer/base.rb, line 46
def serialize
  @serialize ||= serialized_from_cache || _serialize
end
Also aliased as: to_h, to_hash
to_h()
Alias for: serialize
to_hash()
Alias for: serialize
to_s() click to toggle source
# File lib/easy_serializer/base.rb, line 52
def to_s; serialize.to_json end

Private Instance Methods

__serializable_attributes() click to toggle source
# File lib/easy_serializer/base.rb, line 83
def __serializable_attributes
  self.class.instance_variable_get(:@__serializable_attributes) || []
end
_serialize() click to toggle source
# File lib/easy_serializer/base.rb, line 65
def _serialize
  __serializable_attributes.each_with_object({}) do |metadata, hash|
    if metadata.options[:key] === false
      hash.merge!(value_or_default(metadata))
    else
      key = (metadata.options[:key] ? metadata.options[:key] : metadata.name)
      hash[key] = value_or_default(metadata)
    end
  end
end
serialized_from_cache() click to toggle source
# File lib/easy_serializer/base.rb, line 76
def serialized_from_cache
  return false unless EasySerializer.perform_caching
  cache = __cache
  return false unless cache
  RootCacher.call(self){ _serialize }
end
value_or_default(metadata) click to toggle source
# File lib/easy_serializer/base.rb, line 87
def value_or_default(metadata)
  AttributeSerializer.call(self, metadata)
end