Sha256: e97ca6d8d2f77dc58ad785ed079b8645795ffe7373290c21e649da1f6657876f

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module OpenConstructor
  def set values, list = nil
    unless list
      if values.is_a? Hash
        values.each do |k, v|          
          self.respond_to "#{k}=", v
        end
      else
        values.instance_variables.each do |name|
          accessor = "#{name[1..name.size]}="
          if self.respond_to? accessor
            self.send accessor, values.instance_variable_get(name)
          end
        end
      end
    else
      if values.is_a? Hash
        values.each do |k, v|          
          self.respond_to "#{k}=", v if list.include? k
        end
      else
        values.instance_variables.each do |name|
          accessor = name[1..name.size]
          if list.include?(accessor.to_sym) 
            accessor = "#{accessor}="
            if self.respond_to?(accessor)
              self.send accessor, values.instance_variable_get(name)
            end
          end
        end
      end            
    end
    return self    
  end 
  
  def set! values
    values.each do |k, v|
      self.send "#{k}=", v      
    end
    return self
  end
  # alias_method :set! #, :set_with_check
  
  def to_hash
    hash = {}
    instance_variables.each do |name|
      hash[name[1..name.size].to_sym] = instance_variable_get name
    end
    return hash
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_ext-0.4.25 lib/ruby_ext/more/open_constructor.rb
ruby_ext-0.4.24 lib/ruby_ext/more/open_constructor.rb