Class: Keybox::Storage::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/keybox/storage/record.rb

Overview

Record is very similar to an OStruct but it keeps track of the last access, creation and modification times of the object. Each Record also has a UUID for unique identification.

rec = Keybox::Storage::Record.new

rec.modified?                   # => false
rec.some_field = "some value"   # => "some value"
rec.modified?                   # => true
rec.modification_time           # => Time
rec.data_members                # => { :some_field => "some value" }
rec.uuid.to_s                   # => "4b25074d-d3c5-23cd-bf9f-e28d8c8d453d"

The creation_time, modification_time and last_access_time of each field in the Record is recorded.

The UUID is create when the class is instantiated.

Direct Known Subclasses

AccountEntry, Container

Constant Summary collapse

PROTECTED_METHODS =
[ :creation_time=, :modification_time=, :last_access_time=, 
:uuid=, :data_members=, :modified, ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecord

Returns a new instance of Record.



33
34
35
36
37
38
39
40
# File 'lib/keybox/storage/record.rb', line 33

def initialize
    @creation_time     = Time.now
    @modification_time = @creation_time.dup
    @last_access_time  = @creation_time.dup
    @uuid              = Keybox::UUID.new
    @data_members      = Hash.new
    @modified          = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/keybox/storage/record.rb', line 65

def method_missing(method_id, *args)
    method_name = method_id.id2name
    member_sym  = method_name.gsub(/=/,'').to_sym

    # guard against assigning to the time data members and
    # the data_members element
    if PROTECTED_METHODS.include?(method_id) then
        raise NoMethodError, "invalid method #{method_name} for #{self.class.name}", caller(1)
    end

    # if the method ends with '=' and has a single argument,
    # then convert the name to the appropriate data member
    # and store the argument in the hash. 
    if method_name[-1].chr == "=" then
        raise ArgumentError, "'#{method_name}' requires one and only one argument", caller(1) unless args.size == 1
        @data_members[member_sym] = args[0]
        @modified = true
        @modification_time        = Time.now
        @last_access_time         = @modification_time.dup
    elsif args.size == 0 then
        @last_access_time = Time.now
        @data_members[member_sym]
    else
        raise NoMethodError, "undefined method #{method_name} for #{self.class.name}", caller(1)
    end
end

Instance Attribute Details

#creation_timeObject (readonly)

Returns the value of attribute creation_time.



25
26
27
# File 'lib/keybox/storage/record.rb', line 25

def creation_time
  @creation_time
end

#data_membersObject (readonly)

Returns the value of attribute data_members.



29
30
31
# File 'lib/keybox/storage/record.rb', line 29

def data_members
  @data_members
end

#last_access_timeObject (readonly)

Returns the value of attribute last_access_time.



27
28
29
# File 'lib/keybox/storage/record.rb', line 27

def last_access_time
  @last_access_time
end

#modification_timeObject (readonly)

Returns the value of attribute modification_time.



26
27
28
# File 'lib/keybox/storage/record.rb', line 26

def modification_time
  @modification_time
end

#uuidObject (readonly)

Returns the value of attribute uuid.



28
29
30
# File 'lib/keybox/storage/record.rb', line 28

def uuid
  @uuid
end

Instance Method Details

#==(other) ⇒ Object



96
97
98
# File 'lib/keybox/storage/record.rb', line 96

def ==(other)
    self.eql?(other)
end

#data_member_namesObject

an array of the data member names



53
54
55
# File 'lib/keybox/storage/record.rb', line 53

def data_member_names
    @data_members.keys.collect { |n| n.to_s }
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/keybox/storage/record.rb', line 100

def eql?(other)
    if other.kind_of?(Keybox::Storage::Record) then
        self.uuid == other.uuid
    else
        self.uuid == other
    end
end

#modified=(value) ⇒ Object

this is here so that after this class has been serialized to a file the container can mark it as clean. The class should take care of marking itself dirty.



61
62
63
# File 'lib/keybox/storage/record.rb', line 61

def modified=(value)
    @modified = value
end

#modified?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/keybox/storage/record.rb', line 42

def modified?
    # since this class can be loaded from a YAML file and
    # modified is not stored in the serialized format, if
    # @modified is not initialized, initialize it.
    if not instance_variables.include?("@modified") then
        @modified = false
    end
    @modified
end

#to_yaml_propertiesObject



92
93
94
# File 'lib/keybox/storage/record.rb', line 92

def to_yaml_properties
    %w{ @creation_time @modification_time @last_access_time @data_members @uuid }
end