Module: InplaceI18n::HashTools
- Defined in:
- lib/inplace_i18n/hash_tools.rb
Instance Method Summary collapse
- #create_empty_node(path) ⇒ Object
- #create_path_if_not_exist(path) ⇒ Object
- #find_by_path(path) ⇒ Object
- #next_path(path) ⇒ Object
- #nodes_array(path = []) ⇒ Object
- #previous_path(path) ⇒ Object
- #update_by_path(path, string) ⇒ Object
- #update_by_path_wo_parents(path, string) ⇒ Object
Instance Method Details
#create_empty_node(path) ⇒ Object
48 49 50 51 |
# File 'lib/inplace_i18n/hash_tools.rb', line 48 def create_empty_node(path) return false if not find_by_path(path).nil? update_by_path_wo_parents(path, {}) end |
#create_path_if_not_exist(path) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/inplace_i18n/hash_tools.rb', line 53 def create_path_if_not_exist(path) aa = path_to_a(path) new_aa = [] aa.each do |a| new_aa << a create_empty_node(a_to_path(new_aa)) end end |
#find_by_path(path) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/inplace_i18n/hash_tools.rb', line 4 def find_by_path(path) e ="self" path_to_a(path).each{|i| e = e+"['"+ escape_for_eval(i)+ "']" } begin eval(e) rescue NoMethodError return nil end end |
#next_path(path) ⇒ Object
74 75 76 77 |
# File 'lib/inplace_i18n/hash_tools.rb', line 74 def next_path(path) paths = self.paths paths[paths.index(path) + 1] end |
#nodes_array(path = []) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/inplace_i18n/hash_tools.rb', line 62 def nodes_array(path = []) ret = [] self.each do |key,value| if value.class == Hash ret = ret + value.nodes_array(path + [key]) else ret << path + [key] end end ret end |
#previous_path(path) ⇒ Object
79 80 81 82 |
# File 'lib/inplace_i18n/hash_tools.rb', line 79 def previous_path(path) paths = self.paths paths[paths.index(path) - 1] end |
#update_by_path(path, string) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/inplace_i18n/hash_tools.rb', line 14 def update_by_path(path, string) return false if find_by_path(path).class == "Hash" create_path_if_not_exist(path) e ="self" path_a = path_to_a(path) path_a.each{|i| e = e+"['"+escape_for_eval(i)+"']" } string = string.gsub(/^[ \t]+/,"") string = escape_for_eval(string) e = e+"='"+string+"'" begin eval(e) return true rescue => e return [false, e] end end |
#update_by_path_wo_parents(path, string) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/inplace_i18n/hash_tools.rb', line 32 def update_by_path_wo_parents(path, string) return false if find_by_path(path).class.to_s == "Hash" && find_by_path(path).length > 0 e ="self" path_a = path_to_a(path) path_a.each{|i| e = e+"['"+ escape_for_eval(i)+"']" } string = escape_for_eval(string) e = e+"="+string.inspect+"" begin eval(e) return true rescue return false end end |