Class: Serde::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/serde.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Serializer

Returns a new instance of Serializer.



60
61
62
# File 'lib/serde.rb', line 60

def initialize(object)
  @args = self.class.get_schema.keys.map { |k| object.public_send(k) }
end

Class Attribute Details

.descendantsObject (readonly)

Returns the value of attribute descendants.



20
21
22
# File 'lib/serde.rb', line 20

def descendants
  @descendants
end

Class Method Details

.compile!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/serde.rb', line 30

def compile!
  raise 'already compiled' if @compiled

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      SerializerGenerator.call(dir, self)

      root_path = File.expand_path('..', __dir__)
      rust_path = File.expand_path('../rust', __dir__)
      bin_path = File.expand_path('../bin', __dir__)

      # rubocop:disable Metrics/LineLength
      `mkdir -p #{rust_path}/target/release`
      `cd #{rust_path}; #{bin_path}/rustc --edition=2018 --crate-name serde_rb src/lib.rs --crate-type staticlib --crate-type cdylib --emit=dep-info,link -C opt-level=3 -C metadata=aff563bf4af79579 --out-dir #{rust_path}/target/release -L dependency=#{rust_path}/target/release/deps --extern serde=#{rust_path}/target/release/deps/libserde-6a9e4ab445963d7f.rlib --extern serde_derive=#{rust_path}/target/release/deps/libserde_derive-20596753c2ed9015.dylib --extern serde_json=#{rust_path}/target/release/deps/libserde_json-489658cb61f64325.rlib -L #{root_path}/.rustc/rust-std-beta-x86_64-apple-darwin/rust-std-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib`
      # rubocop:enable Metrics/LineLength
      `cp #{rust_path}/target/release/libserde_rb*.a #{dir}/serde_rb/libserde_rb.a`
      `cd #{dir}/serde_rb; ruby extconf.rb; make clean; make`

      require_relative "#{dir}/serde_rb/serde_rb"
    end
  end

  @compiled = true
end

.get_schemaObject



55
56
57
# File 'lib/serde.rb', line 55

def get_schema
  @schema
end

.inherited(klass) ⇒ Object



22
23
24
# File 'lib/serde.rb', line 22

def inherited(klass)
  @descendants << klass
end

.schema(**attrs) ⇒ Object



26
27
28
# File 'lib/serde.rb', line 26

def schema(**attrs)
  @schema = attrs
end

Instance Method Details

#to_jsonObject



64
65
66
# File 'lib/serde.rb', line 64

def to_json
  internal_to_json(*@args)
end