开发者

Simple model.. worth making an in memory class or just put it in the Database?

Sorry for the bad title, I've no idea what to call this question.

I have a simple class which basically just wraps an array and provides some custom access metods for it. Essentially all it stores is an array of doublets, each one holding a property type and it's uid.

My idea is that开发者_运维知识库 because this is such a simple usage, I can get away with just building this class and holding it in memory rather than generating a model and suffering the overhead of ActiveRecord (that may not be much, I'm not sure).

I'm worried I'm overthinking and I should just put it in the DB and forget about it?

class PropertyType
  extend Enumerable

  TYPES = [
      ['Site', 'Site'],
      ['New Home', 'NewHome'],
      ['Terraced House', 'Terraced'],
      ['Detached House', 'Detached'],
      ['Bungalow', 'Bungalow'],
      ['Townhouse', 'Townhouse'],
      ['End of Terrace House', 'EoTHouse'],
      ['Semi-Detached House', 'Semi-D'],
      ['New Development', 'NewDev'],
      ['Apartment', 'Apartment'],
      ['Duplex', 'Duplex'],
      ['House For Sale', 'House']
  ].freeze

  def self.each
    TYPES.each{|type| yield(type[0], type[1]) }
  end

  def self.convert_to_name(uid_to_lookup)
    return if uid_to_lookup.blank?
    TYPES.each{|type| return type[0] if type[1] == uid_to_lookup }
  end

  def self.convert_to_uid(name_to_lookup)
    return if name_to_lookup.blank?
    TYPES.each{|type| return type[1] if type[0] == name_to_lookup }
  end

  def self.each_uid
    TYPES.each{|type| yield(type[1]) }
  end

  def self.each_name
    TYPES.each{|type| yield(type[0]) }
  end

  def self.uids
    TYPES.collect{|type| type[1]}
  end

  def self.names
    TYPES.collect{|type| type[0]}
  end
end


I think your class is fine, but personally I'd still put it in the database, just so I have an easy option for building an administrative interface for property types and letting someone else manage modifications to them over time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜