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

« back to all changes in this revision

Viewing changes to lib/ctioga2/commands/type.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
# type.rb: named types, based on metabuilder
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/metabuilder/types'
 
16
 
 
17
module CTioga2
 
18
 
 
19
  Version::register_svn_info('$Revision: 173 $', '$Date: 2010-10-22 21:41:39 +0200 (Fri, 22 Oct 2010) $')
 
20
 
 
21
  module Commands
 
22
 
 
23
    # A named type, based on CTioga2::MetaBuilder::Type
 
24
    #
 
25
    # @todo *Structural* in real, I don't think it is necessary
 
26
    # anymore to rely on MetaBuilder, as most types in CTioga2 already
 
27
    # provide a from_text class function that does a nice job. I
 
28
    # should convert as many things as possible to using that.
 
29
    class CommandType
 
30
 
 
31
      # The underlying  CTioga2::MetaBuilder::Type object.
 
32
      attr_accessor :type
 
33
 
 
34
      # The unique identification of this type.
 
35
      attr_accessor :name
 
36
 
 
37
      # The description of this type
 
38
      attr_accessor :description
 
39
 
 
40
      # The context of definition [file, line]
 
41
      attr_accessor :context
 
42
 
 
43
 
 
44
      # Makes sure the return value is a CommandType. Will fail
 
45
      # miserably if not.
 
46
      def self.get_type(obj)
 
47
        if obj.is_a? CommandType
 
48
          return obj
 
49
        else 
 
50
          if obj.is_a? Symbol
 
51
            warn {
 
52
              "Converting type specification #{obj.inspect} to string at #{caller[1]}"
 
53
            }
 
54
            obj = obj.to_s
 
55
          end
 
56
          type = Interpreter::type(obj)
 
57
          if type
 
58
            return type
 
59
          else
 
60
            raise InvalidType, "Type #{obj.inspect} unknown"
 
61
          end
 
62
        end
 
63
      end
 
64
 
 
65
      # _type_ is the type of the argument in a descriptive fashion,
 
66
      # as could be fed to CTioga2::MetaBuilder::Type.get_type, or
 
67
      # directly a MetaBuilder::Type object.
 
68
      def initialize(name, type, desc = nil)
 
69
        if type.is_a? MetaBuilder::Type
 
70
          @type = type
 
71
        else
 
72
          @type = CTioga2::MetaBuilder::Type.get_type(type)
 
73
        end
 
74
        @name = name 
 
75
        @description = desc
 
76
        caller[1].gsub(/.*\/ctioga2\//, 'lib/ctioga2/') =~ /(.*):(\d+)/
 
77
        @context = [$1, $2.to_i]
 
78
 
 
79
        Interpreter::register_type(self)
 
80
      end
 
81
 
 
82
      # Now, a series of redirection from/to the underlying
 
83
      # MetaBuilder::Type object.
 
84
 
 
85
      # Whether this is a boolean type or not.
 
86
      def boolean?
 
87
        return @type.boolean?
 
88
      end
 
89
 
 
90
      # Does the actual conversion from string to the real type
 
91
      def string_to_type(str)
 
92
        return @type.string_to_type(str)
 
93
      end
 
94
 
 
95
      # Returns the long option for the option parser.
 
96
      #
 
97
      # \todo maybe this should be rethought a bit ?
 
98
      def option_parser_long_option(name, param = nil)
 
99
        return @type.option_parser_long_option(name, param)
 
100
      end      
 
101
      
 
102
    end
 
103
 
 
104
  end
 
105
 
 
106
end
 
107