Sha256: aa43c7879d38ff5a5e90b67534d386d797602d39062865a1b2adf94ad54074dc

Contents?: true

Size: 1.9 KB

Versions: 6

Compression:

Stored size: 1.9 KB

Contents

module DataMapper
  module Model
    # module Json
      def to_json_schema(repository_name = default_repository_name)
        to_json_schema_compatible_hash(repository_name).to_json
      end
      
      #TODO: Add various options in.
      def to_json_schema_compatible_hash(repository_name = default_repository_name)
        usable_properties = properties.select { |p| p.field != 'id' }
        schema_hash = {}
        schema_hash['id'] = self.storage_name(repository_name)
        properties_hash = {}
        usable_properties.each do |p| 
          properties_hash[p.field] = p.to_json_schema_hash(repository_name) 
        end
        schema_hash['properties'] = properties_hash
        schema_hash['prototype'] = {}
        return schema_hash
      end
  end

  class Property
    def to_json_schema_hash(repo)
      tm = repository(repo).adapter.type_map
      json_hash = {}
      
      if type.eql?(DataMapper::Types::JsonReference)
        json_hash = { "type" => {"$ref" => "../#{reference_class.storage_name}"}, "optional" => true }
      elsif type.eql?(DataMapper::Types::JsonReferenceCollection)
        json_hash = { "type" => "array", 
                      "optional" => true,  
                      "items" => {"$ref" => "../#{reference_class.storage_name}"}
                    }
        #  "maxItems" => # For 0..3 
        #  "minItems" =>
      else
        json_hash = { "type" => tm[type][:primitive] }
        json_hash.merge!( tm[type].reject{ |key,value| key == :primitive } )
        json_hash.merge!({ "optional" => true }) unless required?
        json_hash.merge!({ "unique"  => true})   if     unique?
        json_hash.merge!({"position" => @position }) unless @position.nil?
        json_hash.merge!({"prefix" => @prefix }) unless @prefix.nil?
        json_hash.merge!({"separator" => @separator }) unless @separator.nil?
      end
      
      # MIN
      # MAX
      json_hash
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dm-persevere-adapter-0.52.1 lib/model_json_support.rb
dm-persevere-adapter-0.52.0 lib/model_json_support.rb
dm-persevere-adapter-0.51.0 lib/model_json_support.rb
dm-persevere-adapter-0.50.0 lib/model_json_support.rb
dm-persevere-adapter-0.48.2 lib/model_json_support.rb
dm-persevere-adapter-0.48.1 lib/model_json_support.rb