Module: Alba::Resource::InstanceMethods

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

Overview

Instance methods available when Resource is included

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.

Returns:

  • (Object)


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

def object
  @object
end

#paramsgeneric_hash (readonly)

Returns the value of attribute params.

Returns:

  • (generic_hash)


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

def params
  @params
end

Instance Method Details

#_setupvoid

This method returns an undefined value.



52
# File 'sig/alba/resource.rbs', line 52

def _setup: () -> void

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

Returns a Hash corresponding #serialize

Parameters:

  • _options (Hash) (defaults to: EMPTY_HASH)

    dummy parameter for Rails compatibility

  • root_key (Symbol, nil) (defaults to: nil)
  • meta (Hash) (defaults to: EMPTY_HASH)

    metadata for this serialization

  • options (generic_hash)
  • root_key: (Alba::key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (Hash)


98
99
100
101
102
103
104
105
106
107
# File 'lib/alba/resource.rb', line 98

def as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH)
  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

#assoc_name_from_paramsSymbol, String

Parameters:

  • name (Symbol, String)

Returns:

  • (Symbol, String)


48
# File 'sig/alba/resource.rbs', line 48

def assoc_name_from_params: (Symbol | String name) -> (Symbol | String)

#association_editorModule?

Returns:

  • (Module, nil)


47
# File 'sig/alba/resource.rbs', line 47

def association_editor: () -> Module?

#attributes_from_association[Alba::key_type, untyped]

Parameters:

Returns:

  • ([Alba::key_type, untyped])


46
# File 'sig/alba/resource.rbs', line 46

def attributes_from_association: (Association association, untyped obj) -> [Alba::key_type, untyped]

#conditional_attribute?Boolean

Parameters:

Returns:

  • (Boolean)


51
# File 'sig/alba/resource.rbs', line 51

def conditional_attribute?: (untyped object, ConditionalAttribute attribute) -> bool

#encodeString

Parameters:

  • (generic_hash)

Returns:

  • (String)


38
# File 'sig/alba/resource.rbs', line 38

def encode: (generic_hash) -> String

#fetch_attribute(obj, key, attribute) ⇒ Object

rubocop:disable Metrics

Parameters:

  • object (Object)
  • key (Symbol, String)
  • attribute (attribute)

Returns:

  • (Object)


291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/alba/resource.rb', line 291

def fetch_attribute(obj, key, attribute) # rubocop:disable Metrics
  value = case attribute
          when Symbol then fetch_attribute_from_object_and_resource(obj, attribute)
          when Proc then instance_exec(obj, &attribute)
          when Alba::Association then yield_if_within(attribute.name.to_sym) { |within| attribute.to_h(obj, params: params, within: within) }
          when TypedAttribute then attribute.value(object: obj) { |attr| fetch_attribute(obj, key, attr) }
          when NestedAttribute then attribute.value(object: obj, params: params, within: @within, select: method(:select))
          when ConditionalAttribute then attribute.with_passing_condition(resource: self, object: obj) { |attr| fetch_attribute(obj, key, attr) }
            # :nocov:
          else raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
            # :nocov:
          end
  value.nil? && nil_handler ? instance_exec(obj, key, attribute, &nil_handler) : value
end

#fetch_keyString

Returns:

  • (String)


184
185
186
187
# File 'lib/alba/resource.rb', line 184

def fetch_key
  k = Alba.collection?(@object) ? _key_for_collection : _key
  transforming_root_key? ? transform_key(k) : k
end

#handle_error(error, obj, key, attribute, hash) ⇒ Object

Parameters:

Returns:

  • (Object)


270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/alba/resource.rb', line 270

def handle_error(error, obj, key, attribute, hash)
  case (on_error = @_on_error || :raise)
  when :raise, nil then raise(error)
  when :nullify then hash[key] = nil
  when :ignore then nil
  when Proc
    key, value = on_error.call(error, obj, key, attribute, self.class)
    hash[key] = value unless Alba::REMOVE_KEY == key # rubocop:disable Style/YodaCondition
  else
    # :nocov:
    raise Alba::Error, 'Impossible path'
    # :nocov:
  end
end

#hash_with_metadata(hash, meta) ⇒ generic_hash

Parameters:

  • (generic_hash)
  • (generic_hash)

Returns:

  • (generic_hash)


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/alba/resource.rb', line 142

def (hash, meta)
  return hash if meta.empty? && @_meta&.last.nil?

  key, block = @_meta || :meta

  if key
    hash[key] = (block, meta)
  else
    (block, meta).each { |k, v| hash[k] = v }
  end
  hash
end

#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object

Initialization



55
56
57
58
59
60
61
62
63
64
# File 'lib/alba/resource.rb', line 55

def initialize(object, params: EMPTY_HASH, 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

#on_nilObject

Parameters:

  • object (Object)
  • key (Symbol, String)

Returns:

  • (Object)


50
# File 'sig/alba/resource.rbs', line 50

def on_nil: (untyped object, Symbol | String key) -> untyped

#select(_key, _value, _attribute) ⇒ Boolean

Default implementation for selecting attributes Override this method to filter attributes based on key and value

Parameters:

  • key (Symbol, String)
  • value (Object)
  • attribute (attribute)

Returns:

  • (Boolean)


250
251
252
# File 'lib/alba/resource.rb', line 250

def select(_key, _value, _attribute)
  true
end

#serializable_hashHash Also known as: to_h

A Hash for serialization

Returns:

  • (Hash)


112
113
114
# File 'lib/alba/resource.rb', line 112

def serializable_hash
  Alba.collection?(@object) ? serializable_hash_for_collection : attributes_to_hash(@object, {})
end

#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String

Serialization methods

Parameters:

  • root_key: (Alba::key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)


71
72
73
# File 'lib/alba/resource.rb', line 71

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

#serialize_with(hash) ⇒ String

Parameters:

  • (generic_hash)

Returns:

  • (String)


135
136
137
138
139
140
# File 'lib/alba/resource.rb', line 135

def serialize_with(hash)
  serialized_json = encode(hash)
  return serialized_json unless @_layout

  @_layout.serialize(resource: self, serialized_json: serialized_json, binding: binding)
end

#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String

For Rails compatibility The first options is a dummy parameter

Parameters:

  • options (generic_hash) (defaults to: EMPTY_HASH)
  • root_key: (Alba::key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)

See Also:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/alba/resource.rb', line 80

def to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH)
  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

#transform_key(key) ⇒ Alba::key_type

Parameters:

  • (Alba::key_type)

Returns:

  • (Alba::key_type)


285
286
287
288
289
# File 'lib/alba/resource.rb', line 285

def transform_key(key)
  return Alba.regularize_key(key) if @_transform_type == :none || key.nil? || key.empty? # We can skip transformation

  Alba.transform_key(key, transform_type: @_transform_type)
end

#transform_keysgeneric_hash

Parameters:

  • (generic_hash)

Returns:

  • (generic_hash)


44
# File 'sig/alba/resource.rbs', line 44

def transform_keys: (generic_hash) -> generic_hash

#transformable_root?Boolean

Returns:

  • (Boolean)


42
# File 'sig/alba/resource.rbs', line 42

def transformable_root?: () -> bool