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.



44
45
46
# File 'lib/alba/resource.rb', line 44

def object
  @object
end

#paramsObject (readonly)

Returns the value of attribute params.



44
45
46
# File 'lib/alba/resource.rb', line 44

def params
  @params
end

Instance Method Details

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

Returns a Hash corresponding #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 serialization

Returns:

  • (Hash)


94
95
96
97
98
99
100
101
102
103
# File 'lib/alba/resource.rb', line 94

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, with_traits: nil, select: nil) ⇒ 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.

  • with_traits (Symbol, Array<Symbol>, nil) (defaults to: nil)

    specified traits

  • select (Method) (defaults to: nil)

    select method object used with ‘nested_attribute` and `trait`



51
52
53
54
55
56
57
58
59
60
# File 'lib/alba/resource.rb', line 51

def initialize(object, params: {}, within: WITHIN_DEFAULT, with_traits: nil, select: nil)
  @object = object
  @params = params
  @within = within
  @with_traits = with_traits
  # select override to share the same method with `trait` and `nested_attribute`
  # Trait and NestedAttribute generates anonymous class so it checks if it's anonymous class to prevent accidental overriding
  self.class.define_method(:select, &select) if select && self.class.name.nil?
  _setup
end

#serializable_hashHash Also known as: to_h

A Hash for serialization

Returns:

  • (Hash)


108
109
110
# File 'lib/alba/resource.rb', line 108

def serializable_hash
  Alba.collection?(@object) ? serializable_hash_for_collection : attributes_to_hash(@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 serialization

Returns:

  • (String)

    serialized JSON string



67
68
69
# File 'lib/alba/resource.rb', line 67

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



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/alba/resource.rb', line 76

def to_json(options = {}, root_key: nil, meta: {})
  confusing_keys = [:only, :except]
  confusing_options = options.keys.select { |k| confusing_keys.include?(k.to_sym) }
  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