Created
August 22, 2014 13:28
-
-
Save mashiro/990f323337724b3035eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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