class CkuruTools::HashInitializerClass
extending HashInitalizerClass enables you to get Object.new(hash) functionality where each key will dispatch to an object setter method.
Usage:
class MyClass < HashInitalizerClass ..attr_accessor :foo end
m = MyClass(:foo => “bar”) m.foo
> “bar”¶ ↑
Public Class Methods
new(h={}) { |self| ... }
click to toggle source
# File lib/ckuru-tools.rb, line 260 def initialize(h={}) raise ArgumentError.new("argument to #{self.class}##{current_method} must be of class Hash") unless h.is_a? Hash h.keys.each do |k| self.send("#{k}=",h[k]) end yield self if block_given? end
Public Instance Methods
only_these_parameters(h,*args)
click to toggle source
insure that only the defined parameters have been passed to a function
# File lib/ckuru-tools.rb, line 255 def only_these_parameters(h,*args) CkuruTools.only_these_parameters(h,args) end
parameters(h,*args)
click to toggle source
parse and return parameters from a hash
view = only_these_parameters(h,[:view,{:klass => View,:required => true}])
or
view = parameters(h,[:view,{:klass => View,:required => true}])
# File lib/ckuru-tools.rb, line 250 def parameters(h,*args) CkuruTools.parameters(h,args) end
required_attributes(*symbols)
click to toggle source
check for required attributes
# File lib/ckuru-tools.rb, line 237 def required_attributes(*symbols) symbols.each do |symbol| raise ArgumentError.new("attribute :#{symbol} must be set in #{self.class}.initialize") if self.send(symbol).nil? end end