Class: Value

Inherits:
Object show all
Defined in:
lib/qooxview/value.rb

Overview

A Value is used to define a field in either an Entities or a View. It’s most simple definition is as following:

type :name

But there are other options. The full-blown syntax is:

type_flag1_flag2_StorageType :name, :arg1 => val1, arg2 => val2

with of course 0..n flags and 0..n arguments, and with or without a StorageType. StorageType of ALL is also possible, which will put the type in every StorageType available to the Enttiy. Used only for the id so far.

The to_s method prepares a string to be sent to the Frontend. It’s format is:

@dtype, name, string_on_screen, args

where string_on_screen is taken of an eventual translation-file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmds, arguments, dt = nil) ⇒ Value

Returns a new instance of Value.



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
# File 'lib/qooxview/value.rb', line 25

def initialize(cmds, arguments, dt = nil)
  dputs(3) { "Added new: #{cmds.inspect}, #{arguments.inspect}" }

  if cmds[-1] == 'ALL'
    @st = cmds.pop
  else
    @st = (StorageType.has? cmds[-1]) ? cmds.pop : dt
  end
  @name = arguments.shift
  @dtype = cmds.shift
  @args = {}
  @list = ''
  @eclass_proxy = nil
  @list_type = nil

  if arguments[-1].class == Hash
    dputs(5) { "Merging Hash #{arguments[-1].inspect} into #{@args.inspect}" }
    @args.merge! arguments.pop
  end

  # Do some special cases
  case @dtype
    when 'list'
      case cmds[0]
        when /(array|choice|drop|single)/
          @args.merge! :list_type => (@list_type = cmds.shift)
        when /entity/
          dputs(3) { "Adding an entity with #{cmds.inspect} - #{arguments.inspect}" }
          @dtype = 'list_entity'
          cmds.shift
          @entity_class = cmds.shift.pluralize_simple.gsub(/([A-Z])/, " \\1").
              split.collect { |s| s.capitalize }.join
          @args.merge! :list_type => (@list_type = :single)
          @show_method, @condition = arguments
      end
      if arguments[0]
        @list = arguments.shift
      end
    when 'select'
      @list = arguments.shift
    when 'entity'
      # So that "courseType" gets correctly translated to
      # "CourseTypes" (mind the capital in the middle)
      @entity_class = cmds.shift.pluralize_simple.gsub(/([A-Z])/, " \\1").
          split.collect { |s| s.capitalize }.join
      @args.merge! :list_type => (@list_type = arguments.shift)
      @show_method, @condition = arguments
      if @list_type && cmds.index('all') && @show_method == nil
        dputs(0) { "No show_method defined in #{@name} at #{caller[5].inspect}" }
        raise 'value_entity_uncomplete'
      end
    #if !@list_type
    #  dputs(0) { "No list-type for #{@name} at #{caller[5].inspect}" }
    #  raise "value_entity_uncomplete"
    #end
    when 'array'
      dputs(0) { 'Not yet supported!' }
      exit
    when 'info'
      @args.merge! :text => arguments.pop
    when 'html'
      @args.merge! :text => arguments.pop
    else
      if arguments.size > 0
        dputs(0) { "Arguments should be empty by now, but are #{arguments.inspect}!" }
        exit
      end
  end

  cmds.each { |c|
    @args.merge! c.to_sym => true
  }
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def args
  @args
end

#dtypeObject

Returns the value of attribute dtype.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def dtype
  @dtype
end

#entity_classObject

Returns the value of attribute entity_class.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def entity_class
  @entity_class
end

#listObject

Returns the value of attribute list.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def list
  @list
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def name
  @name
end

#stObject

Returns the value of attribute st.



23
24
25
# File 'lib/qooxview/value.rb', line 23

def st
  @st
end

Class Method Details

.simple(dtype, name, flags = []) ⇒ Object



149
150
151
# File 'lib/qooxview/value.rb', line 149

def self.simple(dtype, name, flags = [])
  return Value.new([dtype] + flags.to_a, [name])
end

Instance Method Details

#clone_laterObject

TODO: implement this cloning instead of deep_clone from object



195
196
197
198
199
200
201
202
# File 'lib/qooxview/value.rb', line 195

def clone_later
  dputs(0) { 'Cloning Value!' }
  v = Value.new([@dtype], [@name])
  v.st = @st
  v.args = @args
  v.list = @list
  return v
end

#eclassObject



153
154
155
156
157
158
159
# File 'lib/qooxview/value.rb', line 153

def eclass
  dputs(4) { "Asking eclass for #{self.inspect}" }
  if @dtype =~ /^(list_)*entity$/ and not @eclass_proxy
    @eclass_proxy = Entities.send(@entity_class)
  end
  return @eclass_proxy
end

#parse(p) ⇒ Object



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
# File 'lib/qooxview/value.rb', line 161

def parse(p)
  case @dtype
    when /entity/
      dputs(3) { "parsing #{@name}: #{p.inspect}" }
      case @list_type.to_sym
        when :drop, :single
          dputs(3) { "Getting entity for #{@list_type}-#{eclass.class.inspect}-" +
              "#{p.inspect}" }
          ret = eclass.match_by(eclass.data_field_id, p[0])
          dputs(3) { "And found #{ret.inspect}" }
          if not ret and @args.has_key? :empty
            dputs(3) { "Converting nil to 0 as we're an entity_empty" }
            ret = 0
          end
          return ret
        when :multi
          dputs(3) { "Getting entities for #{@list_type}-#{eclass.class.inspect}-" +
              "#{p.inspect}" }
          ret = p.collect { |el| eclass.match_by(eclass.data_field_id, el) }
          dputs(3) { "And found #{ret.inspect}" }
          if not ret and @args.has_key? :empty
            dputs(3) { "Converting nil to 0 as we're an entity_empty" }
            ret = 0
          end
          return ret
        else
          dputs(0) { "List-type #{@list_type} not supported yet in " +
              "#{@name}::#{@list_type}!" }
          return nil
      end
  end
end

#to_a(session = nil) ⇒ Object



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
# File 'lib/qooxview/value.rb', line 99

def to_a(session = nil)
  #dputs_func
  fe_type, fe_name, args = @dtype, @name, @args.dup
  case @dtype.to_s
    when 'list', 'select'
      dputs(3) { "List for #{@name} is #{@list}" }
      @list.size > 0 and args.merge! :list_values => eval(@list).to_a
    when /entity/
      dputs(3) { "Converting -#{@name}- to array" }
      fe_type = 'list'
      values = []
      if args.has_key?(:all) and
          ((!args.has_key?(:session)) or session)
        dputs(3) { "will search_all for #{eclass.name} in #{@name}: " }
        e_all = eclass.search_all_
        values = e_all.select { |e|
          begin
            dputs(3) { "Searching whether to show #{@name}/#{e.class.name}:#{e.inspect}" }
            dputs(3) { "Condition is #{@condition.inspect}" }
            if args.has_key?(:session)
              cond = @condition ? @condition.call(e, session) : true
            else
              cond = @condition ? @condition.call(e) : true
            end
            dputs(3) { "cond #{@condition}: #{cond}" }
            method = e.respond_to? @show_method
            dputs(3) { "method #{@show_method}: #{method}" }
            cond and method
          rescue Exception => err
            dputs(0) { "Error: while trying to work #{eclass.name} with #{e.inspect}" }
            dputs(0) { "Error: and condition #{@condition.inspect}: #{err.inspect}" }
            dputs(0) { "Callstack: #{caller.inspect}" }
            false
          end
        }.collect { |e|
          [e.send(eclass.data_field_id), e.send(@show_method)]
        }.sort { |a, b|
          a[1].to_s <=> b[1].to_s
        }
        if args.has_key? :empty
          values.unshift([0, '---'])
        end
      end
      args.merge! :list_values => values
      dputs(3) { "Args for entities is #{args.inspect}" }
  end
  dputs(3) { "Going to name #{fe_name} to #{GetText._(fe_name.to_s)}" }
  [fe_type, fe_name, GetText._(fe_name.to_s), args]
end