Class: Entity

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

Overview

Defines one simple Entity

Direct Known Subclasses

ConfigBase, Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, proxy) ⇒ Entity

Returns a new instance of Entity.



424
425
426
427
428
429
430
431
# File 'lib/qooxview/entity.rb', line 424

def initialize(id, proxy)
  dputs(5) { "Creating entity -#{proxy}- with id #{id}" }
  @id = id.to_i
  @proxy = proxy
  @changed = false
  @cache = {}
  @show_error_missing = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd, *args) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/qooxview/entity.rb', line 470

def method_missing(cmd, *args)
  #cmd == :owner ? dputs_func : dputs_unfunc
  dputs(5) { "Entity#method_missing #{cmd} in #{self.class.name}," +
      " with #{args.inspect} and #{args[0].class}" }
  field = cmd.to_s
  field_clean = field.sub(/^_/, '').sub(/=$/, '')
  if not @proxy.get_value(field_clean)
    case field_clean
      when /^listp_(.*)/
        dputs(3) { "Returning listp for #{cmd} - #{$1}" }
        return [self.id, self.send($1)]
    end
    @show_error_missing && dputs(0) { "ValueUnknown for #{cmd.inspect} in "+
        "#{self.class.name} - #{caller.inspect}" }
    if field =~ /^_/
      raise 'ValueUnknown'
    else
      return super
    end
  end
  case field
    when /=$/
      # Setting the value
      dputs(5) { "data_set #{field} for class #{self.class.name}" }
      field_set = "_#{field.chop.sub(/^_/, '')}"

      if not old_respond_to? "#{field}".to_sym
        dputs(3) { "Creating method #{field} for #{self.class.name}" }
        dputs(4) { "Self is #{self.public_methods.sort.inspect}" }
        self.class.class_eval <<-RUBY
          def #{field}( v )
            data_set( "#{field_set}".to_sym, v )
          end
        RUBY
        dputs(4) { "Sending #{args[0]} to #{field}" }
        send(field, args[0])
      else
        dputs(0) { "#{field} is already defined - don't know what to do..." }
        caller.each { |c|
          dputs(0) { "Caller is #{c.inspect}" }
        }
      end
    else
      # Getting the value
      dputs(5) { "data_get #{field} for class #{self.class.name}" }

      if not old_respond_to? field
        self.class.class_eval <<-RUBY
          def #{field}
            return @cache[:#{field}] if @cache.has_key? :#{field}
            @cache._#{field} = data_get( "_#{field.sub(/^_/, '')}", false )
          end
        RUBY
        send(field)
      else
        dputs(0) { "#{field} is already defined - don't know what to do" }
        caller.each { |c|
          dputs(0) { "Caller is #{c.inspect}" }
        }
      end
    #      data_get( field )
  end
end

Instance Attribute Details

#changedObject

Returns the value of attribute changed.



422
423
424
# File 'lib/qooxview/entity.rb', line 422

def changed
  @changed
end

#idObject (readonly)

Returns the value of attribute id.



421
422
423
# File 'lib/qooxview/entity.rb', line 421

def id
  @id
end

#show_error_missingObject

Returns the value of attribute show_error_missing.



422
423
424
# File 'lib/qooxview/entity.rb', line 422

def show_error_missing
  @show_error_missing
end

Instance Method Details

#dataObject



557
558
559
560
561
562
563
564
565
566
# File 'lib/qooxview/entity.rb', line 557

def data
  if defined? @storage
    @storage.each { |k, di|
      if not di.data_cache
        @proxy.data_update(@id)
      end
    }
  end
  @proxy.data[@id]
end

#data_get(field, raw = false, dbg = 3) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/qooxview/entity.rb', line 568

def data_get(field, raw = false, dbg = 3)
  #dputs_func
  ret = [field].flatten.collect { |f_orig|
    f = f_orig.to_s
    (direct = f =~ /^_/) and f.sub!(/^_/, '')
    dputs(4) { "Direct is #{direct.inspect} for #{f_orig.inspect}" }
    if (self.public_methods.index(f)) and (not direct)
      dputs(4) { "found direct method for #{f} in #{self.class}" }
      send(f)
    else
      dputs(4) { "Using proxy #{@proxy.class.name} for #{f}" }
      e = @proxy.get_entry(@id, f)

      dputs(5) { "e is #{e.inspect} from #{@proxy.data.inspect}" }
      if not raw and e
        v = @proxy.get_value(f)

        if (e.class.to_s =~ /(Integer|Fixnum)/) && v && v.dtype == 'entity'
          dputs(5) { "Getting instance for #{v.inspect}" }
          dputs(5) { "Getting instance with #{e.class} - #{e.inspect}" }
          dputs(5) { "Field = #{field}; id = #{@id}" }
          if e > 0 or @proxy.null_allowed
            e = v.eclass.get_data_instance([e].flatten.first)

          else
            return nil
          end
        elsif v and v.dtype == 'list_entity'
          dputs(4) { "Converting list_entity #{v.inspect} of #{e.inspect}" }
          e = e.collect { |val|
            v.eclass.get_data_instance(val)
          }
        end
      end
      e
    end
  }
  dputs(4) { "Return is #{ret.inspect}" }
  ret.length == 1 ? ret[0] : ret
end

#data_set(field_orig, value) ⇒ Object



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/qooxview/entity.rb', line 609

def data_set(field_orig, value)
  field = field_orig.to_s
  (direct = field =~ /^_/) and field.sub!(/^_/, '')
  dputs(4) { "Direct is #{direct.inspect} for field #{field_orig.inspect}" }
  v = value
  dputs(4) { "Self is #{self.public_methods.sort.inspect}" }
  if (self.public_methods.index("#{field}=".to_sym)) && (not direct)
    dputs(3) { "Setting #{field} through local method" }
    send("#{field}=".to_sym, v)
  else
    dputs(4) { "setting entry #{field} to #{v.inspect}" }
    @proxy.set_entry(@id, field, v)
    dputs(4) { 'Finished setting entry' }
  end
  @changed = true
  @proxy.changed = true
  @cache.delete field.to_sym

  self
end

#data_set_hash(data, create = false) ⇒ Object

Save all data in the hash for which we have an entry



631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/qooxview/entity.rb', line 631

def data_set_hash(data, create = false)
  dputs(4) { "#{data.inspect} - #{create} - id is #{id}" }
  fields = @proxy.get_field_names
  data.each { |k, v|
    ks = k.to_sym
    # Only set data for which there is a field
    if fields.index(ks)
      dputs(4) { "Setting field #{ks}" }
      data_set(ks, v)
    end
  }
  self
end

#deleteObject

Deletes the entry from the main part



553
554
555
# File 'lib/qooxview/entity.rb', line 553

def delete
  @proxy.delete_id(@id)
end

#get_uniqueObject



645
646
647
648
# File 'lib/qooxview/entity.rb', line 645

def get_unique
  dputs(5) { "Unique for #{self.inspect}" }
  data_get(@proxy.data_field_id)
end

#init_instanceObject



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/qooxview/entity.rb', line 433

def init_instance
  @pre_init = true
  if true
    @proxy.values.each { |v|
      field = v.name
      if self.public_methods.index("#{field}=".to_sym) &&
          (value = data_get(field))
        dputs(3) { "Setting #{field} to #{value}" }
        send("#{field}=".to_sym, value)
      end
    }
  end

  setup_instance
  @pre_init = false
end

#inspectObject



654
655
656
657
# File 'lib/qooxview/entity.rb', line 654

def inspect
  #@id
  to_hash.inspect
end

#old_respond_to?Object



454
# File 'lib/qooxview/entity.rb', line 454

alias_method :old_respond_to?, :respond_to?

#respond_to?(cmd, all = false) ⇒ Boolean

Returns:

  • (Boolean)


456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/qooxview/entity.rb', line 456

def respond_to?(cmd, all = false)
  field = cmd.to_s
  if field == 'to_ary'
    dputs(4) { 'not responding to_ary' }
    return false
  end
  case field
    when /=$/
      return true
    else
      return (@proxy.get_value(cmd) or super(cmd, all))
  end
end

#setup_instanceObject

Dummy setup - replace with real setup



451
452
# File 'lib/qooxview/entity.rb', line 451

def setup_instance
end

#to_aObject



659
660
661
662
# File 'lib/qooxview/entity.rb', line 659

def to_a
  dputs(5) { "to_a on #{self}" }
  [self]
end

#to_frontendObject



664
665
666
# File 'lib/qooxview/entity.rb', line 664

def to_frontend
  [id, to_hash.collect { |k, v| v }.join(':')]
end

#to_hash(unique_ids = false) ⇒ Object



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/qooxview/entity.rb', line 534

def to_hash(unique_ids = false)
  ret = @proxy.data[@id].dup
  dputs(5) { "Will return #{ret.to_a.join('-')}" }
  ret.each { |f, v|
    dputs(5) { "Doing field #{f} with #{v.inspect}" }
    #if data_get(f).is_a? Entity
    if value = @proxy.get_value(f) and value.dtype == 'entity'
      dputs(5) { 'Is an entity' }
      if unique_ids
        ret[f] = (d = data_get("_#{f}")) ? d.get_unique : nil
      else
        ret[f] = [v]
      end
    end
  }
  ret
end

#true(*args) ⇒ Object



650
651
652
# File 'lib/qooxview/entity.rb', line 650

def true(*args)
  return true
end