Class: PHPSession::Decoder
- Inherits:
-
Object
- Object
- PHPSession::Decoder
- Defined in:
- lib/php_session/decoder.rb
Defined Under Namespace
Modules: State
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#encoding_option ⇒ Object
readonly
Returns the value of attribute encoding_option.
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #consume_array ⇒ Object
- #decode ⇒ Object
- #elements_count ⇒ Object
- #extract_stack(count) ⇒ Object
- #finished_array ⇒ Object
- #in_array ⇒ Object
-
#initialize(string, encoding, encoding_option) ⇒ Decoder
constructor
A new instance of Decoder.
- #process_empty_array_value ⇒ Object
- #process_value(value) ⇒ Object
- #start_array(length, klass = nil) ⇒ Object
Constructor Details
#initialize(string, encoding, encoding_option) ⇒ Decoder
Returns a new instance of Decoder.
11 12 13 14 15 16 17 18 19 |
# File 'lib/php_session/decoder.rb', line 11 def initialize(string, encoding, encoding_option) @encoding = encoding @encoding_option = encoding_option @buffer = string @data = {} @state = State::VarName @stack = [] @array = [] # array of array end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
4 5 6 |
# File 'lib/php_session/decoder.rb', line 4 def array @array end |
#buffer ⇒ Object
Returns the value of attribute buffer.
4 5 6 |
# File 'lib/php_session/decoder.rb', line 4 def buffer @buffer end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
5 6 7 |
# File 'lib/php_session/decoder.rb', line 5 def encoding @encoding end |
#encoding_option ⇒ Object (readonly)
Returns the value of attribute encoding_option.
5 6 7 |
# File 'lib/php_session/decoder.rb', line 5 def encoding_option @encoding_option end |
#stack ⇒ Object
Returns the value of attribute stack.
4 5 6 |
# File 'lib/php_session/decoder.rb', line 4 def stack @stack end |
#state ⇒ Object
Returns the value of attribute state.
4 5 6 |
# File 'lib/php_session/decoder.rb', line 4 def state @state end |
Class Method Details
.decode(string, encoding = nil, encoding_option = {}) ⇒ Object
7 8 9 |
# File 'lib/php_session/decoder.rb', line 7 def self.decode(string, encoding = nil, encoding_option = {}) self.new(string, encoding, encoding_option).decode end |
Instance Method Details
#consume_array ⇒ Object
43 44 45 |
# File 'lib/php_session/decoder.rb', line 43 def consume_array @array[0][:consumed_count] += 1 end |
#decode ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/php_session/decoder.rb', line 21 def decode loop do break if @buffer.size == 0 @state.parse(self) end @data end |
#elements_count ⇒ Object
37 38 39 |
# File 'lib/php_session/decoder.rb', line 37 def elements_count @array[0][:length] end |
#extract_stack(count) ⇒ Object
50 51 52 53 54 |
# File 'lib/php_session/decoder.rb', line 50 def extract_stack(count) poped = @stack[(@stack.size - count) .. -1] @stack = @stack.slice(0, @stack.size - count) poped end |
#finished_array ⇒ Object
46 47 48 |
# File 'lib/php_session/decoder.rb', line 46 def finished_array @array[0][:length] * 2 == @array[0][:consumed_count] end |
#in_array ⇒ Object
40 41 42 |
# File 'lib/php_session/decoder.rb', line 40 def in_array @array.size > 0 end |
#process_empty_array_value ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/php_session/decoder.rb', line 56 def process_empty_array_value array_which_finished = @array.shift klass = array_which_finished[:klass]; if klass struct = define_or_find_struct(klass, []) process_value(struct.new) else process_value({}) end @state = State::ArrayEnd end |
#process_value(value) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/php_session/decoder.rb', line 68 def process_value(value) if in_array @stack.push(value) consume_array if finished_array array_which_finished = @array.shift key_values_array = extract_stack(array_which_finished[:length] * 2) key_values = key_values_array.group_by.with_index{|el, i| i%2 == 0 ? :key : :value} klass = array_which_finished[:klass]; if klass struct = define_or_find_struct(klass, key_values[:key]) process_value(struct.new(*key_values[:value])) @state = State::ArrayEnd @state.parse(self) else hash = {} key_values_array.each_slice(2) do |kv| hash[kv[0]] = kv[1] end process_value(hash) @state = State::ArrayEnd @state.parse(self) end else @state = State::VarType end else varname = @stack.pop; @data[varname] = value; @state = State::VarName end end |
#start_array(length, klass = nil) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/php_session/decoder.rb', line 29 def start_array(length, klass = nil) # [length, comsumed?, class] @array.unshift({ :length => length, :consumed_count => 0, :klass => klass }) end |