Class: DashOverlord::Serializers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dash_overlord/serializers/base.rb

Direct Known Subclasses

V1::Base

Constant Summary collapse

RAILS_HTTP_STATUS =
{
  continue: 100,
  switching_protocols: 101,
  processing: 102,
  ok: 200,
  created: 201,
  accepted: 202,
  non_authoritative_information: 203,
  no_content: 204,
  reset_content: 205,
  partial_content: 206,
  multi_status: 207,
  im_used: 226,
  multiple_choices: 300,
  moved_permanently: 301,
  found: 302,
  see_other: 303,
  not_modified: 304,
  use_proxy: 305,
  temporary_redirect: 307,
  bad_request: 400,
  unauthorized: 401,
  payment_required: 402,
  forbidden: 403,
  not_found: 404,
  method_not_allowed: 405,
  not_acceptable: 406,
  proxy_authentication_required: 407,
  request_timeout: 408,
  conflict: 409,
  gone: 410,
  length_required: 411,
  precondition_failed: 412,
  request_entity_too_large: 413,
  request_uri_too_long: 414,
  unsupported_media_type: 415,
  requested_range_not_satisfiable: 416,
  expectation_failed: 417,
  unprocessable_entity: 422,
  locked: 423,
  failed_dependency: 424,
  upgrade_required: 426,
  internal_server_error: 500,
  not_implemented: 501,
  bad_gateway: 502,
  service_unavailable: 503,
  gateway_timeout: 504,
  http_version_not_supported: 505,
  insufficient_storage: 507,
  not_extended: 510
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Base

Returns a new instance of Base.



96
97
98
# File 'lib/dash_overlord/serializers/base.rb', line 96

def initialize(object, options = {})
  @object, @options = object, options
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



100
101
102
# File 'lib/dash_overlord/serializers/base.rb', line 100

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



100
101
102
# File 'lib/dash_overlord/serializers/base.rb', line 100

def options
  @options
end

Class Method Details

.attributesObject



89
90
91
92
93
94
# File 'lib/dash_overlord/serializers/base.rb', line 89

def self.attributes
  @attributes ||= (superclass.respond_to?(:attributes) \
    ? (superclass.attributes || [])
    : []
  ).dup
end

.expose(attribute, config = {}) ⇒ Object



85
86
87
# File 'lib/dash_overlord/serializers/base.rb', line 85

def self.expose(attribute, config = {})
  attributes << [attribute, { as: attribute }.merge(config)]
end

.root(root_name) ⇒ Object



74
75
76
# File 'lib/dash_overlord/serializers/base.rb', line 74

def self.root(root_name)
  @root_name = root_name
end

.root_nameObject



78
79
80
81
82
83
# File 'lib/dash_overlord/serializers/base.rb', line 78

def self.root_name
  @root_name ||= (superclass.respond_to?(:root_name) \
    ? (superclass.root_name || '')
    : ''
  ).dup
end

.serialize(object, options) ⇒ Object



68
69
70
71
72
# File 'lib/dash_overlord/serializers/base.rb', line 68

def self.serialize(object, options)
  data = self.new(object, options).serialize

  (root_name != '') ? { root_name => data } : data
end

.to_hash(context, method = nil, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/dash_overlord/serializers/base.rb', line 57

def self.to_hash(context, method = nil, options = {})
  object = method ? context.send(method) : context

  hash = (method == false) ? { data: nil } : serialize(object, options)

  hash.merge \
    code: RAILS_HTTP_STATUS[context.status.to_sym],
    status: context.status,
    message: context.error.message
end

Instance Method Details

#serializeObject



102
103
104
# File 'lib/dash_overlord/serializers/base.rb', line 102

def serialize
  object.respond_to?(:map) ? _serialize_array : _serialize_hash
end