summaryrefslogtreecommitdiff
path: root/lib/CIL/Base.pm
blob: 34fba603b1b696e7a20f3cf1f900068c5ffdfcdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48
49
50
51
52
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
## ----------------------------------------------------------------------------
# cil is a Command line Issue List
# Copyright (C) 2008 Andrew Chilton
#
# This file is part of 'cil'.
#
# cil is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
## ----------------------------------------------------------------------------

package CIL::Base;

use strict;
use warnings;
use Carp;
use DateTime;
use CIL::Utils;

use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw(CreatedBy Inserted Updated));

## ----------------------------------------------------------------------------

sub new_from_name {
    my ($class, $cil, $name) = @_;

    croak 'provide a name'
        unless defined $name;

    my $filename = $class->filename($cil, $name);
    croak "filename '$filename' does no exist"
        unless $cil->file_exists($filename);

    my $data = $cil->parse_cil_file($filename, $class->last_field);
    my $issue = $class->new_from_data( $name, $data );
    return $issue;
}

sub new_from_data {
    my ($class, $name, $data) = @_;

    croak 'please provide an issue name'
        unless defined $name;

    # ToDo: check we have all the other correct fields

    # create the issue
    my $self = $class->new( $name );

    my $fields = $class->fields();
    my $array_fields = $class->array_fields();

    # save each field
    foreach my $field ( @$fields ) {
        next unless defined $data->{$field};

        # make it an array if it should be one
        if ( exists $array_fields->{$field} and ref $data->{$field} ne 'ARRAY' ) {
            $data->{$field} = [ $data->{$field} ];
        }

        # modify the data directly, otherwise Updated will kick in
        $self->set_no_update($field, $data->{$field});
    }
    $self->set_no_update('Changed', 0);
    $self->set_no_update('Updated', $data->{Updated});

    return $self;
}

sub new_from_fh {
    my ($class, $name, $fh) = @_;

    croak 'please provide name'
        unless defined $name;

    my $data = CIL::Utils->parse_from_fh( $fh, $class->last_field );
    return $class->new_from_data( $name, $data );
}

sub set_data {
    my ($self, $data) = @_;

    # loop through all the allowed fields
    my $fields = $self->fields();
    my $array_fields = $self->array_fields();

    # save each field
    foreach my $field ( @$fields ) {
        next unless defined $data->{$field};

        # make it an array if it should be one
        if ( exists $array_fields->{$field} and ref $data->{$field} ne 'ARRAY' ) {
            $data->{$field} = [ $data->{$field} ];
        }

        # modify the data directly, otherwise Updated will kick in
        $self->set_no_update($field, $data->{$field});
    }
    $self->set_no_update('Changed', 1);

    $self->{data} = $data;
}

sub save {
    my ($self, $cil) = @_;

    my $filename = $self->filename($cil, $self->name);

    my $fields = $self->fields();

    $cil->save( $filename, $self->{data}, @$fields );
}

sub as_output {
    my ($self) = @_;
    my $fields = $self->fields();
    return CIL::Utils->format_data_as_output( $self->{data}, @$fields );
}

sub filename {
    my ($class, $cil, $name) = @_;

    # create the filename from it's parts
    my $prefix    = $class->prefix();
    my $issue_dir = $cil->IssueDir;
    my $filename  = "${issue_dir}/${prefix}_${name}.cil";

    return $filename;
}

# override Class::Accessor's get
sub get {
    my ($self, $field) = @_;
    croak "provide a field name"
        unless defined $field;
    $self->{data}{$field};
}

# override Class::Accessor's set
sub set {
    my ($self, $field, $value) = @_;
    croak "provide a field name"
        unless defined $field;

    my $orig = $self->get($field);

    # finish if both are defined and they're the same
    if ( defined $orig and defined $value ) {
        return if $orig eq $value;
    }

    # finish if neither are defined
    return unless ( defined $orig or defined $value );

    # since we're actually changing the field, say we updated something
    $self->{data}{$field} = $value;
    $self->set_updated_now;
}

# so that we can update fields without 'Updated' being changed
sub set_no_update {
    my ($self, $field, $value) = @_;
    $self->{data}{$field} = $value;
}

sub set_inserted_now {
    my ($self) = @_;
    my $time = DateTime->now->iso8601;
    $self->{data}{Inserted} = $time;
    $self->{data}{Updated} = $time;
    $self->{Changed} = 1;
}

sub set_updated_now {
    my ($self) = @_;
    my $time = DateTime->now->iso8601;
    $self->{data}{Updated} = $time;
    $self->{Changed} = 1;
}

sub flag_as_updated {
    my ($self) = @_;
    $self->{Changed} = 1;
}

sub changed {
    my ($self) = @_;
    return $self->{Changed};
}

sub set_name {
    my ($self, $name) = @_;

    croak 'provide a name'
        unless defined $name;

    $self->{name} = $name;
}

sub name {
    my ($self) = @_;
    return $self->{name};
}

sub errors {
    my $self = shift;
    if( @_ ) {
        $self->{errors} = $_[0];
    }
    return $self->{errors};
}

## ----------------------------------------------------------------------------
1;
## ----------------------------------------------------------------------------