| 1 | package WWW::Connpass::Event::Question; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use JSON 2; | ||
| 6 | use Module::Load (); | ||
| 7 | |||
| 8 | my @QUESTION_TYPES = qw/FreeText CheckBox Radio PullDown/; | ||
| 9 | |||
| 10 | sub _answer_type { die "this is abstract method" } | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, %args) = @_; | ||
| 14 | die "$class is abstract class" if $class eq __PACKAGE__; | ||
| 15 | |||
| 16 | # assertion | ||
| 17 | if (exists $args{answer_type}) { | ||
| 18 | $args{answer_type} == $class->_answer_type | ||
| 19 | or die "Invalid answer type: $args{answer_type}"; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | $args{answer_type} = $class->_answer_type; | ||
| 23 | } | ||
| 24 | |||
| 25 | # normalize required option | ||
| 26 | $args{required} = $args{required} ? JSON::true : JSON::false; | ||
| 27 | |||
| 28 | # normalize answer_frame to options | ||
| 29 | if (exists $args{answer_frame}) { | ||
| 30 | $args{options} = [ | ||
| 31 | map { +{ title => $_ } } @{ | ||
| 32 | delete $args{answer_frame} | ||
| 33 | } | ||
| 34 | ]; | ||
| 35 | } | ||
| 36 | |||
| 37 | return bless \%args => $class; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub inflate { | ||
| 41 | my ($class, %args) = @_; | ||
| 42 | $class .= '::'.$QUESTION_TYPES[$args{answer_type} - 1]; | ||
| 43 | Module::Load::load($class); | ||
| 44 | return $class->new(%args); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub raw_data { +{%{$_[0]}} } | ||
| 48 | |||
| 49 | sub title { shift->{title} } | ||
| 50 | sub options { shift->{options} } | ||
| 51 | sub answer_type { shift->{answer_type} } | ||
| 52 | sub required { shift->{required} } | ||
| 53 | |||
| 54 | sub answer_frame { map { $_->{title} } @{ shift->{options} } } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | __END__ |