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

« back to all changes in this revision

Viewing changes to lib/ctioga2/data/backends/parameter.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
# parameter.rb : A class to describe a parameter
 
2
# Copyright (C) 2006 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.
 
13
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
 
 
18
require 'ctioga2/utils'
 
19
 
 
20
require 'ctioga2/metabuilder/type'
 
21
 
 
22
module CTioga2
 
23
 
 
24
  Version::register_svn_info('$Revision: 17 $', '$Date: 2009-04-28 22:22:22 +0200 (Tue, 28 Apr 2009) $')
 
25
 
 
26
  module Data
 
27
 
 
28
    module Backends
 
29
 
 
30
      # A parameter describes a way of storing some information into an
 
31
      # instance of an object. No type checking is done on the target object
 
32
      # when the actual reading/writing is done. However, the type checking
 
33
      # is done upstream by the Description system.
 
34
      #
 
35
      # A Parameter consists of several things:
 
36
      #
 
37
      # * a #name, to identify it in a unique fashion;
 
38
      # * a #type, used to convert to and from String and for user
 
39
      #   interaction in general;
 
40
      # * some explanative text, used to inform the user: #long_name and
 
41
      #   #description
 
42
      # * two symbols that are used to gain read and write access of the
 
43
      #   parameter on the target object.
 
44
      #
 
45
      # The Parameter class can be used to maintain a set of
 
46
      # meta-informations about types in a given object.
 
47
      class Parameter 
 
48
        
 
49
        # The short name of the parameter
 
50
        attr_accessor :name
 
51
        
 
52
        # The long name of the parameter, to be translated
 
53
        attr_accessor :long_name
 
54
        
 
55
        # The function names that should be used to set the symbol and
 
56
        # retrieve it's current value. The corresponding functions should
 
57
        # read or return a string, and writer(reader) should be a noop.
 
58
        attr_accessor :reader_symbol, :writer_symbol
 
59
        
 
60
        # The (text) description of the parameter
 
61
        attr_accessor :description
 
62
        
 
63
        # The actual Commands::CommandType of the parameter
 
64
        attr_accessor :type
 
65
 
 
66
        # Creates a new Parameter with the given symbols. Remember that
 
67
        # if you don't intend to use #get, #get_raw, #set and #set_raw,
 
68
        # you don't need to pass meaningful values to _writer_symbol_ and
 
69
        # _reader_symbol_. 
 
70
        def initialize(name, writer_symbol,
 
71
                       reader_symbol,
 
72
                       long_name, type,
 
73
                       description)
 
74
          @name = name
 
75
          @writer_symbol = writer_symbol
 
76
          @reader_symbol = reader_symbol
 
77
          @description = description
 
78
          @long_name = long_name
 
79
          @type = Commands::CommandType::get_type(type)
 
80
        end
 
81
 
 
82
 
 
83
        # Sets directly the target parameter, without type conversion
 
84
        def set_value(target, val)
 
85
          target.send(@writer_symbol, val) 
 
86
        end
 
87
        
 
88
        # Uses the #writer_symbol of the _target_ to set the value of the
 
89
        # parameter to the one converted from the String _str_
 
90
        def set_from_string(target, str)
 
91
          set_value(target, string_to_type(str)) 
 
92
        end
 
93
        
 
94
 
 
95
        # Aquires the value from the backend, and returns it in the
 
96
        # form of a string
 
97
        def get_string(target)
 
98
          return type_to_string(get_value(target))
 
99
        end
 
100
 
 
101
        # Aquires the value from the backend, and returns it.
 
102
        def get_value(target)
 
103
          target.send(@reader_symbol)
 
104
        end
 
105
 
 
106
      end
 
107
    end
 
108
  end
 
109
end