summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXabier de Zuazo <xabier@onddo.com>2014-11-27 22:44:10 +0100
committerLamont Granquist <lamont@scriptkiddie.org>2015-01-27 11:59:40 -0800
commit8b4b26b6858e681a6d9d1c65abd7f97f68ee42c1 (patch)
tree456517466a8e2d5a63ac812cada43b5275b3f30e
parent9e6a0cdda93de380093c4abdfeb563d6f946223b (diff)
downloadchef-8b4b26b6858e681a6d9d1c65abd7f97f68ee42c1.tar.gz
Use #define_method instead of #class_eval (ruby 1.8 specific, issue #2497)
-rw-r--r--lib/chef/event_dispatch/dispatcher.rb8
-rw-r--r--lib/chef/node/attribute.rb8
-rw-r--r--lib/chef/node/attribute_collections.rb20
-rw-r--r--lib/chef/node/immutable_collections.rb14
-rw-r--r--lib/chef/resource/lwrp_base.rb14
5 files changed, 21 insertions, 43 deletions
diff --git a/lib/chef/event_dispatch/dispatcher.rb b/lib/chef/event_dispatch/dispatcher.rb
index c172a406d8..9f43f14311 100644
--- a/lib/chef/event_dispatch/dispatcher.rb
+++ b/lib/chef/event_dispatch/dispatcher.rb
@@ -25,11 +25,9 @@ class Chef
# Define a method that will be forwarded to all
def self.def_forwarding_method(method_name)
- class_eval(<<-END_OF_METHOD, __FILE__, __LINE__)
- def #{method_name}(*args)
- @subscribers.each {|s| s.#{method_name}(*args)}
- end
- END_OF_METHOD
+ define_method(method_name) do |*args|
+ @subscribers.each { |s| s.send(method_name, *args) }
+ end
end
(Base.instance_methods - Object.instance_methods).each do |method_name|
diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb
index 80f5ac4f8d..9d8738f637 100644
--- a/lib/chef/node/attribute.rb
+++ b/lib/chef/node/attribute.rb
@@ -138,11 +138,9 @@ class Chef
:values,
:values_at,
:zip].each do |delegated_method|
- class_eval(<<-METHOD_DEFN)
- def #{delegated_method}(*args, &block)
- merged_attributes.send(:#{delegated_method}, *args, &block)
- end
- METHOD_DEFN
+ define_method(delegated_method) do |*args, &block|
+ merged_attributes.send(delegated_method, *args, &block)
+ end
end
# return the cookbook level default attribute component
diff --git a/lib/chef/node/attribute_collections.rb b/lib/chef/node/attribute_collections.rb
index 333f4864c6..b912904534 100644
--- a/lib/chef/node/attribute_collections.rb
+++ b/lib/chef/node/attribute_collections.rb
@@ -61,12 +61,10 @@ class Chef
# also invalidate the cached merged_attributes on the root
# Node::Attribute object.
MUTATOR_METHODS.each do |mutator|
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{mutator}(*args, &block)
- root.reset_cache(root.top_level_breadcrumb)
- super
- end
- METHOD_DEFN
+ define_method(mutator) do |*args, &block|
+ root.reset_cache(root.top_level_breadcrumb)
+ super(*args, &block)
+ end
end
attr_reader :root
@@ -126,12 +124,10 @@ class Chef
# also invalidate the cached `merged_attributes` on the root Attribute
# object.
MUTATOR_METHODS.each do |mutator|
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{mutator}(*args, &block)
- root.reset_cache(root.top_level_breadcrumb)
- super
- end
- METHOD_DEFN
+ define_method(mutator) do |*args, &block|
+ root.reset_cache(root.top_level_breadcrumb)
+ super(*args, &block)
+ end
end
def initialize(root, data={})
diff --git a/lib/chef/node/immutable_collections.rb b/lib/chef/node/immutable_collections.rb
index 56b8fed3b7..0e2800641a 100644
--- a/lib/chef/node/immutable_collections.rb
+++ b/lib/chef/node/immutable_collections.rb
@@ -75,12 +75,9 @@ class Chef
# Redefine all of the methods that mutate a Hash to raise an error when called.
# This is the magic that makes this object "Immutable"
DISALLOWED_MUTATOR_METHODS.each do |mutator_method_name|
- # Ruby 1.8 blocks can't have block arguments, so we must use string eval:
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{mutator_method_name}(*args, &block)
- raise Exceptions::ImmutableAttributeModification
- end
- METHOD_DEFN
+ define_method(mutator_method_name) do |*args, &block|
+ raise Exceptions::ImmutableAttributeModification
+ end
end
# For elements like Fixnums, true, nil...
@@ -164,12 +161,9 @@ class Chef
# Redefine all of the methods that mutate a Hash to raise an error when called.
# This is the magic that makes this object "Immutable"
DISALLOWED_MUTATOR_METHODS.each do |mutator_method_name|
- # Ruby 1.8 blocks can't have block arguments, so we must use string eval:
- class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
- def #{mutator_method_name}(*args, &block)
+ define_method(mutator_method_name) do |*args, &block|
raise Exceptions::ImmutableAttributeModification
end
- METHOD_DEFN
end
def method_missing(symbol, *args)
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index 20d177f507..0f2bb1dc21 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -73,17 +73,9 @@ class Chef
# Define an attribute on this resource, including optional validation
# parameters.
def self.attribute(attr_name, validation_opts={})
- # Ruby 1.8 doesn't support default arguments to blocks, but we have to
- # use define_method with a block to capture +validation_opts+.
- # Workaround this by defining two methods :(
- class_eval(<<-SHIM, __FILE__, __LINE__)
- def #{attr_name}(arg=nil)
- _set_or_return_#{attr_name}(arg)
- end
- SHIM
-
- define_method("_set_or_return_#{attr_name.to_s}".to_sym) do |arg|
- set_or_return(attr_name.to_sym, arg, validation_opts)
+ define_method(attr_name) do |*args|
+ raise ArgumentError.new("wrong number of arguments (#{args.length} for 1)") if args.length > 1
+ set_or_return(attr_name.to_sym, args.first, validation_opts)
end
end