Sha256: c3a0cd4ef905cbfc83fc78d753e2603f6881ccca466e73c1f1e30e411760a336

Contents?: true

Size: 1004 Bytes

Versions: 15

Compression:

Stored size: 1004 Bytes

Contents

require 'ardm/property/text'
require 'ardm/property/support/dirty_minder'
require 'multi_json'

module Ardm
  class Property
    class Json < Text
      load_as ::Object

      def load(value)
        if value.nil? || value_loaded?(value)
          value
        elsif value.is_a?(::String)
          typecast(value)
        else
          raise ArgumentError.new("+value+ of a property of JSON type must be nil or a String")
        end
      end

      def dump(value)
        if value.nil? || value.is_a?(::String)
          value
        else
          MultiJson.dump(value)
        end
      end

      def typecast(value)
        return if value.nil?

        if value_loaded?(value)
          value
        else
          MultiJson.load(value.to_s)
        end
      end

      def value_loaded?(value)
        value.kind_of?(::Array) || value.kind_of?(::Hash)
      end

      include ::Ardm::Property::DirtyMinder

    end # class Json

    JSON = Json

  end # class Property
end # module Ardm

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ardm-0.4.0.ar427 lib/ardm/property/json.rb
ardm-0.4.0 lib/ardm/property/json.rb
ardm-0.3.2 lib/ardm/property/json.rb
ardm-0.3.1 lib/ardm/property/json.rb
ardm-0.3.0 lib/ardm/property/json.rb
ardm-0.2.7 lib/ardm/property/json.rb
ardm-0.2.6 lib/ardm/property/json.rb
ardm-0.2.5 lib/ardm/property/json.rb
ardm-0.2.4 lib/ardm/property/json.rb
ardm-0.2.3 lib/ardm/property/json.rb
ardm-0.2.2 lib/ardm/property/json.rb
ardm-0.2.1 lib/ardm/property/json.rb
ardm-0.2.0 lib/ardm/property/json.rb
ardm-0.1.0 lib/ardm/property/json.rb
ardm-0.0.1 lib/ardm/property/json.rb