aboutsummaryrefslogtreecommitdiff
path: root/ruby/metadata_file.cc
blob: 55ffad8b87ddf33cdf17bfb0fb33bb74d1f5a937 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Richard Brown <mynamewasgone@gmail.com>
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis_ruby.hh>
#include <paludis/qa/metadata_file.hh>
#include <paludis/util/stringify.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::qa;
using namespace paludis::ruby;

#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)

namespace
{
    static VALUE c_metadata_file;

    VALUE
    metadata_file_init(int, VALUE *, VALUE self)
    {
        return self;
    }

    VALUE
    metadata_file_new(int argc, VALUE *argv, VALUE self)
    {
        MetadataFile * ptr(0);
        try
        {
            if (1 == argc)
            {
                std::string s = StringValuePtr(argv[0]);
                FSEntry fse = FSEntry(s);
                if (fse.is_regular_file())
                    ptr = new MetadataFile(fse);
                else
                    ptr = new MetadataFile(s);
            }
            else
            {
                rb_raise(rb_eArgError, "MetadataFile expects one argument, but got %d",argc);
            }
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<MetadataFile>::free, ptr));
            rb_obj_call_init(tdata, argc, argv);
            return tdata;
        }
        catch (const std::exception & e)
        {
            delete ptr;
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     herds -> Array
     *
     * Array of herds specified in file.
     */
    VALUE
    metadata_file_herds(VALUE self)
    {
        MetadataFile * ptr;
        Data_Get_Struct(self, MetadataFile, ptr);

        VALUE result(rb_ary_new());
        for (MetadataFile::HerdsIterator i (ptr->begin_herds()),
                i_end(ptr->end_herds()) ; i != i_end ; ++i)
            rb_ary_push(result, rb_str_new2(i->c_str()));
        return result;
    }

    /*
     * call-seq:
     *     maintainers -> Hash
     *
     * Hash of maintainers specified in file, keys are :email and :name.
     */
    VALUE
    metadata_file_maintainers(VALUE self)
    {
        MetadataFile * ptr;
        Data_Get_Struct(self, MetadataFile, ptr);
        VALUE result(rb_ary_new());
        VALUE result_hash;
        for (MetadataFile::MaintainersIterator i(ptr->begin_maintainers()),
                i_end(ptr->end_maintainers()) ; i != i_end ; ++i)
        {
            if (i->first.empty() && i->second.empty())
                continue;

            result_hash = rb_hash_new();
            if (!i->first.empty())
                rb_hash_aset(result_hash, ID2SYM(rb_intern("email")), rb_str_new2(i->first.c_str()));
            if (!i->second.empty())
                rb_hash_aset(result_hash, ID2SYM(rb_intern("name")), rb_str_new2(i->second.c_str()));
            rb_ary_push(result, result_hash);
        }
        return result;
    }
    void do_register_metadata_file()
    {
        /*
         * Document-class: Paludis::QA::MetadataFile
         *
         * Wrapper around metadata.xml files.
         */
        c_metadata_file = rb_define_class_under(paludis_qa_module(), "MetadataFile", rb_cObject);
        rb_define_singleton_method(c_metadata_file, "new", RUBY_FUNC_CAST(&metadata_file_new),-1);
        rb_define_method(c_metadata_file, "initialize", RUBY_FUNC_CAST(&metadata_file_init),-1);
        rb_define_method(c_metadata_file, "herds", RUBY_FUNC_CAST(&metadata_file_herds),0);
        rb_define_method(c_metadata_file, "maintainers", RUBY_FUNC_CAST(&metadata_file_maintainers),0);
    }
}

VALUE
paludis::ruby::metadata_file_to_value(const MetadataFile & v)
{
    MetadataFile * vv(new MetadataFile(v));
    return Data_Wrap_Struct(c_metadata_file, 0, &Common<MetadataFile>::free, vv);
}

RegisterRubyClass::Register paludis_ruby_register_metadata_file PALUDIS_ATTRIBUTE((used))
    (&do_register_metadata_file);