Module: Ns::FieldMapping::ClassMethods

Defined in:
lib/ns_service_pack/field_mapping.rb

Instance Method Summary collapse

Instance Method Details

#__model_clazz_nameObject

Fix a bug on 20111117 by shang, this override the original method name



164
165
166
# File 'lib/ns_service_pack/field_mapping.rb', line 164

def __model_clazz_name
  self.name.underscore
end

#_find_resource(params = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# 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 = {}) ⇒ Object

将外界条件转化为一个数组



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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 = {}) ⇒ Object

将数据层表示转成业务层表示



245
246
247
248
249
250
251
252
253
254
255
256
257
# 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 = {}) ⇒ Object



190
191
192
# File 'lib/ns_service_pack/field_mapping.rb', line 190

def create_from_buz(params = {})
  create(db_hashize(params))
end

#db_hashize(params = {}) ⇒ Object

业务层到数据层字段参数转换并做一定值处理



234
235
236
237
238
239
240
241
242
# 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 = []) ⇒ Object

convert buz array to db array



76
77
78
79
80
81
82
83
84
# 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) ⇒ Object

TODO



228
229
230
231
# 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) ⇒ Object

生成field mapping,dump到指定的yaml文件中



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# 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) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# 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

#mappingObject



171
172
173
# File 'lib/ns_service_pack/field_mapping.rb', line 171

def mapping
  GlobalConst.send(model_key)
end

#model_keyObject



167
168
169
# File 'lib/ns_service_pack/field_mapping.rb', line 167

def model_key
  "#{__model_clazz_name}_mapping"
end

#new_from_buz(params = {}) ⇒ Object

辅助方法



187
188
189
# File 'lib/ns_service_pack/field_mapping.rb', line 187

def new_from_buz(params = {})
  new(db_hashize(params))
end

#paginate(params = {}) ⇒ Object

分页方法 # 加入文档说明



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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 = []) ⇒ Object



86
87
88
89
90
91
92
# 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