Class: Chainer::Serializers::MarshalSerializer

Inherits:
Chainer::Serializer show all
Defined in:
lib/chainer/serializers/marshal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chainer::Serializer

#save

Constructor Details

#initialize(target: nil, path: "") ⇒ MarshalSerializer

Returns a new instance of MarshalSerializer.



16
17
18
19
# File 'lib/chainer/serializers/marshal.rb', line 16

def initialize(target: nil, path: "")
  @target = target.nil? ? {} : target
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/chainer/serializers/marshal.rb', line 4

def path
  @path
end

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/chainer/serializers/marshal.rb', line 4

def target
  @target
end

Class Method Details

.save_file(file_path, obj) ⇒ Object

Parameters:

  • file_path (string)

    Target file path

  • obj (Object)

    Object to be serialized



8
9
10
11
12
13
14
# File 'lib/chainer/serializers/marshal.rb', line 8

def self.save_file(file_path, obj)
  s = self.new
  s.save(obj)
  File.open(file_path, 'wb') do |f|
    Marshal.dump(s.target, f)
  end
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
# File 'lib/chainer/serializers/marshal.rb', line 21

def [](key)
  self.class.new(target: @target, path: File.join(@path, key, '/'))
end

#call(key, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chainer/serializers/marshal.rb', line 25

def call(key, value)
  ret = value
  if value.is_a?(TrueClass)
    arr = Numo::Bit[1]
  elsif value.is_a?(FalseClass)
    arr = Numo::Bit[0]
  elsif value.instance_of?(String) || value.nil?
    arr = value
  else
    arr = Numo::NArray.cast(value)
  end
  @target[File.join(@path, key)] = arr
  ret
end