Sha256: a9b14d0eaf3cf0b394668435c750fe33bb9958d1c4ca1e306e1cdb28e61002aa

Contents?: true

Size: 1006 Bytes

Versions: 3

Compression:

Stored size: 1006 Bytes

Contents

# frozen_string_literal: true

module Danica
  module Expressable
    extend ::ActiveSupport::Concern
    include VariablesHolder

    class_methods do
      def built_with(block_name)
        delegate :to, :to_f, to: block_name

        define_singleton_method(:build) do |*vars, &block|
          Class.new(self) do
            variables(*vars)

            private

            module_eval do
              define_method block_name do
                instance_variable_get("@#{block_name}") ||
                  block && instance_variable_set("@#{block_name}", instance_eval(&block))
              end
            end
          end
        end

        define_singleton_method(:create) do |*vars, &block|
          build(*vars, &block).new
        end
      end
    end

    def initialize(*args)
      options = args.extract_options!

      attributes = { variables: args.flatten }.merge(options)

      attributes.each do |key, value|
        public_send("#{key}=", value)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.7.7 lib/danica/expressable.rb
danica-2.7.6 lib/danica/expressable.rb
danica-2.7.5 lib/danica/expressable.rb