Skip to content

Instantly share code, notes, and snippets.

@stevecrozz
Created February 1, 2019 21:58
Show Gist options
  • Select an option

  • Save stevecrozz/836873c0c06c7b6a3df7a0f5210507a2 to your computer and use it in GitHub Desktop.

Select an option

Save stevecrozz/836873c0c06c7b6a3df7a0f5210507a2 to your computer and use it in GitHub Desktop.
autogenerate mobx-state-tree models from ruby mongoid models
def doit
type_map = {
BSON::ObjectId => 'string',
Time => 'date',
Object => 'reference',
Hash => 'map',
Integer => 'integer',
DateTime => 'Date',
String => 'string',
Array => 'array',
Time => 'Date',
Mongoid::Boolean => 'boolean',
Float => 'string',
}
Mongoid::Config.models.each_with_object({}) do |model, obj|
name = model.model_name.name
cat = "export const #{name} = types\n"
cat << " .model({\n"
model.fields.each do |k, v|
st_type = type_map[v.type]
case v
when Mongoid::Fields::ForeignKey
cat << " #{v.metadata.name}: types.reference(#{v.metadata.class_name}),\n"
when Mongoid::Fields::Standard
cat << " #{v.name}: types.#{st_type},\n"
else
cat << " #{v.name}: types.??,\n"
end
end
cat << " })\n"
obj[name] = cat
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment