Class: RSence::HValue
- Inherits:
-
Object
- Object
- RSence::HValue
- Defined in:
- lib/rsence/value.rb
Overview
HValue is the model for client-server synchronized values. A value contains its payload #data and enough meta-data to define its behavior.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The payload data.
-
#is_valid ⇒ Boolean
(also: #valid, #valid?)
readonly
The validity of the data.
-
#meta ⇒ Object
Value meta-data.
Instance Method Summary collapse
-
#bind(plugin_name, method_name) ⇒ true
Binds the value to a responder.
-
#bound?(plugin_name, method_name) ⇒ Boolean
Checks, if the plugin_name and method_name pairing is already bound with the bind method.
-
#die!(msg = false) ⇒ Object
(also: #die)
Destructor method.
-
#initialize(msg, data, meta = { :name => nil }) ⇒ HValue
constructor
Creates a new client-server automatically synchronized data wrapper object.
- #inspect ⇒ Object
-
#release(plugin_name = false, method_name = false) ⇒ Boolean
(also: #unbind)
Releases the responder of the value, both params as in bind, but optional
method_name
can be omitted, matching all methods bound to theplugin_name
. -
#release_all ⇒ true
Releases all responders.
-
#set(msg, data, dont_tell_client = false) ⇒ Object
Sets the data of the value, the change will be synced with the client.
-
#set_key(msg, key, data, dont_tell_client = false) ⇒ Object
Sets the key of the hash data of the value, the change will be synced with the client.
Constructor Details
#initialize(msg, data, meta = { :name => nil }) ⇒ HValue
Creates a new client-server automatically synchronized data wrapper object.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rsence/value.rb', line 81 def initialize( msg, data, = { :name => nil } ) ## Get an unique integer id for the value if RSence.args[:debug] and [:name] and not msg.valuemanager.id_exists?( msg, [:name] ) @value_id = [:name] else @value_id = msg.valuemanager.ses_unique_id( msg ) end @meta = ## set the data of the hvalue set( msg, data, true ) ## the @sync flag is raised, when the client data is older than the server data @sync = false ## the @is_valid flag is lowered, when the client data is newer than the server data @is_valid = true ## Bind the value to the value manager and report it to the client add( msg ) ## storage for validator bindings @members = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
The payload data. Use #set to change.
30 31 32 |
# File 'lib/rsence/value.rb', line 30 def data @data end |
#is_valid ⇒ Boolean Also known as: valid, valid?
The validity of the data. Defaults to false, when the data comes from the client.
20 21 22 |
# File 'lib/rsence/value.rb', line 20 def is_valid @is_valid end |
#meta ⇒ Object
Value meta-data. The third constructor parameter
75 76 77 |
# File 'lib/rsence/value.rb', line 75 def @meta end |
Instance Method Details
#bind(plugin_name, method_name) ⇒ true
Binds the value to a responder. The responder is an instance of Plugin or GUIPlugin. Responders are called once, when the client requests the data to be changed. Responders must return true
, if they accept the change. Otherwise the data is treated as invalid. Responders must respond to exactly two parameters: ( (Message) msg
, (HValue) value
)
117 118 119 120 121 122 123 |
# File 'lib/rsence/value.rb', line 117 def bind( plugin_name, method_name ) plugin_name = plugin_name.to_sym unless plugin_name.class == Symbol method_name = method_name.to_sym unless method_name.class == Symbol @members[plugin_name] = [] unless @members.has_key?( plugin_name ) @members[plugin_name].push( method_name ) unless @members[plugin_name].include?( method_name ) return true end |
#bound?(plugin_name, method_name) ⇒ Boolean
Checks, if the plugin_name and method_name pairing is already bound with the bind method. Returns true or false.
126 127 128 129 130 131 |
# File 'lib/rsence/value.rb', line 126 def bound?( plugin_name, method_name ) plugin_name = plugin_name.to_sym unless plugin_name.class == Symbol method_name = method_name.to_sym unless method_name.class == Symbol return false unless @members.has_key?(plugin_name) return @members[plugin_name].include?(method_name) end |
#die!(msg = false) ⇒ Object Also known as: die
Destructor method. If msg is supplied, deletes the client representation too.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/rsence/value.rb', line 259 def die!( msg=false ) release_all # get the value storage from the session data session_values = msg.session[:values][:by_id] ## Store the object here session_values.delete( @value_id ) if msg and not @is_new_to_client msg.reply_value( :del, @value_id ) end end |
#inspect ⇒ Object
275 276 277 |
# File 'lib/rsence/value.rb', line 275 def inspect "#<RSence::HValue value_id:#{@value_id.inspect}, valid: #{@is_valid.inspect}, sync: #{@sync.inspect}, is_new_to_client: #{@is_new_to_client.inspect}, meta: #{@meta.inspect[0..100]}, data: #{@data.inspect[0..100]} ...>" end |
#release(plugin_name = false, method_name = false) ⇒ Boolean Also known as: unbind
Releases the responder of the value, both params as in bind, but optional method_name
can be omitted, matching all methods bound to the plugin_name
.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rsence/value.rb', line 137 def release( plugin_name=false, method_name=false ) plugin_name = plugin_name.to_sym if plugin_name.class == String method_name = method_name.to_sym if method_name.class == String return release_all if not plugin_name and not method_name return false unless @members.has_key?( plugin_name ) if not method_name @members.delete( plugin_name ) else @members[plugin_name].slice!(@members[plugin_name].index( method_name )) if @members[plugin_name].include?(method_name) if @members[plugin_name].empty? @members.delete( plugin_name ) end end return true end |
#release_all ⇒ true
Releases all responders.
155 156 157 158 |
# File 'lib/rsence/value.rb', line 155 def release_all @members = {} return true end |
#set(msg, data, dont_tell_client = false) ⇒ Object
Sets the data of the value, the change will be synced with the client.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rsence/value.rb', line 212 def set( msg, data, dont_tell_client=false ) @data = data # won't tell the client about the change, usually not needed unless dont_tell_client ## update the flags @sync = false @is_valid = true add_to_sync( msg ) end end |
#set_key(msg, key, data, dont_tell_client = false) ⇒ Object
Sets the key of the hash data of the value, the change will be synced with the client.
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rsence/value.rb', line 231 def set_key( msg, key, data, dont_tell_client=false ) @data[key] = data # won't tell the client about the change, usually not needed unless dont_tell_client ## update the flags @sync = false @is_valid = true add_to_sync( msg ) end end |