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

Constant Summary

Constants included from Configurable

Configurable::ADD_SYMBOL, Configurable::FONT_TYPE_SYMBOL, Configurable::LINK_SYMBOL

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

clear_properties_group_cache, #make_value, #properties_file2hash, properties_group, #resolve_properties_link, #resolve_value

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



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
# File 'lib/a-commons.rb', line 942

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



965
966
967
# File 'lib/a-commons.rb', line 965

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

.del_conf(_k) ⇒ Object



1004
1005
1006
# File 'lib/a-commons.rb', line 1004

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

.del_conf_group(_conf_hash, _group) ⇒ Object

def Application.conf_group(_group, _suff = ‘conf’)

  group_key="#{_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


995
996
997
998
999
1000
1001
1002
# File 'lib/a-commons.rb', line 995

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



961
962
963
# File 'lib/a-commons.rb', line 961

def Application.instance
  @@instance
end

.sys_infoObject



973
974
975
# File 'lib/a-commons.rb', line 973

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

Instance Method Details

#[](_name) ⇒ Object



1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/a-commons.rb', line 1118

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

#[]=(_name, _value) ⇒ Object



1127
1128
1129
1130
1131
1132
# File 'lib/a-commons.rb', line 1127

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

#conf(_property) ⇒ Object



969
970
971
# File 'lib/a-commons.rb', line 969

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

#create(_name, _class) ⇒ Object



1110
1111
1112
# File 'lib/a-commons.rb', line 1110

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



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'lib/a-commons.rb', line 1050

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



1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
# File 'lib/a-commons.rb', line 1077

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



1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
# File 'lib/a-commons.rb', line 1094

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



1020
1021
1022
# File 'lib/a-commons.rb', line 1020

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

#objects(_name) ⇒ Object



1114
1115
1116
# File 'lib/a-commons.rb', line 1114

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

#prepareObject



1008
1009
# File 'lib/a-commons.rb', line 1008

def prepare
end

#publish(_name, _obj) ⇒ Object



1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/a-commons.rb', line 1011

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



1135
1136
# File 'lib/a-commons.rb', line 1135

def run
end

#update_local_configObject



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/a-commons.rb', line 1024

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