Class: Tcl::Msgcat::Parser
- Inherits:
-
Object
- Object
- Tcl::Msgcat::Parser
- Defined in:
- lib/tcl/msgcat/parser.rb
Overview
Parses a msgcat file into a ruby hash which can then be converted to whatever
Instance Attribute Summary collapse
-
#msgs ⇒ Object
readonly
Returns the value of attribute msgs.
Instance Method Summary collapse
-
#add(scopes, name, string) ⇒ Object
Adds a key and translation to the msg hash.
-
#initialize(file) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #to_json(pretty = true) ⇒ Object
Constructor Details
#initialize(file) ⇒ Parser
Returns a new instance of Parser.
9 10 11 12 13 14 |
# File 'lib/tcl/msgcat/parser.rb', line 9 def initialize(file) raise ArgumentError, "File not found" unless File.exist? file @msgs = {} @file = file end |
Instance Attribute Details
#msgs ⇒ Object (readonly)
Returns the value of attribute msgs.
7 8 9 |
# File 'lib/tcl/msgcat/parser.rb', line 7 def msgs @msgs end |
Instance Method Details
#add(scopes, name, string) ⇒ Object
Adds a key and translation to the msg hash
Uses eval to dynamically create a multi dimensional hash from the scopes array that is given
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tcl/msgcat/parser.rb', line 53 def add(scopes, name, string) chain = [] # run through each scope scopes.each_with_index do |k, i| chain = [] # build the chain of send commands from the top level # to the level of the current scope, for each iteration # # e.g. for iteration: # # first: hash[:scope1] # second: hash[:scope1][:scope2] # third: hash[:scope1][:scope2][:scope3] subkeys = scopes[0..i] subkeys.each do |k| chain << "send('[]', '#{k}')" end # if the (sub)key doesn't exist for this (chain of) scopes # then set it to an empty hash if eval("@msgs.#{chain.join(".")}").nil? set_chain = chain[0..-2] + ["send('[]=', '#{subkeys.last}', {})"] eval("@msgs.#{set_chain.join(".")}") end end # set the value a the end of the chain chain << "send('[]=', '#{name}', %q[#{string}])" eval("@msgs.#{chain.join(".")}") end |
#parse ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tcl/msgcat/parser.rb', line 16 def parse lines = File.read(@file).lines scopes = [] # strip the comments out lines.reject! {|l| l.match(/^\s?#/) } # run through each line and do an action # depending on the kind of line it is lines.each do |line| # start of a message group/namespace definition if match = line.match(/msgs (\S+) \{/) # msg ::site::irrigation { scopes << match[1] next end # end of the message group/namespace definition if match = line.match(/\}/) # } scopes.pop next end # an actual message definition if match = line.match(/m (\S+) "(.+)"/) # m label "the actual string" add(scopes, match[1], match[2]) end end self end |
#to_json(pretty = true) ⇒ Object
86 87 88 |
# File 'lib/tcl/msgcat/parser.rb', line 86 def to_json(pretty=true) pretty ? JSON.pretty_generate(@msgs) : @msgs.to_json end |