• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

notEthan / jsi / 9708116927

24 Jun 2024 04:43AM UTC coverage: 98.097% (+0.9%) from 97.218%
9708116927

push

github

notEthan
v0.8.0

5877 of 5991 relevant lines covered (98.1%)

149117.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/lib/jsi/jsi_coder.rb
1
# frozen_string_literal: true
2

3
module JSI
14✔
4
  # this is an ActiveRecord serialization coder intended to serialize between
5
  # JSON-compatible objects on the database side, and a JSI instance loaded on
6
  # the model attribute.
7
  #
8
  # on its own this coder is useful with a JSON database column. in order to
9
  # serialize further to a string of JSON, or to YAML, the gem `arms` allows
10
  # coders to be chained together. for example, for a table `foos` and a column
11
  # `preferences_json` which is an actual json column, and `preferences_txt`
12
  # which is a string:
13
  #
14
  #     Preferences = JSI.new_schema_module(preferences_json_schema)
15
  #     class Foo < ActiveRecord::Base
16
  #       # as a single serializer, loads a Preferences instance from a json column
17
  #       serialize 'preferences_json', JSI::JSICoder.new(Preferences)
18
  #
19
  #       # for a text column, arms_serialize will go from JSI to JSON-compatible
20
  #       # objects to a string. the symbol `:jsi` is a shortcut for JSI::JSICoder.
21
  #       arms_serialize 'preferences_txt', [:jsi, Preferences], :json
22
  #     end
23
  #
24
  # the column data may be either a single instance of the schema class
25
  # (represented as one json object) or an array of them (represented as a json
26
  # array of json objects), indicated by the keyword argument `array`.
27
  class JSICoder
14✔
28
    # @param schema [#new_jsi] a Schema, SchemaSet, or JSI schema module. #load
29
    #   will instantiate column data using the JSI schemas represented.
30
    # @param array [Boolean] whether the dumped data represent one instance of the schema,
31
    #   or an array of them. note that it may be preferable to simply use an array schema.
32
    # @param jsi_opt [Hash] keyword arguments to pass to {Schema#new_jsi} when loading
33
    # @param as_json_opt [Hash] keyword arguments to pass to `#as_json` when dumping
34
    def initialize(schema, array: false, jsi_opt: Util::EMPTY_HASH, as_json_opt: Util::EMPTY_HASH)
14✔
35
      unless schema.respond_to?(:new_jsi)
238✔
36
        raise(ArgumentError, "schema param does not respond to #new_jsi: #{schema.inspect}")
×
37
      end
38
      @schema = schema
238✔
39
      @array = array
238✔
40
      @jsi_opt = jsi_opt
238✔
41
      @as_json_opt = as_json_opt
238✔
42
    end
43

44
    # loads the database column to JSI instances of our schema
45
    #
46
    # @param data [Object, Array, nil] the dumped schema instance(s) of the JSI(s)
47
    # @return [JSI::Base, Array<JSI::Base>, nil] the JSI or JSIs containing the schema
48
    #   instance(s), or nil if data is nil
49
    def load(data)
14✔
50
      return nil if data.nil?
154✔
51

52
      if @array
140✔
53
        unless data.respond_to?(:to_ary)
42✔
54
          raise TypeError, "expected array-like column data; got: #{data.class}: #{data.inspect}"
14✔
55
        end
56
        data.to_ary.map { |el| load_object(el) }
56✔
57
      else
58
        load_object(data)
98✔
59
      end
60
    end
61

62
    # dumps the object for the database
63
    # @param object [JSI::Base, Array<JSI::Base>, nil] the JSI or array of JSIs containing
64
    #   the schema instance(s)
65
    # @return [Object, Array, nil] the schema instance(s) of the JSI(s), or nil if object is nil
66
    def dump(object)
14✔
67
      return nil if object.nil?
112✔
68

69
      if @array
98✔
70
        unless object.respond_to?(:to_ary)
14✔
71
          raise(TypeError, "expected array-like attribute; got: #{object.class}: #{object.inspect}")
×
72
        end
73
        object.to_ary.map do |el|
14✔
74
          dump_object(el)
28✔
75
        end
76
      else
77
        dump_object(object)
84✔
78
      end
79
    end
80

81
    private
14✔
82
    # @param data [Object]
83
    # @return [JSI::Base]
84
    def load_object(data)
14✔
85
      @schema.new_jsi(data, **@jsi_opt)
126✔
86
    end
87

88
    # @param object [JSI::Base, Object]
89
    # @return [Object]
90
    def dump_object(object)
14✔
91
      JSI::Util.as_json(object, **@as_json_opt)
112✔
92
    end
93
  end
94
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc