Class: KBSecret::Record::Abstract Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/kbsecret/record/abstract.rb

Overview

This class is abstract.

Represents an abstract kbsecret record that can be subclassed to produce more useful records.

Direct Known Subclasses

Environment, Login, Unstructured

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, label) ⇒ Abstract

Note:

Creation does not sync the new record; see #sync! for that.

Create a brand new record, associated with a session.

Parameters:

  • session (Session)

    the session to associate with

  • label (Symbol)

    the new record's label



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

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/kbsecret/record/abstract.rb', line 13

def data
  @data
end

#labelObject (readonly)

Returns the value of attribute label.



11
12
13
# File 'lib/kbsecret/record/abstract.rb', line 11

def label
  @label
end

#sessionObject

Returns the value of attribute session.



9
10
11
# File 'lib/kbsecret/record/abstract.rb', line 9

def session
  @session
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



10
11
12
# File 'lib/kbsecret/record/abstract.rb', line 10

def timestamp
  @timestamp
end

#typeObject (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_fieldsObject



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.

Parameters:

  • session (Session)

    the session to associate with

  • hsh (Hash)

    the record's hash-representation

Returns:

  • (Record::AbstractRecord)

    the created 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

.typeString

Returns the record's type.

Examples:

KBSecret::Record::Abstract.type # => "abstract"

Returns:

  • (String)

    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.

Parameters:

  • hsh (Hash)

    the 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

#pathString

Note:

If the record hasn't been synced to disk, this path may not exist yet.

The fully qualified path to the record's file.

Returns:

  • (String)

    the path



84
85
86
# File 'lib/kbsecret/record/abstract.rb', line 84

def path
  File.join(session.directory, "#{label}.json")
end

#sync!void

Note:

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_hHash

Create a hash-representation of the current record.

Returns:

  • (Hash)

    the hash-representation



90
91
92
93
94
95
96
97
# File 'lib/kbsecret/record/abstract.rb', line 90

def to_h
  {
    timestamp: timestamp,
    label: label,
    type: type,
    data: data,
  }
end