~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/metabuilder/types/generic.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# generic.rb: conversion-function based type (or the Death of MetaBuilder)
 
2
# you can choose among several possibilities
 
3
# Copyright (C) 2010 Vincent Fourmond
 
4
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
18
 
 
19
require 'ctioga2/utils'
 
20
 
 
21
module CTioga2
 
22
 
 
23
  Version::register_svn_info('$Revision$', '$Date$')
 
24
 
 
25
 
 
26
  module MetaBuilder
 
27
 
 
28
    module Types
 
29
 
 
30
      # A type based on a conversion function from_text from a given
 
31
      # class/module.
 
32
      class FunctionBasedType < Type
 
33
 
 
34
        type_name :function_based
 
35
        
 
36
        def type_name
 
37
          return 'function_based'
 
38
        end
 
39
 
 
40
 
 
41
        def initialize(type)
 
42
          super
 
43
          raise "type must have a :class key" unless type.has_key?(:class)
 
44
          # We make a copy for our own purposes.
 
45
          @cls = type[:class]
 
46
          @func_name = type[:func_name] || :from_text
 
47
        end
 
48
 
 
49
        def string_to_type_internal(str)
 
50
          return @cls.send(@func_name, str)
 
51
        end
 
52
 
 
53
        def type_to_string_internal(val)
 
54
          return val.to_s
 
55
        end
 
56
      end
 
57
 
 
58
    end
 
59
  end
 
60
end