Class: KStor::Model::Base
- Inherits:
-
Object
- Object
- KStor::Model::Base
- Defined in:
- lib/kstor/model.rb
Overview
Base class for model objects.
Direct Known Subclasses
Class Attribute Summary collapse
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
Class Method Summary collapse
-
.property(name, read_only: false) ⇒ Object
Define a named property.
-
.property?(name) ⇒ Boolean
Check if a property is defined.
Instance Method Summary collapse
-
#clean ⇒ Object
Tell the object that dirty properties were persisted.
-
#dirty? ⇒ Boolean
Check if properties were modified since instanciation.
-
#initialize(**values) ⇒ KStor::Model::Base
constructor
Create a model object from hash values.
-
#to_h ⇒ Hash
Represent model object as a Hash.
Constructor Details
#initialize(**values) ⇒ KStor::Model::Base
Create a model object from hash values
50 51 52 53 54 55 56 |
# File 'lib/kstor/model.rb', line 50 def initialize(**values) @data = {} values.each do |k, v| @data[k] = v if self.class.property?(k) end @dirty = false end |
Class Attribute Details
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
17 18 19 |
# File 'lib/kstor/model.rb', line 17 def properties @properties end |
Class Method Details
.property(name, read_only: false) ⇒ Object
Define a named property
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kstor/model.rb', line 23 def property(name, read_only: false) @properties ||= [] @properties << name define_method(name) do @data[name] end return if read_only define_method("#{name}=".to_sym) do |value| @data[name] = value @dirty = true end end |
.property?(name) ⇒ Boolean
Check if a property is defined.
41 42 43 |
# File 'lib/kstor/model.rb', line 41 def property?(name) @properties.include?(name) end |
Instance Method Details
#clean ⇒ Object
Tell the object that dirty properties were persisted.
66 67 68 |
# File 'lib/kstor/model.rb', line 66 def clean @dirty = false end |
#dirty? ⇒ Boolean
Check if properties were modified since instanciation
61 62 63 |
# File 'lib/kstor/model.rb', line 61 def dirty? @dirty end |
#to_h ⇒ Hash
Represent model object as a Hash
73 74 75 |
# File 'lib/kstor/model.rb', line 73 def to_h @data.to_h { |k, v| [k.to_s, v.respond_to?(:to_h) ? v.to_h : v] } end |