Sha256: a54e4adc5e927a3cf3f82743564c10b3feb3b0d75aab76d4eed77e6e7237f9e1

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

require "ginny"

module Eddy
  module Build
    module Elements

      # @param el [Eddy::Schema::ElementSummary]
      # @return [Array<Hash>]
      def self.element_params(el)
        return [
          { name: "val", type: el.yard_type, optional: true, keyword: true },
          { name: "req", type: "String",     optional: true, keyword: true },
          { name: "ref", type: "String",     optional: true, keyword: true },
        ]
      end

      # @param el [Eddy::Schema::ElementSummary]
      # @return [String]
      def self.default_constructor(el)
        return Ginny::Func.create({
          name: "initialize",
          params: self.element_params(el),
          body: <<~RB,
            @id = "#{el.id}"
            @name = "#{el.name}"
            @description = "#{el.description}"
            super(
              min: #{el.min},
              max: #{el.max},
              req: req,
              ref: ref,
              val: val,
            )
          RB
        }).render()
      end

      # @param el [Eddy::Schema::ElementSummary]
      # @param body [String]
      # @return [Ginny::Class]
      def self.ginny_class(el, body)
        return Ginny::Class.create({
          classify_name: false,
          name:          Eddy::Util.normalize_id(el.id),
          description:   el.doc_comment,
          parent:        "Eddy::Element::#{el.edi_type}",
          modules:       ["Eddy", "Elements"],
          body:          body,
        })
      end

      # @param el [Eddy::Schema::ElementSummary]
      # @param test [Boolean] (false) When true, returns output as a string instead of writing to a file.
      # @return [void]
      def self.element(el, test: false)
        c = self.ginny_class(el, self.default_constructor(el))
        return c.render if test
        c.generate(
          File.join(Eddy::Util.root_dir, "build", "elements"),
          file: "#{el.id}.#{Eddy::Util.snake_case(el.name)}.rb",
        )
        return nil
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eddy-0.5.1 lib/eddy/build/elements/element.rb
eddy-0.5.0 lib/eddy/build/elements/element.rb
eddy-0.4.0 lib/eddy/build/elements/element.rb
eddy-0.3.0 lib/eddy/build/elements/element.rb