Coder Social home page Coder Social logo

dialekt's Introduction

What is it?

With Dialekt you can easily define properties for DSL objects that support nice accessor methods, type checking and transformation.

Dialekt is based on Docile, which is a great tool to create DSLs in Ruby. However, you will soon find yourself creating lots of repetetive code to implement your DSL accessors. Dialekt aims to simplify this task.

Example

Let's assume you want to create a build tool called Backscratcher (sort of a small rake to scratch an itch :-) that uses a DSL for configuration. Your tool supports tasks and dependencies between tasks, and tasks can be grouped into namespaces. You start by creating a model and using Dialekt to define the properties.

    require "dialekt"
    require "forwardable"

    module Backscratcher
      extend Forwardable

      class Task
        attr_reader :name

        # Create a set property containing strings
        dsl_set :dependencies, value_type: String

        def initialize(name:)
          @name = name
        end
      end

      class FileTask < Task
      end

      class Namespace
        attr_reader :name

        # Create a tasks hash with string keys
        dsl_map :tasks, key_type: String, value_type: Task do
          # Create an accessor for task entries
          entry :task, value_factory: ->(key:) { Task.new(name: key) }
          # Create an accessor for file task entries
          entry :file, value_type: FileTask, value_factory: ->(key:) { FileTask.new(name: key) }
        end

        # Create a namespace hash with string keys
        dsl_map :namespaces, key_type: String, value_type: Namespace do
          # Create an accessor for namespace entries
          entry :namespace, value_factory: ->(key:) { Namespace.new(name: key) }
        end

        def initialize(name:)
          @name = name
          @namespaces = {}
          @tasks = {}
        end
      end

      # Make some methods available in the root namespace for convenience
      def_delegators :root, :namespace, :task, :file

      def root
        @root ||= Namespace.new(name: "")
      end
    end

    include Backscratcher

You now have a DSL for your build tool giving you methods to create namespaces, tasks and dependencies. Dialekt will handle the definition of accessores, type checking, creating new collection entries and applying DSL configurations:

    task "build" do
      dependency "db:create"
      dependency "db:load"
    end

    file "test.txt"

    namespace "db" do
      task "create"

      task "load" do
        dependencies ["db:create"]
      end
    end

dialekt's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.