Class: ActiveJsonModel::JsonAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_json_model/json_attribute.rb

Overview

Instance of an attribute for a model backed by JSON persistence. Data object used for tracking the attributes on the models.

e.g.

class class Credentials < ::ActiveJsonModel
  json_attribute :username
  json_attribute :password
end

#...

# Returns instances of JsonAttribute
Credentials.active_json_model_attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, clazz:, default:, validation:, dump_proc:, load_proc:, render_default: true) ⇒ JsonAttribute

Creates a record of a JSON-backed attribute

Parameters:

  • name (Symbol, String)

    the name of the attribute

  • clazz (Class)

    the Class that implements the type of the attribute (ActiveJsonModel)

  • default (Object, ...)

    the default value for the attribute if unspecified

  • validation (Hash)

    an object with properties that represent ActiveModel validation

  • dump_proc (Proc)

    proc to generate a value from the value to be rendered to JSON. Given value and parent_model values. The value returned is assumed to be a valid JSON value. The proc can take either one or two parameters this is automatically handled by the caller.

  • load_proc (Proc)

    proc to generate a value from the JSON. May take json_value and json_hash. The raw value and the parent hash being parsed, respectively. May return either a class (which will be instantiated) or a value directly. The proc can take either one or two parameters this is automatically handled by the caller.

  • render_default (Boolean) (defaults to: true)

    should the default value be rendered to JSON? Default is true. Note this only applies if the value has not be explicitly set. If explicitly set, the value renders, regardless of if the value is the same as the default value.



42
43
44
45
46
47
48
49
50
# File 'lib/active_json_model/json_attribute.rb', line 42

def initialize(name:, clazz:, default:, validation:, dump_proc:, load_proc:, render_default: true)
  @name = name.to_sym
  @clazz = clazz
  @default = default
  @render_default = render_default
  @validation = validation
  @dump_proc = dump_proc
  @load_proc = load_proc
end

Instance Attribute Details

#clazzObject (readonly)

Returns the value of attribute clazz.



19
20
21
# File 'lib/active_json_model/json_attribute.rb', line 19

def clazz
  @clazz
end

#defaultObject (readonly)

Returns the value of attribute default.



20
21
22
# File 'lib/active_json_model/json_attribute.rb', line 20

def default
  @default
end

#dump_procObject (readonly)

Returns the value of attribute dump_proc.



24
25
26
# File 'lib/active_json_model/json_attribute.rb', line 24

def dump_proc
  @dump_proc
end

#load_procObject (readonly)

Returns the value of attribute load_proc.



23
24
25
# File 'lib/active_json_model/json_attribute.rb', line 23

def load_proc
  @load_proc
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/active_json_model/json_attribute.rb', line 18

def name
  @name
end

#render_defaultObject (readonly)

Returns the value of attribute render_default.



21
22
23
# File 'lib/active_json_model/json_attribute.rb', line 21

def render_default
  @render_default
end

#validationObject (readonly)

Returns the value of attribute validation.



22
23
24
# File 'lib/active_json_model/json_attribute.rb', line 22

def validation
  @validation
end

Instance Method Details

#get_default_valueObject

Get a default value for this attribute. Handles defaults that can be generators with callbacks and proper cloning of real values to avoid cross-object mutation.



54
55
56
57
58
59
60
61
62
# File 'lib/active_json_model/json_attribute.rb', line 54

def get_default_value
  if default
    if default.respond_to?(:call)
      default.call
    else
      default.clone
    end
  end
end