Class: KeyVortex::Record
- Inherits:
-
Object
- Object
- KeyVortex::Record
- Defined in:
- lib/key_vortex/record.rb
Overview
To represent a structured piece of information you want to save, it has to be a subclass of KeyVortex::Record. There will always be field named #key, which is at most 36 characters long, but keys with any other name can also be added.
class ExampleRecord < KeyVortex::Record
field :a, Integer, minimum: 10, maximum: 100
field :b, String, length: 100
field :c, Integer
end
See Record.field for more details on how those directives are structured. When you’re ready to use the class, values can be provided either in the constructor like this:
ExampleRecord.new(
a: 15,
b: "foo",
c: 1000,
)
Or set after the object is created like this:
r = ExampleRecord.new
r.a = 15
r.b = "foo"
r.c = 1000
In either case, the values can be retrieved as attributes after they’re set:
r.a # 15
r.b # "foo"
r.c # 1000
Instance Attribute Summary collapse
-
#key ⇒ String
The key used to save and retrieve this record.
-
#values ⇒ Hash
readonly
A hash of all of the attributes, in which the keys are symbols.
Class Method Summary collapse
-
.field(name, type, **constraints) ⇒ Field
Declares what values are tracked by this record.
-
.fields ⇒ Array
All of the fields declared for this class.
-
.json_create(object) ⇒ Record
Convert a json parse result into a record for this class.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Two records are equal if they are the same class and have the same values.
-
#initialize(values = {}) ⇒ Record
constructor
Values can optionally be specified when the object is created.
-
#to_json(*args) ⇒ String
Converts the record into a JSON string appropriate for parsing using the create_additions option.
Constructor Details
#initialize(values = {}) ⇒ Record
Values can optionally be specified when the object is created.
138 139 140 141 142 143 |
# File 'lib/key_vortex/record.rb', line 138 def initialize(values = {}) @values = {} values.each do |name, value| send("#{name}=", value) end end |
Instance Attribute Details
#key ⇒ String
The key used to save and retrieve this record. Every field has a key defined, it can be a String up to 36 characters long, which is enough to accomodate a GUID if that’s what you want to use.
118 |
# File 'lib/key_vortex/record.rb', line 118 field :key, String, length: 36 |
#values ⇒ Hash (readonly)
A hash of all of the attributes, in which the keys are symbols.
129 130 131 |
# File 'lib/key_vortex/record.rb', line 129 def values @values end |
Class Method Details
.field(name, type, **constraints) ⇒ Field
Declares what values are tracked by this record. When declaring a field, it follows this general format:
field :field_name, ClassName, constraint1: contraint1_value, constraint2: constraint2_value
From a record’s perspective any number of constraints are valid, including none at all.
59 60 61 62 63 |
# File 'lib/key_vortex/record.rb', line 59 def field(name, type, **constraints) field = KeyVortex::Field.new(name, type, **constraints) register_field(field) field end |
.fields ⇒ Array
Returns All of the fields declared for this class.
46 47 48 |
# File 'lib/key_vortex/record.rb', line 46 def fields field_hash.values end |
.json_create(object) ⇒ Record
Convert a json parse result into a record for this class. This is what’s called when this command is used:
JSON.parse(json, create_additions: true)
See #to_json for an example of the JSON structure this example expects.
73 74 75 |
# File 'lib/key_vortex/record.rb', line 73 def json_create(object) new(object["values"]) end |
Instance Method Details
#==(other) ⇒ Boolean
Two records are equal if they are the same class and have the same values.
148 149 150 |
# File 'lib/key_vortex/record.rb', line 148 def ==(other) self.class == other.class && values == other.values end |
#to_json(*args) ⇒ String
Converts the record into a JSON string appropriate for parsing using the create_additions option. Here is an example of the format that’s produced.
{
"json_class": "ExampleRecord",
"values": {
"key": "foo",
"a": 15,
"b": "bar",
"c": 1000
}
}
167 168 169 170 171 172 |
# File 'lib/key_vortex/record.rb', line 167 def to_json(*args) { JSON.create_id => self.class.name, "values" => @values }.to_json(*args) end |