Class: ActiveJsonModel::JsonAttribute
- Inherits:
-
Object
- Object
- ActiveJsonModel::JsonAttribute
- 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
-
#clazz ⇒ Object
readonly
Returns the value of attribute clazz.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#dump_proc ⇒ Object
readonly
Returns the value of attribute dump_proc.
-
#load_proc ⇒ Object
readonly
Returns the value of attribute load_proc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#render_default ⇒ Object
readonly
Returns the value of attribute render_default.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
Instance Method Summary collapse
-
#get_default_value ⇒ Object
Get a default value for this attribute.
-
#initialize(name:, clazz:, default:, validation:, dump_proc:, load_proc:, render_default: true) ⇒ JsonAttribute
constructor
Creates a record of a JSON-backed attribute.
Constructor Details
#initialize(name:, clazz:, default:, validation:, dump_proc:, load_proc:, render_default: true) ⇒ JsonAttribute
Creates a record of a JSON-backed attribute
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
#clazz ⇒ Object (readonly)
Returns the value of attribute clazz.
19 20 21 |
# File 'lib/active_json_model/json_attribute.rb', line 19 def clazz @clazz end |
#default ⇒ Object (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_proc ⇒ Object (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_proc ⇒ Object (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 |
#name ⇒ Object (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_default ⇒ Object (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 |
#validation ⇒ Object (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_value ⇒ Object
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 |