Skip to content

Commit

Permalink
Fix for Bug # 1046. The default field argument to update_from_pod() now
Browse files Browse the repository at this point in the history
checks the default field value on an as-needed basis. This allows it to work when
not all elements and subelements use a field with the same key name, which is
of course the general rule.
  • Loading branch information
theory committed Oct 26, 2005
1 parent b364500 commit 2726065
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
71 changes: 35 additions & 36 deletions lib/Bric/Biz/Element/Container.pm
Expand Up @@ -1623,7 +1623,11 @@ B<Throws:>
=item *
No such field "[_1]", did you mean "[_2]"?
No context for content beginning at line [_1].
=item *
No such field "[_1]" at line [_2]. Did you mean "[_3]"?
=item *
Expand Down Expand Up @@ -1685,6 +1689,7 @@ of those values.

sub update_from_pod {
my ($self, $pod, $def_field) = @_;
$def_field = '' unless defined $def_field;
$self->_deserialize_pod([ split /\r?\n|\r/, $pod ], $def_field, '', 0);
return $self;
}
Expand Down Expand Up @@ -2015,6 +2020,30 @@ in exception messages.
=cut

sub _bad_def_field {
my ($field_types, $key_name, $line_num) = @_;

# Throw an exception if there is no default field key name.
throw_invalid
error => "No context for content beginning at line $line_num.",
maketext => [
'No context for content beginning at line [_1].',
$line_num,
] unless defined $key_name && $key_name ne '';

# Suggest an alternative default field.
my $try =_find_closest_word($key_name, keys %$field_types);
throw_invalid
error => qq{No such field "$key_name" at line $line_num. }
. qq{Did you mean "$try"?},
maketext => [
'No such field "[_1]" at line [_2]. Did you mean "[_3]"?',
$key_name,
$line_num,
$try,
];
}

sub _deserialize_pod {
my ($self, $pod, $def_field, $indent, $line_num) = @_;

Expand All @@ -2032,23 +2061,6 @@ sub _deserialize_pod {
my %field_types = map { $_->get_key_name => $_ }
$elem_type->get_field_types;

# Set up the default field.
if (defined $def_field && $def_field ne '') {
unless ($field_types{$def_field}) {
my $try = _find_closest_word($def_field, keys %field_types);
throw_invalid
error => qq{No such field "$def_field", did you mean "$try"?},
maketext => [
'No such field "[_1]", did you mean "[_2]"?',
$def_field,
$try,
]
;
}
} else {
$def_field = '';
}

# Gather up the existing elements and fields.
my (%elems_for, %fields_for);
for my $e ($self->get_elements) {
Expand Down Expand Up @@ -2132,7 +2144,7 @@ sub _deserialize_pod {
\@subpod,
$def_field,
($subindent || ''),
$line_num
$line_num,
);
next POD;
}
Expand Down Expand Up @@ -2230,20 +2242,8 @@ sub _deserialize_pod {
$kn = $1;
$field_type = $field_types{$kn};
unless ($field_type) {
unless ($fields_for{$kn} && @{$fields_for{$kn}}) {
my $try = _find_closest_word($kn, keys %field_types);
throw_invalid
error => qq{No such field "$kn" at line }
. qq{$line_num. Did you mean "$try"?},
maketext => [
'No such field "[_1]" at line [_2]. Did you mean '
. '"[_3]"?',
$kn,
$line_num,
$try
]
;
}
_bad_def_field(\%field_types, $kn, $line_num)
unless $fields_for{$kn} && @{$fields_for{$kn}};
$field_type = shift @{$fields_for{$kn}};
}

Expand Down Expand Up @@ -2296,10 +2296,9 @@ sub _deserialize_pod {
}

else {
throw_gen "No context for content beginning at line $line_num"
unless $def_field ne '';
$kn = $def_field;
$field_type = $field_types{$kn};
$field_type = $field_types{$kn}
|| _bad_def_field(\%field_types, $kn, $line_num);
if ($field_ord{$kn} && !$field_type->get_quantifier) {
throw_invalid
error => qq{Non-repeatable field "$kn" appears more }
Expand Down
23 changes: 12 additions & 11 deletions t/Bric/Biz/Element/Container/DevTest.pm
Expand Up @@ -490,7 +490,7 @@ sub test_pod : Test(221) {
is $pqs[1]->get_value('date'), '1933-03-04 00:00:00', 'Check second PQ date';

# Try adding a page with its own pull quote subelement.
ok $elem->update_from_pod($self->pod_output_plus_page),
ok $elem->update_from_pod($self->pod_output_plus_page, 'para'),
'Update from POD with page and different indent';

# Check the contents.
Expand All @@ -511,11 +511,11 @@ sub test_pod : Test(221) {

# Check the page.
ok my $page_elem = $elem->get_container('_page_');
is $page_elem->get_value('para'),
is $page_elem->get_value('paragraph'),
"This is the first paragraph from page one.\nIt isn't a long "
. "paragraph.\nBut it'll do.",
'Check the page paragraph';
ok my $ppara = $page_elem->get_field('para'), 'Get page para';
ok my $ppara = $page_elem->get_field('paragraph'), 'Get page paragraph';
is $ppara->get_place, 0, q{Check page paragraph's place};
is $ppara->get_object_order, 1, q{Check page paragraph's obj order};
ok my $subpq = $page_elem->get_container('_pull_quote_'),
Expand All @@ -530,14 +530,17 @@ sub test_pod : Test(221) {
is $subpq->get_value('date'), '2004-12-03 00:00:00', q{... And its date};

# Test a bad default field.
eval { $elem->update_from_pod('', 'par') };
eval { $elem->update_from_pod('foo', 'par') };
ok my $err = $@, 'Catch invalid default field excetpion';
isa_ok $err, 'Bric::Util::Fault::Error::Invalid';
is $err->error, 'No such field "par", did you mean "para"?',
is $err->error, 'No such field "par" at line 1. Did you mean "para"?',
'Should get the correct exception message';
is_deeply $err->maketext,
['No such field "[_1]", did you mean "[_2]"?', 'par', 'para' ],
'Should get the correct maketext array';
is_deeply $err->maketext, [
'No such field "[_1]" at line [_2]. Did you mean "[_3]"?',
'par',
1,
'para'
], 'Should get the correct maketext array';

# Try a bad field.
eval { $elem->update_from_pod("=para\n\nfoo\n\n=par\n\n") };
Expand Down Expand Up @@ -931,16 +934,14 @@ sub pod_output_plus_pq {
sub pod_output_plus_page {
return shift->pod_output . q{=begin _page_
=para
=paragraph
This is the first paragraph from page one.
It isn't a long paragraph.
But it'll do.
=begin _pull_quote_
=para
Granted, Opera has been available for a while, but it remains the province of a devoted few -- the Amiga of Web browsers.
=by
Expand Down
2 changes: 1 addition & 1 deletion t/Bric/Biz/Element/DevTest.pm
Expand Up @@ -170,7 +170,7 @@ sub create_element_types {

# Give it a paragraph field.
ok my $page_para = $page->new_field_type({
key_name => 'para',
key_name => 'paragraph',
name => 'Paragraph',
required => 0,
quantifier => 0,
Expand Down

0 comments on commit 2726065

Please sign in to comment.