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.

Parameters:

  • session (Session)

    the session to associate with

  • label (String, Symbol)

    the new record's label



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)

Returns the record's data.

Returns:

  • (Hash)

    the record's data



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

def data
  @data
end

#labelString (readonly)

Returns the record's label.

Returns:

  • (String)

    the record's label



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

def label
  @label
end

#pathString (readonly)

Returns the fully qualified path to the record in KBFS.

Returns:

  • (String)

    the fully qualified path to the record in KBFS



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

def path
  @path
end

#sessionSession

Returns the session associated with the record.

Returns:

  • (Session)

    the session associated with the record



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

def session
  @session
end

#timestampInteger (readonly)

Returns the UNIX timestamp marking the record's last modification.

Returns:

  • (Integer)

    the UNIX timestamp marking the record's last modification



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

def timestamp
  @timestamp
end

#typeSymbol (readonly)

Returns the record's type.

Returns:

  • (Symbol)

    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.

Parameters:

  • field (Symbol)

    the new field's name



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>

Returns all data fields for the record class.

Returns:

  • (Array<Symbol>)

    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.

Parameters:

  • field (Symbol)

    the new field's name



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.

Parameters:

  • session (Session)

    the session to associate with

  • hsh (Hash)

    the record's hash-representation

Returns:

  • (Record::AbstractRecord)

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

Returns:

  • (Symbol)

    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.

Parameters:

  • hsh (Hash)

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

Returns:

  • (Hash)

    the hash-representation



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.

Returns:

  • (String)

    the string representation



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

def to_s
  @label
end