From: Ken Barber Date: Thu, 28 Feb 2013 21:15:02 +0000 (+0000) Subject: (GH-128) Change method_missing to define_method X-Git-Tag: 0.1.1~3^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=9131fad7cd845ea26911e20ce51e4eb4f9154f33;p=puppet-modules%2Fpuppetlabs-firewall.git (GH-128) Change method_missing to define_method Previously method_missing was enough to create dynamic methods but Puppet 3.0 broke that functionality. So here we used 'define_method' instead to work around that. Signed-off-by: Ken Barber --- diff --git a/lib/puppet/provider/firewall.rb b/lib/puppet/provider/firewall.rb index 0ab16f7..c6b0b10 100644 --- a/lib/puppet/provider/firewall.rb +++ b/lib/puppet/provider/firewall.rb @@ -31,30 +31,4 @@ class Puppet::Provider::Firewall < Puppet::Provider end nil end - - # Executed if method is missing. In this case we are going to catch - # unqualified property methods for dynamic property setting and getting. - def method_missing(meth, *args, &block) - dynamic_methods = self.class.instance_variable_get('@resource_map').keys - dynamic_methods << :chain - dynamic_methods << :table - dynamic_methods << :action - - if dynamic_methods.include?(meth.to_sym) then - if @property_hash[meth.to_sym] then - return @property_hash[meth.to_sym] - else - return nil - end - elsif dynamic_methods.include?(meth.to_s.chomp("=").to_sym) then - debug("Args: #{args}") - @property_hash[:needs_change] = true - return true - end - - debug("Dynamic methods: #{dynamic_methods.join(' ')}") - debug("Method missing: #{meth}. Calling super.") - - super - end end diff --git a/lib/puppet/provider/firewall/ip6tables.rb b/lib/puppet/provider/firewall/ip6tables.rb index 5759920..d681caa 100644 --- a/lib/puppet/provider/firewall/ip6tables.rb +++ b/lib/puppet/provider/firewall/ip6tables.rb @@ -56,6 +56,17 @@ Puppet::Type.type(:firewall).provide :ip6tables, :parent => :iptables, :source = :pkttype => "-m pkttype --pkt-type" } + # Create property methods dynamically + (@resource_map.keys << :chain << :table << :action).each do |property| + define_method "#{property}" do + @property_hash[property.to_sym] + end + + define_method "#{property}=" do + @property_hash[:needs_change] = true + end + end + # This is the order of resources as they appear in iptables-save output, # we need it to properly parse and apply rules, if the order of resource # changes between puppet runs, the changed rules will be re-applied again. diff --git a/lib/puppet/provider/firewall/iptables.rb b/lib/puppet/provider/firewall/iptables.rb index b34e736..5044414 100644 --- a/lib/puppet/provider/firewall/iptables.rb +++ b/lib/puppet/provider/firewall/iptables.rb @@ -66,6 +66,17 @@ Puppet::Type.type(:firewall).provide :iptables, :parent => Puppet::Provider::Fir :pkttype => "-m pkttype --pkt-type" } + # Create property methods dynamically + (@resource_map.keys << :chain << :table << :action).each do |property| + define_method "#{property}" do + @property_hash[property.to_sym] + end + + define_method "#{property}=" do + @property_hash[:needs_change] = true + end + end + # This is the order of resources as they appear in iptables-save output, # we need it to properly parse and apply rules, if the order of resource # changes between puppet runs, the changed rules will be re-applied again.