class Object
Public Class Methods
symbolize_keys(item)
click to toggle source
take keys of hash and transform those to a symbols
# File lib/erp_base_erp_svcs/extensions/core/hash.rb, line 3 def self.symbolize_keys(item) if item.class == Hash item.to_options! item.each do |key, value| self.symbolize_keys(value) end elsif item.class == Array item.each do |array_val| self.symbolize_keys(array_val) end end item end
Public Instance Methods
all_subclasses()
click to toggle source
# File lib/erp_base_erp_svcs/extensions/core/object.rb, line 2 def all_subclasses klasses = self.subclasses (klasses | klasses.collect do |klass| klass.all_subclasses end).flatten.uniq end
apply_if(hash)
click to toggle source
merge hash and overwrite key only if it is nil
# File lib/erp_base_erp_svcs/extensions/core/hash.rb, line 19 def apply_if(hash) self.merge(hash){|key, v1, v2| v1.nil? ? v2 : v1} end
apply_if!(hash)
click to toggle source
# File lib/erp_base_erp_svcs/extensions/core/hash.rb, line 23 def apply_if!(hash) self.merge!(hash){|key, v1, v2| v1.nil? ? v2 : v1} end
class_exists?(class_name)
click to toggle source
Check if a class exists
@param class_name [String] name of class to check
# File lib/erp_base_erp_svcs/extensions/core/object.rb, line 10 def class_exists?(class_name) class_name.to_s.constantize # try to constantize twice which will cause false positives for Rails Models to fail class_name.to_s.constantize true rescue NameError false end
copy(destination, sources, options = {})
click to toggle source
# File lib/erp_base_erp_svcs/extensions/active_record/migration.rb, line 2 def copy(destination, sources, options = {}) copied = [] FileUtils.mkdir_p(destination) unless File.exists?(destination) destination_migrations = ActiveRecord::Migrator.migrations(destination) last = destination_migrations.last sources.each do |scope, path| source_migrations = ActiveRecord::Migrator.migrations(path) source_migrations.each do |migration| source = File.read(migration.filename) source = "# This migration comes from #{scope} (originally #{migration.version})\n#{source}" if duplicate = destination_migrations.detect { |m| m.name == migration.name } if options[:refresh] && duplicate.scope == scope.to_s Dir.glob(File.join(destination,"*_#{migration.name.underscore}.#{scope.to_s}.rb")).each { |f| puts "Removing old migration #{migration.name}"; File.delete(f) } elsif options[:on_skip] && duplicate.scope != scope.to_s options[:on_skip].call(scope, migration) end next unless options[:refresh] end migration.version = next_migration_number(last ? last.version + 1 : 0).to_i unless options[:preserve_timestamp] new_path = File.join(destination, "#{migration.version}_#{migration.name.underscore}.#{scope}.rb") old_path, migration.filename = migration.filename, new_path last = migration File.open(migration.filename, "w") { |f| f.write source } copied << migration options[:on_copy].call(scope, migration, old_path) if options[:on_copy] destination_migrations << migration end end copied end
to_hash(options={})
click to toggle source
takes array of method names and returns hash with key/value pairs of methods
# File lib/erp_base_erp_svcs/extensions/active_record/to_hash.rb, line 4 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 if options[:methods] options[:methods].each do |method| if method.is_a?(Hash) name, method_name = method.first hash[name] = self.send(method_name) else hash[method] = self.send(method) end hash[method] = self.send(method) end end options.each do |key, value| next if [:only].include?(key) next if [:methods].include?(key) hash[key] = value end end #end hash tap end
to_sym()
click to toggle source
convert all elements of array to symbols throw exception if all elements are not of class String
# File lib/erp_base_erp_svcs/extensions/core/array.rb, line 5 def to_sym new_ary = [] old_ary = to_ary old_ary.each do |item| if item.class == String new_ary << item.to_sym else throw "All array elements must be of class String to use to_sym" end end new_ary end