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



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/a-commons.rb', line 717

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



740
741
742
# File 'lib/a-commons.rb', line 740

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

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



752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/a-commons.rb', line 752

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



778
779
780
# File 'lib/a-commons.rb', line 778

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

.del_conf_group(_conf_hash, _group) ⇒ Object



769
770
771
772
773
774
775
776
# File 'lib/a-commons.rb', line 769

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



736
737
738
# File 'lib/a-commons.rb', line 736

def Application.instance
  @@instance
end

.sys_infoObject



748
749
750
# File 'lib/a-commons.rb', line 748

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

Instance Method Details

#[](_name) ⇒ Object



892
893
894
895
896
897
898
899
# File 'lib/a-commons.rb', line 892

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

#[]=(_name, _value) ⇒ Object



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

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

#conf(_property) ⇒ Object



744
745
746
# File 'lib/a-commons.rb', line 744

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

#create(_name, _class) ⇒ Object



884
885
886
# File 'lib/a-commons.rb', line 884

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



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/a-commons.rb', line 824

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



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/a-commons.rb', line 851

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



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
# File 'lib/a-commons.rb', line 868

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



794
795
796
# File 'lib/a-commons.rb', line 794

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

#objects(_name) ⇒ Object



888
889
890
# File 'lib/a-commons.rb', line 888

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

#prepareObject



782
783
# File 'lib/a-commons.rb', line 782

def prepare
end

#publish(_name, _obj) ⇒ Object



785
786
787
788
789
790
791
792
# File 'lib/a-commons.rb', line 785

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



909
910
# File 'lib/a-commons.rb', line 909

def run
end

#update_local_configObject



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/a-commons.rb', line 798

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