File: helpers.rb

package info (click to toggle)
ruby-jsonb-accessor 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 316 kB
  • sloc: ruby: 1,819; makefile: 10; sh: 5
file content (69 lines) | stat: -rw-r--r-- 1,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module JsonbAccessor
  module Helpers
    module_function

    def active_record_default_timezone
      ActiveRecord.try(:default_timezone) || ActiveRecord::Base.default_timezone
    end

    # Replaces all keys in `attributes` that have a defined store_key with the store_key
    def convert_keys_to_store_keys(attributes, store_key_mapping)
      attributes.stringify_keys.transform_keys do |key|
        store_key_mapping[key] || key
      end
    end

    # Replaces all keys in `attributes` that have a defined store_key with the named key (alias)
    def convert_store_keys_to_keys(attributes, store_key_mapping)
      convert_keys_to_store_keys(attributes, store_key_mapping.invert)
    end

    def deserialize_value(value, attribute_type)
      return value if value.blank?

      if attribute_type == :datetime
        value = if value.is_a?(Array)
                  value.map { |v| parse_date(v) }
                else
                  parse_date(value)
                end
      end

      value
    end

    # Parse datetime based on the configured default_timezone
    def parse_date(datetime)
      if active_record_default_timezone == :utc
        Time.find_zone("UTC").parse(datetime).in_time_zone
      else
        Time.zone.parse(datetime)
      end
    end

    def define_attribute_name(json_attribute, name, prefix, suffix)
      accessor_prefix =
        case prefix
        when String, Symbol
          "#{prefix}_"
        when TrueClass
          "#{json_attribute}_"
        else
          ""
        end
      accessor_suffix =
        case suffix
        when String, Symbol
          "_#{suffix}"
        when TrueClass
          "_#{json_attribute}"
        else
          ""
        end

      "#{accessor_prefix}#{name}#{accessor_suffix}"
    end
  end
end