Class: OsCtld::Attributes

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/osctld/attributes.rb

Overview

Storage of user attributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initializeAttributes

Returns a new instance of Attributes.



16
17
18
19
# File 'lib/osctld/attributes.rb', line 16

def initialize
  init_lock
  @attrs = {}
end

Instance Attribute Details

#attrsObject (readonly, protected)

Returns the value of attribute attrs.



66
67
68
# File 'lib/osctld/attributes.rb', line 66

def attrs
  @attrs
end

Class Method Details

.load(cfg) ⇒ Object

Load attributes from config

Parameters:

  • cfg (Hash)


8
9
10
11
12
# File 'lib/osctld/attributes.rb', line 8

def self.load(cfg)
  ret = new
  cfg.each { |k, v| ret.set(k, v) }
  ret
end

Instance Method Details

#[](name) ⇒ Object



44
45
46
# File 'lib/osctld/attributes.rb', line 44

def [](name)
  inclusively { attrs[key(name)] }
end

#[]=(name, value) ⇒ Object



40
41
42
# File 'lib/osctld/attributes.rb', line 40

def []=(name, value)
  set(key(name), value)
end

#dumpObject

Dump to config



49
50
51
# File 'lib/osctld/attributes.rb', line 49

def dump
  inclusively { attrs.clone }
end

#dupObject



58
59
60
61
62
# File 'lib/osctld/attributes.rb', line 58

def dup
  ret = super
  ret.init_lock
  ret
end

#exportObject

Export attributes to client



54
55
56
# File 'lib/osctld/attributes.rb', line 54

def export
  inclusively { attrs.transform_keys(&:to_sym) }
end

#key(v) ⇒ Object (protected)



68
69
70
# File 'lib/osctld/attributes.rb', line 68

def key(v)
  v.to_s
end

#set(name, value) ⇒ Object

Set an attribute



22
23
24
25
26
27
# File 'lib/osctld/attributes.rb', line 22

def set(name, value)
  k = key(name)
  raise 'invalid attribute name' if /^[^:]+:.+$/ !~ k

  exclusively { attrs[k] = value }
end

#unset(name) ⇒ Object

Unset an attribute



30
31
32
# File 'lib/osctld/attributes.rb', line 30

def unset(name)
  exclusively { attrs.delete(key(name)) }
end

#update(new_attrs) ⇒ Object

Update attributes

Parameters:

  • new_attrs (Hash)


36
37
38
# File 'lib/osctld/attributes.rb', line 36

def update(new_attrs)
  exclusively { new_attrs.each { |k, v| set(key(k), v) } }
end