module Ns::FieldMapping::ClassMethods
Public Instance Methods
__model_clazz_name()
click to toggle source
Fix a bug on 20111117 by shang, this override the original method name
# File lib/ns_service_pack/field_mapping.rb, line 164 def __model_clazz_name self.name.underscore end
_find_resource(params = {})
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 64 def _find_resource(params = {}) obj = find(params[:id]) if mapping.keys.include?(:status) && !erp_admin?(params) buz_value = get_map_value(:status, obj.send(mapping[:status])) if buz_value == '作废' raise "资源#{name}##{obj.object_id}已经作废不可用!" end end obj end
arrayify_conds(params = {})
click to toggle source
将外界条件转化为一个数组
# File lib/ns_service_pack/field_mapping.rb, line 116 def arrayify_conds(params = {}) #清除空条件 params.delete_if{|k, v| v.blank? } field_hash = {} mapping_hash = {} params.each do |k, v| the_key = k.to_sym if mapping.keys.include?(the_key) field_hash[the_key] = get_map_value(the_key, v) mapping_hash[the_key] = mapping[the_key] end end result_array = [] cond_str = "" if params[:extra_where_sql] cond_str << params[:extra_where_sql].to_s end if mapping_hash.present? cond_str << " and " unless cond_str.blank? cond_str << mapping_hash.map do |buz_key, db_key| "#{db_key} = :#{buz_key}" end.join(" and ") end #如有status字段 if mapping.keys.include?(:status) Rails.logger.debug "=======include status" #如非erp admin后台请求 unless erp_admin?(params) cond_str << " and " unless cond_str.blank? cond_str << " #{mapping[:status]} != -1" #TODO FIXME -1 use end else Rails.logger.debug "=======not include status" end result_array << cond_str result_array << field_hash result_array end
buz_hashize(attrs = {})
click to toggle source
将数据层表示转成业务层表示
# File lib/ns_service_pack/field_mapping.rb, line 245 def buz_hashize(attrs = {}) #return {} if attrs.blank? #获取默认初始值 attrs = new.attributes if attrs.blank? new_attrs = attrs.symbolize_keys invert_maps = mapping.invert_data common_db_attrs = new_attrs.keys & invert_maps.keys common_db_attrs.inject({}) do |result, attr| buz_field = invert_maps[attr] result[buz_field] = get_map_value(buz_field, new_attrs[attr]) result end end
create_from_buz(params = {})
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 190 def create_from_buz(params = {}) create(db_hashize(params)) end
db_hashize(params = {})
click to toggle source
业务层到数据层字段参数转换并做一定值处理
# File lib/ns_service_pack/field_mapping.rb, line 234 def db_hashize(params = {}) return {} if params.blank? new_params = params.symbolize_keys common_keys = new_params.keys & mapping.keys common_keys.inject({}) do |result, key| result[mapping[key]] = get_map_value(key, new_params[key]) result end end
dbize_array(buz_array = [])
click to toggle source
convert buz array to db array
# File lib/ns_service_pack/field_mapping.rb, line 76 def dbize_array(buz_array = []) result = nil if buz_array && buz_array.is_a?(Array) result = buz_array.map do |e| db_key = mapping[e.to_sym] rescue nil end.compact end result end
dump_field_map(force=true)
click to toggle source
TODO
# File lib/ns_service_pack/field_mapping.rb, line 228 def dump_field_map(force=true) puts "==Warning: please use dump_mapping instead, this will be removed..." dump_mapping(nil, true) end
dump_mapping(attr_prefix = nil, force = false, with_timestamp = false)
click to toggle source
生成field mapping,dump到指定的yaml文件中
# File lib/ns_service_pack/field_mapping.rb, line 195 def dump_mapping(attr_prefix = nil, force = false, with_timestamp = false) file = nil if defined?(Rails) tstamp = with_timestamp ? ".#{Time.now.to_i}" : "" file = "#{Rails.root}/#{GlobalConst::APP_CODE_HASHES}mappings/#{model_key}.yml#{tstamp}" path = File.dirname(file) FileUtils.mkpath(path) unless File.directory?(path) existed = File.exists?(file) if existed puts "Warning: #{file} has existed! check it!" end if !existed || force #获取数据库字段列表,屏蔽掉自动维护的created_at/updated_at,也可在生成的文件中配上 #20111203 讨论后打开此屏蔽 #.delete_if{|k| k =~ /^created_at|updated_at$/} keys = new.attributes.symbolize_keys.keys h = keys.inject({}) do |r, k| #移除掉表前缀 rm_prefix = attr_prefix.nil? ? "#{__model_clazz_name}_" : attr_prefix new_key = k.to_s.sub(rm_prefix, '').to_sym r[new_key] = k r end FileUtils.rm_f(file) if existed File.open(file, 'w+') do |f| f.puts "#==本文件由Nsp生成#于{Time.now.to_s(:db)},可根据规则编辑" f.puts YAML.dump(model_key.to_sym=>h) end end file end end
erp_admin?(params = {})
click to toggle source
@params invoke_src: erp_admin
# File lib/ns_service_pack/field_mapping.rb, line 159 def erp_admin?(params = {}) params[:invoke_src] == 'erp_admin' end
get_map_value(k, value)
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 175 def get_map_value(k, value) #raise "Not implemented!" #提供默认处理 #TODO 根据数据类型做些更好的转换 case k when :created_at, :updated_at value.try(:to_s, :db) else value end end
mapping()
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 171 def mapping GlobalConst.send(model_key) end
model_key()
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 167 def model_key "#{__model_clazz_name}_mapping" end
new_from_buz(params = {})
click to toggle source
辅助方法
# File lib/ns_service_pack/field_mapping.rb, line 187 def new_from_buz(params = {}) new(db_hashize(params)) end
paginate(params = {}) { |offset, page_size| ... }
click to toggle source
分页方法 # 加入文档说明
# File lib/ns_service_pack/field_mapping.rb, line 95 def paginate(params = {}) current_page = params[:current_page].to_i current_page = current_page < 1 ? 1 : current_page page_size = params[:page_size].to_i page_size = page_size <= 0 ? 10 : page_size #increment the limit page_size = page_size > 5000 ? 5000 : page_size offset = (current_page-1)*page_size #返回总数和当前页数据 起始位置, 数量 total_count, page_data = yield(offset, page_size) { :total_size=>total_count, :page_size=>page_size, :current_page=>current_page, :page_from=>offset + 1, :page_to=>offset + page_data.size, :page_items=>page_data } end
select_fields(array = [])
click to toggle source
# File lib/ns_service_pack/field_mapping.rb, line 86 def select_fields(array = []) db_keys = dbize_array(array) || mapping.values #FIXME 添加是否是数据属性的判断 better attrs = new.attributes.keys.map(&:to_sym) db_keys & attrs rescue nil end