Class: OsCtld::Attributes
- Inherits:
-
Object
- Object
- OsCtld::Attributes
- Includes:
- Lockable
- Defined in:
- lib/osctld/attributes.rb
Overview
Storage of user attributes
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
protected
Returns the value of attribute attrs.
Class Method Summary collapse
-
.load(cfg) ⇒ Object
Load attributes from config.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#dump ⇒ Object
Dump to config.
- #dup ⇒ Object
-
#export ⇒ Object
Export attributes to client.
-
#initialize ⇒ Attributes
constructor
A new instance of Attributes.
- #key(v) ⇒ Object protected
-
#set(name, value) ⇒ Object
Set an attribute.
-
#unset(name) ⇒ Object
Unset an attribute.
-
#update(new_attrs) ⇒ Object
Update attributes.
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
#initialize ⇒ Attributes
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
#attrs ⇒ Object (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
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 |
#dump ⇒ Object
Dump to config
49 50 51 |
# File 'lib/osctld/attributes.rb', line 49 def dump inclusively { attrs.clone } end |
#dup ⇒ Object
58 59 60 61 62 |
# File 'lib/osctld/attributes.rb', line 58 def dup ret = super ret.init_lock ret end |
#export ⇒ Object
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
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 |