Module: Jsapi::Model::Nestable

Included in:
Controller::Parameters, JSON::Object
Defined in:
lib/jsapi/model/nestable.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Returns the value assigned to name.



7
8
9
# File 'lib/jsapi/model/nestable.rb', line 7

def [](name)
  raw_attributes[name&.to_s]&.value
end

#additional_attributesObject

Returns a hash containing the additional attributes.



12
13
14
# File 'lib/jsapi/model/nestable.rb', line 12

def additional_attributes
  raw_additional_attributes.transform_values(&:value)
end

#attribute?(name) ⇒ Boolean

Returns true if name is present, false otherwise.

Returns:

  • (Boolean)


17
18
19
# File 'lib/jsapi/model/nestable.rb', line 17

def attribute?(name)
  raw_attributes.key?(name&.to_s)
end

#attributesObject

Returns a hash containing all attributes.



22
23
24
# File 'lib/jsapi/model/nestable.rb', line 22

def attributes
  raw_attributes.transform_values(&:value)
end

#inspectObject

:nodoc:



26
27
28
29
30
31
32
33
# File 'lib/jsapi/model/nestable.rb', line 26

def inspect # :nodoc:
  "#<#{self.class.name} #{
    raw_attributes
      .merge('additional_attributes' => raw_additional_attributes)
      .map { |k, v| "#{k}: #{v.inspect}" }
      .join(', ')
  }>"
end

#serializable_hash(**options) ⇒ Object

Returns a hash containing serializable representations of all attributes.

Possible options are:

  • :only - The hash contains the given attributes only.

  • :except - The hash does not contain the given attributes.

  • :symbolize_names - If set to true, keys are symbols.

  • :jsonify_values - If set to true, values are converted by as_json.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsapi/model/nestable.rb', line 43

def serializable_hash(**options)
  options = options.dup
  except = options.delete(:except)&.map(&:to_s)
  only = options.delete(:only)&.map(&:to_s)
  symbolize_names = options[:symbolize_names] == true

  {}.tap do |hash|
    [raw_attributes, raw_additional_attributes].each do |attributes|
      attributes.each do |name, value|
        next if except&.include?(name) || only&.exclude?(name)

        name = name.to_sym if symbolize_names
        hash[name] = value.serializable_value(**options)
      end
    end
  end
end