Sha256: 3d6390d34db576635a7abb0d2fbd123c431cd5df2e64f26ef4c5f845293f9d57
Contents?: true
Size: 1.62 KB
Versions: 4
Compression:
Stored size: 1.62 KB
Contents
# frozen_string_literal: true require "key_vortex" require "key_vortex/constraint" require "key_vortex/field" require "key_vortex/limitation" class KeyVortex class Record def self.fields field_hash.values end def self.field_hash @field_hash ||= {} end def self.field(name, type, **constraints_hash) register_field(KeyVortex::Field.new(name, type, **constraints_hash)) end def self.field_constraints(field) @field_hash[field] end def self.register_field(field) field_hash[field.name] = field field.enable_json_additions define_getter(field) define_setter(field) end def self.define_getter(field) define_method(field.name) { @values[field.name] } end def self.define_setter(field) define_method("#{field.name}=") do |val| raise KeyVortex::Error, "Invalid value #{val} for #{field.name}" unless field.accepts?(val) @values[field.name] = val end end def self.inherited(subclass) super fields.each do |field| subclass.register_field(field) end end # Long enough to accomodate a GUID field :key, String, length: 36 attr_reader :values def initialize(values = {}) @values = {} values.each do |name, value| send("#{name}=", value) end end def ==(other) self.class == other.class && values == other.values end def to_json(*args) { JSON.create_id => self.class.name, "values" => @values }.to_json(*args) end def self.json_create(object) new(object["values"]) end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
key-vortex-0.2.5 | lib/key_vortex/record.rb |
key-vortex-0.2.4 | lib/key_vortex/record.rb |
key-vortex-0.2.3 | lib/key_vortex/record.rb |
key-vortex-0.2.2 | lib/key_vortex/record.rb |