Class: Application

Inherits:
Object
  • Object
show all
Extended by:
EventBus
Includes:
Configurable, Persistable
Defined in:
lib/a-commons.rb

Direct Known Subclasses

TkApplication

Defined Under Namespace

Classes: ApplicationParams

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventBus

attach_listener, broadcast_event, detach_listener, process_event

Methods included from Persistable

#append_persistent_property, #override_persistent

Methods included from Configurable

#properties_file2hash, properties_group, #resolve_link, #resolve_properties_link

Constructor Details

#initialize(_ap = ApplicationParams.new) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Yields:

  • (_self)

Yield Parameters:

  • _self (Application)

    the object that the method was called on



595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/a-commons.rb', line 595

def initialize(_ap=ApplicationParams.new)
  @@instance = self
  eval('$'+_ap.name+'=self')
  publish('applicationParams', _ap)
  publish(_ap.name,self)
  @first_run = false
  self['applicationParams'].persistent_file = File.join(local_dir, self['applicationParams'].name+'.pers')
  if !File.exists?(self['applicationParams'].persistent_file)
    File.new(self['applicationParams'].persistent_file, File::CREAT).close
  end
  publish('conf', properties_file2hash(self['applicationParams'].config_file)) if self['applicationParams'].config_file
  publish('pers', properties_file2hash(self['applicationParams'].persistent_file)) if self['applicationParams'].persistent_file
  yield(self) if block_given?
end

Class Method Details

.conf(_property) ⇒ Object



614
615
616
# File 'lib/a-commons.rb', line 614

def Application.conf(_property)
 @@instance['conf'][_property] if @@instance
end

.conf_group(_group) ⇒ Object



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/a-commons.rb', line 622

def Application.conf_group(_group)
  @@conf_groups = Hash.new if !defined?(@@conf_groups)
  if @@conf_groups[_group].nil?
    @@conf_groups[_group] = Hash.new
    glen=_group.length
    @@instance['conf'].keys.sort.each{|k|
      if k[0..glen] == "#{_group}."
        @@conf_groups[_group][k[glen+1..-1]]=@@instance['conf'][k]
      elsif @@conf_groups[_group].length > 0
        break
      end
    }
  end
  @@conf_groups[_group]
end

.instanceObject



610
611
612
# File 'lib/a-commons.rb', line 610

def Application.instance
  @@instance
end

Instance Method Details

#[](_name) ⇒ Object



717
718
719
720
721
722
723
# File 'lib/a-commons.rb', line 717

def [](_name)
  if @objs[_name]
    return @objs[_name]
  else
    raise RuntimeError, "resurce '"+_name+"' unavabled ", caller
  end
end

#[]=(_name, _value) ⇒ Object



725
726
727
728
729
# File 'lib/a-commons.rb', line 725

def []=(_name, _value)
  if @objs[_name]
    @objs[_name] = _value
  end
end

#create(_name, _class) ⇒ Object



709
710
711
# File 'lib/a-commons.rb', line 709

def create(_name, _class)
  register(_name,_class.new)
end

#load_local_config(_create_if_not_exist = true) ⇒ Object

this method load config file from local directory for personalizations



652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/a-commons.rb', line 652

def load_local_config(_create_if_not_exist=true)
    local_file_config = File.join(local_dir,File.basename(self['applicationParams'].config_file))
    if FileTest.exist?(local_file_config)
      self['conf'].update(self.properties_file2hash(local_file_config))
    elsif _create_if_not_exist
      if FileTest.writable?(local_dir)
        f = File.new(local_file_config, "w")
        begin
          if f
            p = self['conf']
            if p
              p.keys.sort.each{|key|
                f.syswrite('#'+key+'='+self['conf'][key]+"\n")
              }
            end
          end
        ensure
          f.close unless f.nil?
        end
      else
        msg = "Locad dir "+'"'+local_dir+'"'+" must be writable!"
        Arcadia.dialog(self, 'type'=>'ok','title' => '(Arcadia)', 'msg' => msg, 'level'=>'error')
        exit
      end
    end
end

#load_theme(_name = nil) ⇒ Object



679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/a-commons.rb', line 679

def load_theme(_name=nil)
  _theme_file = "conf/theme-#{_name}.conf" if !_name.nil?
  if _theme_file && File.exist?(_theme_file)
    self['conf'].update(self.properties_file2hash(_theme_file))
    _theme_res_file = "conf/theme-#{_name}.res.rb"
    if _theme_res_file && File.exist?(_theme_res_file)
      begin
        require _theme_res_file
      rescue Exception => e  
      end

    end
  end  
end

#local_dirObject



694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/a-commons.rb', line 694

def local_dir
  _local_dir = File.join(ENV["HOME"],'.'+self['applicationParams'].name)  if ENV["HOME"] 
  if _local_dir && !File.exist?(_local_dir)
    if FileTest.exist?(ENV["HOME"])
      Dir.mkdir(_local_dir)
      @first_run = true
    else
      msg = "Locad dir "+'"'+ENV["HOME"]+'"'+" must be writable!"
      Arcadia.dialog(self, 'type'=>'ok', 'title' => "(#{self['applicationParams'].name})", 'msg' => msg, 'level'=>'error')
      exit
    end
  end
  return _local_dir
end

#objects(_name) ⇒ Object



713
714
715
# File 'lib/a-commons.rb', line 713

def objects(_name)
  return @objs[_name]
end

#prepareObject



639
640
# File 'lib/a-commons.rb', line 639

def prepare
end

#publish(_name, _obj) ⇒ Object



642
643
644
645
646
647
648
649
# File 'lib/a-commons.rb', line 642

def publish(_name, _obj)
  @objs = Hash.new if !defined?(@objs)
  if @objs[_name] == nil
    @objs[_name] = _obj
  else
    raise("The name #{_name} already exist")
  end
end

#runObject



732
733
# File 'lib/a-commons.rb', line 732

def run
end

#sys_infoObject



618
619
620
# File 'lib/a-commons.rb', line 618

def sys_info
  "[Platform = #{RUBY_PLATFORM}] [Ruby version = #{RUBY_VERSION}]"
end