Module: Alba::Resource::InstanceMethods

Included in:
Alba::Resource
Defined in:
lib/alba/resource.rb

Overview

Instance methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



42
43
44
# File 'lib/alba/resource.rb', line 42

def object
  @object
end

#paramsObject (readonly)

Returns the value of attribute params.



42
43
44
# File 'lib/alba/resource.rb', line 42

def params
  @params
end

Instance Method Details

#as_json(_options = {}, root_key: nil, meta: {}) ⇒ Hash

Returns a Hash correspondng #serialize

Parameters:

  • _options (Hash) (defaults to: {})

    dummy parameter for Rails compatibility

  • root_key (Symbol, nil, true) (defaults to: nil)
  • meta (Hash) (defaults to: {})

    metadata for this seialization

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
93
94
# File 'lib/alba/resource.rb', line 85

def as_json(_options = {}, root_key: nil, meta: {})
  key = root_key.nil? ? fetch_key : root_key
  key = Alba.regularize_key(key)
  if key && !key.empty?
    h = {key => serializable_hash}
    (h, meta)
  else
    serializable_hash
  end
end

#initialize(object, params: {}, within: WITHIN_DEFAULT) ⇒ Object

Parameters:

  • object (Object)

    the object to be serialized

  • params (Hash) (defaults to: {})

    user-given Hash for arbitrary data

  • within (Object, nil, false, true) (defaults to: WITHIN_DEFAULT)

    determines what associations to be serialized. If not set, it serializes all associations.



47
48
49
50
51
52
# File 'lib/alba/resource.rb', line 47

def initialize(object, params: {}, within: WITHIN_DEFAULT)
  @object = object
  @params = params
  @within = within
  _setup
end

#serializable_hashHash Also known as: to_h

A Hash for serialization

Returns:

  • (Hash)


99
100
101
# File 'lib/alba/resource.rb', line 99

def serializable_hash
  collection? ? serializable_hash_for_collection : converter.call(@object)
end

#serialize(root_key: nil, meta: {}) ⇒ String

Serialize object into JSON string

Parameters:

  • root_key (Symbol, nil, true) (defaults to: nil)
  • meta (Hash) (defaults to: {})

    metadata for this seialization

Returns:

  • (String)

    serialized JSON string



59
60
61
# File 'lib/alba/resource.rb', line 59

def serialize(root_key: nil, meta: {})
  serialize_with(as_json(root_key: root_key, meta: meta))
end

#to_json(options = {}, root_key: nil, meta: {}) ⇒ Object

For Rails compatibility The first options is a dummy parameter



68
69
70
71
72
73
74
75
76
77
# File 'lib/alba/resource.rb', line 68

def to_json(options = {}, root_key: nil, meta: {})
  confusing_options = options.keys.select { |k| k.to_sym == :only || k.to_sym == :except }
  unless confusing_options.empty?
    confusing_options.sort!
    confusing_options.map! { |s| "\"#{s}\"" }
    message = "You passed #{confusing_options.join(' and ')} options but ignored. Please refer to the document: https://github.com/okuramasafumi/alba/blob/main/docs/rails.md"
    Kernel.warn(message)
  end
  serialize(root_key: root_key, meta: meta)
end