Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created August 22, 2014 13:28
Show Gist options
  • Save mashiro/990f323337724b3035eb to your computer and use it in GitHub Desktop.
Save mashiro/990f323337724b3035eb to your computer and use it in GitHub Desktop.
require 'json'
class Schema
attr_reader :items
def initialize(&block)
@items = []
instance_eval &block
end
def field(name, type, options = {})
@items << {name: name, type: type.upcase}.merge(options)
end
%w(string integer float boolean).each do |type|
define_method(type) do |name|
field name, type
end
end
def fields(name, &block)
field name, :record, fields: Schema.new(&block).items
end
def to_json
JSON.pretty_generate @items
end
end
schema = Schema.new do
integer :id
integer :created_at
boolean :favorited
fields :user do
integer :id
string :screen_name
string :name
end
end
puts schema.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment