Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
Module.new with a block is equivalent to module_eval
  • Loading branch information
MatheusRich committed Sep 2, 2023
1 parent 283dada commit 4644fe5
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/ez_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def self.extended(other)
end

def self.configure(getters: true)
mod = Module.new

mod.module_eval do
Module.new do
# Defines multiple keyword arguments for a class initializer
define_method :attributes do |*args, **args_with_default|
required_args = args.map { |name| "#{name}:" }
Expand All @@ -40,17 +38,15 @@ def self.configure(getters: true)
all_args = args + args_with_default.keys
attr_reader(*(all_args - EXCEPTIONS)) if getters

class_eval <<~RUBY, __FILE__, __LINE__ + 1
class_eval <<~CODE, __FILE__, __LINE__ + 1
def initialize(#{init_args})
#{all_args.map { |name| "@#{name} = binding.local_variable_get(:#{name})" }.join("; ")}
end
RUBY
CODE
end

# Defines a single keyword argument for a class initializer
alias_method :attribute, :attributes
end

mod
end
end

0 comments on commit 4644fe5

Please sign in to comment.