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 ⇒ Object
readonly
Returns the value of attribute data.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#session ⇒ Object
Returns the value of attribute session.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .data_field(field) ⇒ Object
- .data_fields ⇒ Object
-
.load!(session, hsh) ⇒ Record::AbstractRecord
private
Load the given hash-representation into a record.
-
.type ⇒ String
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.
-
#path ⇒ String
The fully qualified path to the record's file.
-
#sync! ⇒ void
Write the record's in-memory state to disk.
-
#to_h ⇒ Hash
Create a hash-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.
61 62 63 64 65 66 67 |
# File 'lib/kbsecret/record/abstract.rb', line 61 def initialize(session, label) @session = session @timestamp = Time.now.to_i @label = label @type = self.class.type @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/kbsecret/record/abstract.rb', line 13 def data @data end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
11 12 13 |
# File 'lib/kbsecret/record/abstract.rb', line 11 def label @label end |
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/kbsecret/record/abstract.rb', line 9 def session @session end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
10 11 12 |
# File 'lib/kbsecret/record/abstract.rb', line 10 def @timestamp end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
12 13 14 |
# File 'lib/kbsecret/record/abstract.rb', line 12 def type @type end |
Class Method Details
.data_field(field) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kbsecret/record/abstract.rb', line 16 def data_field(field) @fields ||= [] @fields << 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 sync! end ] end |
.data_fields ⇒ Object
32 33 34 |
# File 'lib/kbsecret/record/abstract.rb', line 32 def data_fields @fields 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.
48 49 50 51 52 53 54 |
# File 'lib/kbsecret/record/abstract.rb', line 48 def load!(session, hsh) instance = allocate instance.session = session instance.initialize_from_hash(hsh) instance end |
.type ⇒ String
Returns the record's type.
39 40 41 |
# File 'lib/kbsecret/record/abstract.rb', line 39 def type name.split("::").last.downcase 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.
73 74 75 76 77 78 |
# File 'lib/kbsecret/record/abstract.rb', line 73 def initialize_from_hash(hsh) @timestamp = hsh[:timestamp] @label = hsh[:label] @type = hsh[:type] @data = hsh[:data] end |
#path ⇒ String
If the record hasn't been synced to disk, this path may not exist yet.
The fully qualified path to the record's file.
84 85 86 |
# File 'lib/kbsecret/record/abstract.rb', line 84 def 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.
102 103 104 105 106 107 |
# File 'lib/kbsecret/record/abstract.rb', line 102 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.
90 91 92 93 94 95 96 97 |
# File 'lib/kbsecret/record/abstract.rb', line 90 def to_h { timestamp: , label: label, type: type, data: data, } end |