Ruby examples

Simple model

A simple model with some scalar properties.
{
  "definitions": {
    "Student": {
      "description": "A simple student struct",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  },
  "root": "Student"
}

student.rb

module TypeSchema

# A simple student struct
class Student
  attr_accessor :first_name, :last_name, :age

  def initialize(first_name, last_name, age)
    @first_name = first_name
    @last_name = last_name
    @age = age
  end
end
end

Model with inheritance

A student struct which extends from a different struct.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        }
      }
    },
    "Student": {
      "parent": {
        "type": "reference",
        "target": "Human"
      },
      "type": "struct",
      "properties": {
        "studentId": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

human.rb

module TypeSchema
class Human
  attr_accessor :first_name, :last_name, :age

  def initialize(first_name, last_name, age)
    @first_name = first_name
    @last_name = last_name
    @age = age
  end
end
end

student.rb

module TypeSchema
class Student
  extend Human
  attr_accessor :student_id

  def initialize(student_id)
    @student_id = student_id
  end
end
end

Model with reference

A student struct which reference a faculty struct.
{
  "definitions": {
    "Student": {
      "description": "A student struct with an assigned faculty",
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "faculty": {
          "type": "reference",
          "target": "Faculty"
        }
      }
    },
    "Faculty": {
      "type": "struct",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  },
  "root": "Student"
}

student.rb

module TypeSchema

# A student struct with an assigned faculty
class Student
  attr_accessor :first_name, :last_name, :age, :faculty

  def initialize(first_name, last_name, age, faculty)
    @first_name = first_name
    @last_name = last_name
    @age = age
    @faculty = faculty
  end
end
end

faculty.rb

module TypeSchema
class Faculty
  attr_accessor :name

  def initialize(name)
    @name = name
  end
end
end

Map with string values

A student struct which contains a map with arbitrary string values.
{
  "definitions": {
    "Student": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "type": "integer"
        },
        "properties": {
          "type": "map",
          "schema": {
            "type": "string"
          }
        }
      }
    }
  },
  "root": "Student"
}

student.rb

module TypeSchema
class Student
  attr_accessor :first_name, :last_name, :age, :properties

  def initialize(first_name, last_name, age, properties)
    @first_name = first_name
    @last_name = last_name
    @age = age
    @properties = properties
  end
end
end

Model with discriminator

A struct which uses a discriminator mapping.
{
  "definitions": {
    "Human": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "location": {
          "type": "reference",
          "target": "Location"
        }
      }
    },
    "Location": {
      "type": "struct",
      "base": true,
      "properties": {
        "type": {
          "type": "string"
        }
      },
      "discriminator": "type",
      "mapping": {
        "Web": "web",
        "World": "world"
      }
    },
    "Web": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "url": {
          "type": "string"
        }
      }
    },
    "World": {
      "parent": {
        "type": "reference",
        "target": "Location"
      },
      "type": "struct",
      "properties": {
        "lat": {
          "type": "string"
        },
        "long": {
          "type": "string"
        }
      }
    }
  },
  "root": "Human"
}

human.rb

module TypeSchema
class Human
  attr_accessor :first_name, :last_name, :location

  def initialize(first_name, last_name, location)
    @first_name = first_name
    @last_name = last_name
    @location = location
  end
end
end

location.rb

module TypeSchema
class Location
  attr_accessor :type

  def initialize(type)
    @type = type
  end
end
end

web.rb

module TypeSchema
class Web
  extend Location
  attr_accessor :url

  def initialize(url)
    @url = url
  end
end
end

world.rb

module TypeSchema
class World
  extend Location
  attr_accessor :lat, :long

  def initialize(lat, long)
    @lat = lat
    @long = long
  end
end
end

Model with generics

A struct which uses generics.
{
  "definitions": {
    "Student": {
      "type": "object",
      "properties": {
        "matricleNumber": {
          "type": "integer"
        }
      }
    },
    "StudentMap": {
      "type": "struct",
      "parent": {
        "type": "reference",
        "target": "Map",
        "template": {
          "T": "Student"
        }
      }
    },
    "Map": {
      "type": "object",
      "properties": {
        "totalResults": {
          "type": "integer"
        },
        "entries": {
          "type": "array",
          "schema": {
            "type": "generic",
            "name": "T"
          }
        }
      }
    }
  },
  "root": "StudentMap"
}

student.rb

module TypeSchema
class Student
  attr_accessor :matricle_number

  def initialize(matricle_number)
    @matricle_number = matricle_number
  end
end
end

map.rb

module TypeSchema
class Map
  attr_accessor :total_results, :entries

  def initialize(total_results, entries)
    @total_results = total_results
    @entries = entries
  end
end
end

student_map.rb

module TypeSchema
class StudentMap
  extend Map

  def initialize()
  end
end
end

Import other TypeSchema specification

A struct which references an external TypeSchema.
{
  "import": {
    "my_ns": "file:///generic.json"
  },
  "definitions": {
    "Faculty": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "students": {
          "type": "array",
          "schema": {
            "type": "reference",
            "target": "my_ns:StudentMap"
          }
        }
      }
    }
  },
  "root": "Faculty"
}

faculty.rb

module TypeSchema
class Faculty
  attr_accessor :description, :students

  def initialize(description, students)
    @description = description
    @students = students
  end
end
end

Edit this page