0.0
The project is in a healthy, maintained state
A Ruby gem for YAML serialization and deserialization of ActiveRecord models with JSON schema generation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13.0

Runtime

 Project Readme

YamlExporter

YamlExporter is a Ruby gem that provides YAML serialization and deserialization functionality for ActiveRecord models, with JSON schema generation for validation.

Features

  • Serialize ActiveRecord models to YAML
  • Deserialize YAML back to ActiveRecord models
  • Generate JSON schemas for model validation
  • Support for nested associations (has_many and has_one)
  • Automatic type inference based on database column types

Installation

Add this line to your application's Gemfile:

gem 'yaml_exporter'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install yaml_exporter

Usage

Setting up your model

Include the YamlExporter module in your ActiveRecord model and define the YAML structure:

class Quiz < ApplicationRecord
  include YamlExporter

  yaml_structure do
    yaml_attribute :title, :quiz_type
    yaml_has_many :questions do
      yaml_attribute :text, :question_type, :feedback
      yaml_has_many :answers do
        yaml_attribute :text, :is_correct, :impact
      end
    end
  end
end

Serializing to YAML

To serialize a model instance to YAML:

quiz = Quiz.find(1)
yaml_string = quiz.yaml_export

Deserializing from YAML

To deserialize YAML back to a model instance:

quiz = Quiz.new
quiz.yaml_import(yaml_string)

Generating JSON Schema

To generate a JSON schema for validation:

schema = Quiz.yaml_schema

Configuration

The YamlExporter automatically infers types based on the database column types. JSON and JSONB columns are treated as objects in the generated schema.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/itadventurer/yaml_exporter.

License

The gem is available as open source under the terms of the MIT License.