Module: Kwatable::LabelHelper
- Defined in:
- lib/kwatable/template/helper/label.rb
Instance Method Summary collapse
- #_w(key, error_when_not_found = false, *args) ⇒ Object
-
#enum_value_and_label(enum_item, column_name) ⇒ Object
ex.
- #load_dictionary_files(option_path_list = nil) ⇒ Object
-
#to_label(key) ⇒ Object
‘foo’ => ‘Foo’, ‘foo_bar_baz’ => ‘Foo bar baz’.
-
#w(key, *args) ⇒ Object
word.
- #w!(key, *args) ⇒ Object
Instance Method Details
#_w(key, error_when_not_found = false, *args) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kwatable/template/helper/label.rb', line 49 def _w(key, error_when_not_found=false, *args) @_dict ||= load_dictionary_files() word = @_dict[key.to_s] unless word if error_when_not_found err = StandardError.new("word key '#{key.inspect}' is not found. ") err.set_backtrace(caller()) raise err else return nil end end return args && !args.empty? ? word % args : word end |
#enum_value_and_label(enum_item, column_name) ⇒ Object
ex.
colname = column['name']
column['enum'].each do |enum_item|
value, label = enum_value_and_label(enum_item, colname)
p value, label
end
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kwatable/template/helper/label.rb', line 76 def enum_value_and_label(enum_item, column_name) item = enum_item #return item.is_a?(Hash) ? [item['value'], item['label']] : [item, camel_case(item)] if item.is_a?(Hash) return item['value'], item['label'] else key = "#{column_name}_#{item}" label = _w(key) ? to_label(key) : to_label(item) return item, label end end |
#load_dictionary_files(option_path_list = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/kwatable/template/helper/label.rb', line 13 def load_dictionary_files(option_path_list=nil) path_list = [] option_path_list ||= @options[?I].split(/,/) if @options[?I] path_list += option_path_list if option_path_list path_list += Kwatable.template_path if Kwatable.template_path # lang = @properties[:lang] || 'en' dict_names = ['dictionary'] dict_names += @properties[:dict].split(/,/) if @properties[:dict] filenames = [] dict_names.each do |dict_name| fname = find_file("#{dict_name}.#{lang}.yaml", path_list) fname ||= find_file("#{dict_name}.en.yaml", path_list) unless lang == 'en' filenames << fname if fname end # dictionary = {} filenames.each do |filename| hash = YAML.load_file(filename) raise "#{filename}: mapping is required." unless hash.is_a?(Hash) dictionary.update(hash) end return dictionary end |
#to_label(key) ⇒ Object
‘foo’ => ‘Foo’, ‘foo_bar_baz’ => ‘Foo bar baz’
65 66 67 68 |
# File 'lib/kwatable/template/helper/label.rb', line 65 def to_label(key) word = w!(key) || key.to_s return word =~ /\A\w+\z/ ? word.capitalize.split(/_/).join(' ') : word end |
#w(key, *args) ⇒ Object
word
39 40 41 |
# File 'lib/kwatable/template/helper/label.rb', line 39 def w(key, *args) return _w(key, true, *args) end |
#w!(key, *args) ⇒ Object
44 45 46 |
# File 'lib/kwatable/template/helper/label.rb', line 44 def w!(key, *args) return _w(key, false, *args) end |