Sha256: f3ec05326c5247ce8def128d1b1f754c69f1ae75442d7a8e8e247c55bc7dcbc7

Contents?: true

Size: 950 Bytes

Versions: 17

Compression:

Stored size: 950 Bytes

Contents

module NxtSupport
  module IndifferentlyAccessibleJsonAttrs
    extend ActiveSupport::Concern

    class JsonSerializer
      class << self
        def load(str)
          indifferent_accessable(JSON.load(str))
        end

        def dump(obj)
          JSON.dump(obj)
        end

        private

        def indifferent_accessable(obj)
          if obj.is_a?(Array)
            obj.map! { |o| indifferent_accessable(o) }
          elsif obj.is_a?(Hash)
            obj.with_indifferent_access
          elsif obj.is_a?(NilClass)
            obj
          else
            raise ArgumentError, "Cant deserialize '#{obj}'"
          end
        end
      end
    end

    class_methods do
      def indifferently_accessible_json_attrs(*attrs)
        attrs.each do |attr|
          serialize attr, JsonSerializer
        end
      end

      alias_method :indifferently_accessible_json_attr, :indifferently_accessible_json_attrs
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
nxt_support-0.5.0 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.4.3 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.4.2 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.4.1 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.4.0 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.3.0 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.2.1 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.2.0 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.16 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.15 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.9 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.8 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.7 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.6 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.5 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.4 lib/nxt_support/models/indifferently_accessible_json_attrs.rb
nxt_support-0.1.3 lib/nxt_support/models/indifferently_accessible_json_attrs.rb