Class: ServiceTypeValue

Inherits:
Object
  • Object
show all
Defined in:
lib/service_type_value.rb

Overview

This model is the actual list of valid service types. Not ActiveRecord, just load from config/service_type_values.yml into memory (loaded on file load, initialization)

This is stored in lib/ intentionally, to avoid Rails dev-mode auto-reloading, because we store state in this class, and reloading was losing state we intentionally set, eg for plugins adding new service types.

ServiceTypeValues have human-displayable names, that are controlled by Rails I18n, using standard Rails I18n pluralization forms, so for instance:

For a ServiceTypeValue with name 'fulltext', key in i18n at 'umlaut.service_type_names.fulltext.one' represents the singular (non-plural) name of objects of this ServiceTypeValue, and 'umlaut.service_type_names.fulltext.other' is the plural name.

Other languages may have more complex pluralization rules, but Umlaut at present only looks up a plural form for 10 items, so 'other' is probably all that is used.

If you want to load additional custom service type values from your own custom .yml file (say in a plugin), do `ServiceTypeValue.merge_yml_file!( filepath_to_yml )`, perhaps in an initializer.

Constant Summary collapse

@@distro_conf_file =
File.join(Umlaut::Engine.root, "db", "orig_fixed_data", "service_type_values.yml")
@@local_conf_file =
File.join(Rails.root, "config", "umlaut_service_type_values.yml")

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ ServiceTypeValue


30
31
32
33
34
# File 'lib/service_type_value.rb', line 30

def initialize(hash)
  hash.each_pair do |key, value|
    self.send(key.to_s+"=", value)
  end
end

Class Attribute Details

.valuesObject

Returns the value of attribute values


28
29
30
# File 'lib/service_type_value.rb', line 28

def values
  @values
end

Instance Attribute Details

#display_nameObject


44
45
46
# File 'lib/service_type_value.rb', line 44

def display_name
  I18n.t(self.name, :scope => "umlaut.service_type_names", :default => :default, :count => 1)
end

#display_name_plural=(value) ⇒ Object (writeonly)

Sets the attribute display_name_plural


26
27
28
# File 'lib/service_type_value.rb', line 26

def display_name_plural=(value)
  @display_name_plural = value
end

#idObject

Returns the value of attribute id


25
26
27
# File 'lib/service_type_value.rb', line 25

def id
  @id
end

#nameObject

Returns the value of attribute name


25
26
27
# File 'lib/service_type_value.rb', line 25

def name
  @name
end

Class Method Details

.[](name) ⇒ Object


40
41
42
# File 'lib/service_type_value.rb', line 40

def self.[](name)
  find(name)
end

.find(name) ⇒ Object


36
37
38
# File 'lib/service_type_value.rb', line 36

def self.find(name)    
  values[name.to_sym] or raise ArgumentError.new("No ServiceTypeValue found for #{name}")
end

.load_values!Object

Loads from config files, distro and local, into memory.


62
63
64
65
66
67
68
69
70
71
# File 'lib/service_type_value.rb', line 62

def self.load_values!
  # Load in starting set of ServiceTypeValue, merge in local defines.       
  self.merge_yml_file! @@distro_conf_file 
  

  if File.exists?( @@local_conf_file )
    self.merge_yml_file! @@local_conf_file
  end

end

.merge_hash!(definition_hash) ⇒ Object


73
74
75
76
77
78
79
# File 'lib/service_type_value.rb', line 73

def self.merge_hash!(definition_hash)
  self.values ||= {}

  definition_hash.each_pair do |name, value_hash|
    self.values[name.to_sym] = ServiceTypeValue.new(value_hash.merge(:name =>  name))
  end
end

.merge_yml_file!(filepath) ⇒ Object


81
82
83
# File 'lib/service_type_value.rb', line 81

def self.merge_yml_file!(filepath)
  self.merge_hash! YAML.load_file(filepath)
end

Instance Method Details

#display_name_pluralizeObject

Some languages may have multiple plural forms, but Umlaut at present is not sophisticated enough to use em all, we just look up the plural form for 10 items – most Umlaut use of plural form is for either zero or indetermininate number. But it still may need enhancing.


53
54
55
# File 'lib/service_type_value.rb', line 53

def display_name_pluralize
  I18n.t(self.name, :scope => "umlaut.service_type_names", :default => :default, :count => 10)
end