Sha256: f9813733fc3ed16ea5e96120922819c9aca0193e03cf2acad1c52bb5046ff76e

Contents?: true

Size: 882 Bytes

Versions: 3

Compression:

Stored size: 882 Bytes

Contents

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

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

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

            private

            module_eval("define_method :#{block_name} do
                @#{block_name} ||= instance_eval(&block) if block
              end")
          end
        end

        self.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|
        self.public_send("#{key}=", value)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
danica-2.7.4 lib/danica/expressable.rb
danica-2.7.3 lib/danica/expressable.rb
danica-2.7.2 lib/danica/expressable.rb