Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pair-kit-miniflector

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pair-kit-miniflector

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

DSL Factory

This gem helps to build custom DSL. More documentation coming later.


NOTE

More documentation coming soon.


Usage

In your gemfile


gem 'pair-kit-dsl-factory'

In your code


require 'pair_kit/dsl_factory'

Example

This example show how to build simple JSON Schema DSL.

Step 1: Define your DSL parts as modules

module SchemaDsl
  def struct(&block)
    _schema[:type] = :object
    _schema[:properties] = {}
    _schema[:required] = []

    build(:struct, _schema, &block)
  end

  def array(&block)
    _schema[:type] = :array
    _schema[:item] ||= {}

    build(:array, _schema, &block)
  end

  def number
    _schema[:type] = :number
  end

  def string
    _schema[:type] = :string
  end
end

Step 2: Configure your factory

class ApplicationModel < ActiveRecord::Model
  JSON_SCHEMA_BUILDER = DslFactory.new do
    configure_builder(:schema, SchemaDsl) do
      private
      
      def _schema
        @subject
      end
    end
    
    configure_builder(:struct, SchemaDsl) do
      def prop(name, &block)
        name = name.to_sym
        @subject[:properties][name] = {}

        @dsl.(@subject, builder: :property, name: name, &block)
      end
    end
    
    configure_builder(:array, SchemaDsl) do
      def item(&block)
        @dsl.(@subject[:item], builder: :schema &block)
      end

      private

      def _schema
        @subject[:item]
      end
    end
    
    configure_builder(:property, SchemaDsl) do
      def required
        (@subject[:required] ||= []) << @options[:name]
      end

      private

      def _schema
        @subject[:properties][@options[:name]]
      end
    end
  end
  
  class_attribute :json_schema 
  
  def self.draw_schema(&block)
    self.json_schema ||= {}
    JSON_SCHEMA_BUILDER.builder(json_schema).struct(&block)
  end
end

Step 3: Use your DSL

class User < ApplicationModel
  draw_schema do 
    prop(:first_name).required.string
    prop(:last_name).required.string
    prop(:balance).required.number
    prop(:tags).required.array.string
  end
end

FAQs

Package last updated on 03 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc