Class: KBSecret::Record::Abstract Abstract
- Inherits:
-
Object
- Object
- KBSecret::Record::Abstract
- Defined in:
- lib/kbsecret/record/abstract.rb
Overview
Represents an abstract kbsecret record that can be subclassed to produce more useful records.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
The record's data.
-
#label ⇒ String
readonly
The record's label.
-
#path ⇒ String
readonly
The fully qualified path to the record in KBFS.
-
#session ⇒ Session
The session associated with the record.
-
#timestamp ⇒ Integer
readonly
The UNIX timestamp marking the record's last modification.
-
#type ⇒ Symbol
readonly
The record's type.
Class Method Summary collapse
-
.data_field(field) ⇒ void
Add a field to the record's data.
-
.data_fields ⇒ Array<Symbol>
All data fields for the record class.
-
.gen_methods(field) ⇒ void
Generate the methods used to access a given field.
-
.load!(session, hsh) ⇒ Record::AbstractRecord
private
Load the given hash-representation into a record.
-
.type ⇒ Symbol
The record's type.
Instance Method Summary collapse
-
#initialize(session, label) ⇒ Abstract
constructor
Create a brand new record, associated with a session.
-
#initialize_from_hash(hsh) ⇒ void
private
Fill in instance fields from a record's hash-representation.
-
#sync! ⇒ void
Write the record's in-memory state to disk.
-
#to_h ⇒ Hash
Create a hash-representation of the current record.
-
#to_s ⇒ String
Create a string representation of the current record.
Constructor Details
#initialize(session, label) ⇒ Abstract
Creation does not sync the new record; see #sync! for that.
Create a brand new record, associated with a session.
91 92 93 94 95 96 97 98 |
# File 'lib/kbsecret/record/abstract.rb', line 91 def initialize(session, label) @session = session @timestamp = Time.now.to_i @label = label.to_s @type = self.class.type @data = {} @path = File.join(session.directory, "#{label}.json") end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Returns the record's data.
24 25 26 |
# File 'lib/kbsecret/record/abstract.rb', line 24 def data @data end |
#label ⇒ String (readonly)
Returns the record's label.
18 19 20 |
# File 'lib/kbsecret/record/abstract.rb', line 18 def label @label end |
#path ⇒ String (readonly)
Returns the fully qualified path to the record in KBFS.
27 28 29 |
# File 'lib/kbsecret/record/abstract.rb', line 27 def path @path end |
#session ⇒ Session
Returns the session associated with the record.
12 13 14 |
# File 'lib/kbsecret/record/abstract.rb', line 12 def session @session end |
#timestamp ⇒ Integer (readonly)
Returns the UNIX timestamp marking the record's last modification.
15 16 17 |
# File 'lib/kbsecret/record/abstract.rb', line 15 def @timestamp end |
#type ⇒ Symbol (readonly)
Returns the record's type.
21 22 23 |
# File 'lib/kbsecret/record/abstract.rb', line 21 def type @type end |
Class Method Details
.data_field(field) ⇒ void
This method returns an undefined value.
Add a field to the record's data.
33 34 35 36 37 38 |
# File 'lib/kbsecret/record/abstract.rb', line 33 def data_field(field) @fields ||= [] @fields << field gen_methods field end |
.data_fields ⇒ Array<Symbol>
Returns all data fields for the record class.
58 59 60 |
# File 'lib/kbsecret/record/abstract.rb', line 58 def data_fields @fields end |
.gen_methods(field) ⇒ void
This method returns an undefined value.
Generate the methods used to access a given field.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/kbsecret/record/abstract.rb', line 43 def gen_methods(field) class_eval %[ def #{field} @data[self.class.type.to_sym]["#{field}".to_sym] end def #{field}=(val) @data[self.class.type.to_sym]["#{field}".to_sym] = val @timestamp = Time.now.to_i sync! end ] end |
.load!(session, hsh) ⇒ Record::AbstractRecord
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load the given hash-representation into a record.
78 79 80 81 82 83 84 |
# File 'lib/kbsecret/record/abstract.rb', line 78 def load!(session, hsh) instance = allocate instance.session = session instance.initialize_from_hash(hsh) instance end |
.type ⇒ Symbol
Returns the record's type.
65 66 67 68 69 70 71 |
# File 'lib/kbsecret/record/abstract.rb', line 65 def type name.split("::") # ["Foo", "BarBaz"] .last # "BarBaz" .gsub(/([^A-Z])([A-Z]+)/, '\1_\2') # "Bar_Baz" .downcase # "bar_baz" .to_sym # :bar_baz end |
Instance Method Details
#initialize_from_hash(hsh) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Fill in instance fields from a record's hash-representation.
104 105 106 107 108 109 110 |
# File 'lib/kbsecret/record/abstract.rb', line 104 def initialize_from_hash(hsh) @timestamp = hsh[:timestamp] @label = hsh[:label] @type = hsh[:type].to_sym @data = hsh[:data] @path = File.join(session.directory, "#{label}.json") end |
#sync! ⇒ void
Every sync updates the record's timestamp.
This method returns an undefined value.
Write the record's in-memory state to disk.
132 133 134 135 136 137 |
# File 'lib/kbsecret/record/abstract.rb', line 132 def sync! # bump the timestamp every time we sync @timestamp = Time.now.to_i File.write(path, JSON.pretty_generate(to_h)) end |
#to_h ⇒ Hash
Create a hash-representation of the current record.
120 121 122 123 124 125 126 127 |
# File 'lib/kbsecret/record/abstract.rb', line 120 def to_h { timestamp: , label: label, type: type, data: data, } end |
#to_s ⇒ String
Create a string representation of the current record.
114 115 116 |
# File 'lib/kbsecret/record/abstract.rb', line 114 def to_s @label end |