Sha256: 8ac03d375f1a0e1d17adba4b4efa9aea913c26200b58ce1c0e6bcb1630d92790

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require "parametric"

module Parametric
  module DSL
    # Example
    #   class Foo
    #     include Parametric::DSL
    #
    #     schema do
    #       field(:title).type(:string).present
    #       field(:age).type(:integer).default(20)
    #     end
    #
    #      attr_reader :params
    #
    #      def initialize(input)
    #        @params = self.class.schema.resolve(input)
    #      end
    #   end
    #
    #   foo = Foo.new(title: "A title", nope: "hello")
    #
    #   foo.params # => {title: "A title", age: 20}
    #
    def self.included(base)
      base.extend(ClassMethods)
      base.schema = Parametric::Schema.new
    end

    module ClassMethods
      def schema=(sc)
        @schema = sc
      end

      def inherited(subclass)
        subclass.schema = @schema.merge(Parametric::Schema.new)
      end

      def schema(options = {}, &block)
        return @schema unless options.any? || block_given?

        new_schema = Parametric::Schema.new(options, &block)
        @schema = @schema.merge(new_schema)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
parametric-0.1.3 lib/parametric/dsl.rb
parametric-0.1.2 lib/parametric/dsl.rb
parametric-0.1.1 lib/parametric/dsl.rb
parametric-0.1.0 lib/parametric/dsl.rb