Sha256: 3430b9e25e406c0f2a1b4e9c000598180ea80bcad043f8a64371e00ec69ec385

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 KB

Contents

ActiveRecord::Base.class_eval do

  #takes array of method names and returns hash with key/value pairs of methods
  def to_hash(options={})
    {}.tap do |hash|
      #check for only option to get only attributes specified
      if options[:only]
        options[:only].each do |attribute|
          if attribute.is_a?(Hash)
            attribute.each do |k,v|
              k = k.to_sym
              v = v.to_sym
              hash[v] = self.send(k)
            end
          else
            attribute = attribute.to_sym
            hash[attribute] = self.send(attribute)
          end
        end
      else
        hash.merge!(self.attributes)
      end

      #check for methods option
      if options[:methods]
        options[:methods].each do |method|
          if method.is_a?(Hash)
            method.each do |k,v|
              k = k.to_sym
              v = v.to_sym
              hash[v] = self.send(k)
            end
          else
            method = method.to_sym
            hash[method] = self.send(method)
          end
        end
      end

      #check for additional values
      options[:additional_values].each{|k, v| hash[k] = v} if options[:additional_values]
      
    end#end hash tap
  end
end


Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
erp_base_erp_svcs-3.1.5 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.1.4 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.1.3 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.1.2 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.1.1 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.7 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.1.0 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.6 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.5 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.4 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.3 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb
erp_base_erp_svcs-3.0.2 lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb