Class: MyHelp::Org2Hash
- Inherits:
-
Object
- Object
- MyHelp::Org2Hash
- Defined in:
- lib/my_help/org2hash.rb
Overview
get org and trans it to hash by FSM
Constant Summary collapse
- TRANSITIONS =
current_state => [ new_state, action ]
{ :header_read => { "* " => [:contents_read, :start_new_item], :default => [:header_read, :ignore], }, :contents_read => { "* " => [:contents_read, :start_new_item], :default => [:contents_read, :add_contents], }, }
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(org_text) ⇒ Org2Hash
constructor
A new instance of Org2Hash.
- #read_item(line) ⇒ Object
- #simple_fsm ⇒ Object
Constructor Details
#initialize(org_text) ⇒ Org2Hash
Returns a new instance of Org2Hash.
17 18 19 20 21 |
# File 'lib/my_help/org2hash.rb', line 17 def initialize(org_text) @text = org_text @contents = Hash.new simple_fsm() end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
4 5 6 |
# File 'lib/my_help/org2hash.rb', line 4 def contents @contents end |
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/my_help/org2hash.rb', line 4 def text @text end |
Instance Method Details
#read_item(line) ⇒ Object
41 42 43 44 |
# File 'lib/my_help/org2hash.rb', line 41 def read_item(line) m = line.match(/\* (.+)/) m ? m[1] : nil end |
#simple_fsm ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/my_help/org2hash.rb', line 23 def simple_fsm() state = :header_read item = "" @text.split("\n").each do |line| next if line.size < 1 state, action = TRANSITIONS[state][line[0..1]] || TRANSITIONS[state][:default] case action when :ignore when :start_new_item item = read_item(line) @contents[item] = "" when :add_contents @contents[item] += line + "\n" end end end |