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

#make_value, #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



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/a-commons.rb', line 723

def initialize(_ap=ApplicationParams.new)
  @@instance = self
  eval('$'+_ap.name+'=self') # set $arcadia to this instance
  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
  # read in the settings'
  publish('conf', properties_file2hash(self['applicationParams'].config_file)) if self['applicationParams'].config_file
  publish('origin_conf', Hash.new.update(self['conf'])) if self['conf']
  publish('local_conf', Hash.new)
  publish('conf_without_local', Hash.new.update(self['conf'])) if self['conf']
  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



746
747
748
# File 'lib/a-commons.rb', line 746

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

.conf_group(_group, _suff = 'conf') ⇒ Object



758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/a-commons.rb', line 758

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

.del_conf(_k) ⇒ Object



784
785
786
# File 'lib/a-commons.rb', line 784

def Application.del_conf(_k)
  @@instance['conf'].delete(_k)
end

.del_conf_group(_conf_hash, _group) ⇒ Object



775
776
777
778
779
780
781
782
# File 'lib/a-commons.rb', line 775

def Application.del_conf_group(_conf_hash, _group)
    glen=_group.length
    _conf_hash.keys.sort.each{|k|
      if k[0..glen] == "#{_group}."
        _conf_hash.delete(k)
      end
    }
end

.instanceObject



742
743
744
# File 'lib/a-commons.rb', line 742

def Application.instance
  @@instance
end

.sys_infoObject



754
755
756
# File 'lib/a-commons.rb', line 754

def Application.sys_info
  "[Platform = #{RUBY_PLATFORM}]\n[Ruby version = #{RUBY_VERSION}]"
end

Instance Method Details

#[](_name) ⇒ Object



898
899
900
901
902
903
904
905
# File 'lib/a-commons.rb', line 898

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

#[]=(_name, _value) ⇒ Object



907
908
909
910
911
912
# File 'lib/a-commons.rb', line 907

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

#conf(_property) ⇒ Object



750
751
752
# File 'lib/a-commons.rb', line 750

def conf(_property)
 self['conf'][_property]
end

#create(_name, _class) ⇒ Object



890
891
892
# File 'lib/a-commons.rb', line 890

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



830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/a-commons.rb', line 830

def load_local_config(_create_if_not_exist=true)
    if FileTest.exist?(local_file_config)
      self['local_conf']= self.properties_file2hash(local_file_config)
      self['conf'].update(self['local_conf'])
    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



857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/a-commons.rb', line 857

def load_theme(_name=nil)
  _theme_file = "conf/theme-#{_name}.conf" if !_name.nil?
  if _theme_file && File.exist?(_theme_file)
    self['conf_theme'] = self.properties_file2hash(_theme_file)
    self['conf'].update(self['conf_theme'])
    self['conf_without_local'].update(self['conf_theme'])
    _theme_res_file = "#{Dir.pwd}/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



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'lib/a-commons.rb', line 874

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

#local_file_configObject



800
801
802
# File 'lib/a-commons.rb', line 800

def local_file_config
  File.join(local_dir, File.basename(self['applicationParams'].config_file))
end

#objects(_name) ⇒ Object



894
895
896
# File 'lib/a-commons.rb', line 894

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

#prepareObject



788
789
# File 'lib/a-commons.rb', line 788

def prepare
end

#publish(_name, _obj) ⇒ Object



791
792
793
794
795
796
797
798
# File 'lib/a-commons.rb', line 791

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



915
916
# File 'lib/a-commons.rb', line 915

def run
end

#update_local_configObject



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/a-commons.rb', line 804

def update_local_config
    # local_dir is ~/arcadia
    if FileTest.exist?(local_file_config)
      if FileTest.writable?(local_dir)
        f = File.new(local_file_config, "w")
        begin
          if f
            properties = self['conf']
            if properties
              properties.keys.sort.each{|key|
                if self['conf_without_local'][key] == self['conf'][key] || (self['origin_conf'][key] && self['origin_conf'][key].include?('>>>')) # || self['local_conf'][key].nil?
                  f.syswrite("# #{key}=#{self['origin_conf'][key]}\n") # write it as a comment since it isn't a real change
                elsif self['conf'][key]
                  f.syswrite("#{key}=#{self['conf'][key]}\n")
                end
              }
            end
          end
        ensure
          f.close unless f.nil?
        end
      end
    end
end