T
trans. (T. Onoma)
I am finding it difficult to properly generalize and organize my code. Perhaps
others can offer some advice. Here is a snip two related parts. I have about
five others like these:
# section
def parse_section_set_tab? ; false ; end
def parse_section_set_tab ; tab ; end
def parse_section?( line, tab )
true
end
def parse_section_skip? ; false ; end
def parse_section( block )
text = block.collect{ |line| line.text }.join("\n")
md = /([=]+)[ ]*(.*?)[ ]*[=]+/.match( text )
raise "Unexpeted match error" unless md
level = md[1].length
title = md[2]
dobj = ArtML:OM::Section.new( level, title )
until level > @document_stack.last.level do
@document_stack.pop
end
@document_stack.last << dobj # add this section to its parent
@document_stack << dobj # and pop it on
end
# paragraph
def parse_paragraph_set_tab? ; true ; end
def parse_paragraph_set_tab( line, tab ) ; line.indent ; end
def parse_paragraph?( line, tab )
line.kind != aragraph or line.indent > tab
end
def parse_paragraph_skip? ; false ; end
def parse_paragraph( block )
text = block.collect{ |line| line.text }.join("\n")
content = text #parse_inline( text )
@document_stack.last << ArtML:OM:aragraph.new( content )
end
In the generalized code these are called using #send. Eg.
if send("parse_#{state}?", line, tab)
send("parse_#{state}", block)
end
As you can see, I am using a name prefix "trick" to encapsulate these. I
would like to encapsulate them better, but I have a couple of difficulties.
My first problem is the use of @document_stack which they all work on. The
second problem is that I'm not sure what the encapsulation would be --they
consist of both data and code (but not state), yet there is only ever one of
each, so they aren't classes. And yet they don't seem like objects either b/c
the code differs between each of them. Perhaps use a module? That only seems
to adjust the name trick (Paragraph:arse instead of paragraph_parse). Plus,
I then have to do 'class << self', which seems ugly to me. Nor does it seem
like that's how a "module" is meant to be used.
What is the proper way to handle this in a class-based OOPL?
Thanks,
T.
others can offer some advice. Here is a snip two related parts. I have about
five others like these:
# section
def parse_section_set_tab? ; false ; end
def parse_section_set_tab ; tab ; end
def parse_section?( line, tab )
true
end
def parse_section_skip? ; false ; end
def parse_section( block )
text = block.collect{ |line| line.text }.join("\n")
md = /([=]+)[ ]*(.*?)[ ]*[=]+/.match( text )
raise "Unexpeted match error" unless md
level = md[1].length
title = md[2]
dobj = ArtML:OM::Section.new( level, title )
until level > @document_stack.last.level do
@document_stack.pop
end
@document_stack.last << dobj # add this section to its parent
@document_stack << dobj # and pop it on
end
# paragraph
def parse_paragraph_set_tab? ; true ; end
def parse_paragraph_set_tab( line, tab ) ; line.indent ; end
def parse_paragraph?( line, tab )
line.kind != aragraph or line.indent > tab
end
def parse_paragraph_skip? ; false ; end
def parse_paragraph( block )
text = block.collect{ |line| line.text }.join("\n")
content = text #parse_inline( text )
@document_stack.last << ArtML:OM:aragraph.new( content )
end
In the generalized code these are called using #send. Eg.
if send("parse_#{state}?", line, tab)
send("parse_#{state}", block)
end
As you can see, I am using a name prefix "trick" to encapsulate these. I
would like to encapsulate them better, but I have a couple of difficulties.
My first problem is the use of @document_stack which they all work on. The
second problem is that I'm not sure what the encapsulation would be --they
consist of both data and code (but not state), yet there is only ever one of
each, so they aren't classes. And yet they don't seem like objects either b/c
the code differs between each of them. Perhaps use a module? That only seems
to adjust the name trick (Paragraph:arse instead of paragraph_parse). Plus,
I then have to do 'class << self', which seems ugly to me. Nor does it seem
like that's how a "module" is meant to be used.
What is the proper way to handle this in a class-based OOPL?
Thanks,
T.