lib/danica/expressable.rb in danica-2.7.4 vs lib/danica/expressable.rb in danica-2.7.5

- old
+ new

@@ -1,26 +1,32 @@ +# frozen_string_literal: true + module Danica - module Expressable extend ::ActiveSupport::Concern + module Expressable + extend ::ActiveSupport::Concern include VariablesHolder class_methods do def built_with(block_name) - self.delegate :to, :to_f, to: block_name + delegate :to, :to_f, to: block_name - self.define_singleton_method(:build) do |*vars, &block| + 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") + 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 - self.define_singleton_method(:create) do |*vars, &block| + define_singleton_method(:create) do |*vars, &block| build(*vars, &block).new end end end @@ -28,10 +34,10 @@ options = args.extract_options! attributes = { variables: args.flatten }.merge(options) attributes.each do |key, value| - self.public_send("#{key}=", value) + public_send("#{key}=", value) end end end end