Sha256: 6546901e09edac6114375fb454a5db2ef9600e69842de624a4160b1fd019258a

Contents?: true

Size: 950 Bytes

Versions: 4

Compression:

Stored size: 950 Bytes

Contents

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

    included do
      class << self
        def built_with(block_name)
          self.send(:delegate, :to_f, to: block_name)

          self.send(: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.send(:define_singleton_method, :create) do |*vars, &block|
            build(*vars, &block).new
          end
        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

4 entries across 4 versions & 1 rubygems

Version Path
danica-2.6.4 lib/danica/expressable.rb
danica-2.6.3 lib/danica/expressable.rb
danica-2.6.2 lib/danica/expressable.rb
danica-2.6.1 lib/danica/expressable.rb