Class: KeyVortex::Record

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Record

Values can optionally be specified when the object is created.

Examples:

ExampleRecord.new(
  a: 15,
  b: "foo",
  c: 1000,
)


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

#keyString

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.

Returns:

  • (String)


118
# File 'lib/key_vortex/record.rb', line 118

field :key, String, length: 36

#valuesHash (readonly)

A hash of all of the attributes, in which the keys are symbols.

Examples:

{
  a: 15,
  b: "foo",
  c: 1000,
}

Returns:

  • (Hash)


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.

Returns:

  • (Field)

    The created field



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

.fieldsArray

Returns All of the fields declared for this class.

Returns:

  • (Array)

    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.

Returns:

  • (Record)

    Subclasses will return an instance of themselves



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.

Returns:

  • (Boolean)


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
  }
}

Returns:

  • (String)

    JSON representation of the object



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