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, Snippet, Todo, 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.



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

#dataHash (readonly)



24
25
26
# File 'lib/kbsecret/record/abstract.rb', line 24

def data
  @data
end

#labelString (readonly)



18
19
20
# File 'lib/kbsecret/record/abstract.rb', line 18

def label
  @label
end

#pathString (readonly)



27
28
29
# File 'lib/kbsecret/record/abstract.rb', line 27

def path
  @path
end

#sessionSession



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

def session
  @session
end

#timestampInteger (readonly)



15
16
17
# File 'lib/kbsecret/record/abstract.rb', line 15

def timestamp
  @timestamp
end

#typeSymbol (readonly)



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_fieldsArray<Symbol>



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

.typeSymbol

Returns the record's type.

Examples:

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


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

Note:

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_hHash

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: timestamp,
    label: label,
    type: type,
    data: data,
  }
end

#to_sString

Create a string representation of the current record.



114
115
116
# File 'lib/kbsecret/record/abstract.rb', line 114

def to_s
  @label
end