Sha256: ccdf60addad9f072f6edf107d6706339076e1e020b37c525ab24032150fc247e

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

require "ginny"

module Eddy
  module Build
    module Elements

      # @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.n(el, test: false)
        decimal_points = self.determine_decimals(el.type)
        constructor = 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,
              decimals: #{decimal_points},
            )
          RB
        }).render()
        c = self.ginny_class(el, constructor)
        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

      # @param type [String]
      # @return [Integer]
      def self.determine_decimals(type)
        match = type.match(/(?<=N)(?<decimal>\d)/)
        return 0 if match.nil?
        return match[:decimal].to_i
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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