Module: Configurable

Included in:
AgMultiEditor, Application, ArcadiaLocalization
Defined in:
lib/a-commons.rb

Constant Summary collapse

'>>>'
ADD_SYMBOL =
'+++'
LC_SYMBOL =
'LC@'
FONT_TYPE_SYMBOL =
'$$font:::'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



1004
1005
1006
1007
1008
1009
1010
# File 'lib/a-commons.rb', line 1004

def Configurable.clear_properties_group_cache(_group, _suff='conf')
  group_key="#{_suff}.#{_group}"
  if !@@conf_groups[group_key].nil?
    @@conf_groups[group_key].clear
    @@conf_groups[group_key] = nil
  end 
end

.properties_group(_group, _hash_source, _hash_suff = 'conf', _refresh = false) ⇒ Object



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/a-commons.rb', line 987

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

Instance Method Details

#hash2properties_file(_hash = nil, _file = nil) ⇒ Object

def one_line_format_as_hash(_line)

  ret = Hash.new
end

def hash_as_one_line_format(_name, _hash)
end


972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/a-commons.rb', line 972

def hash2properties_file(_hash=nil, _file=nil)
  if _hash && _file && FileTest.writable?(File.dirname(_file))
    f = File.new(_file, "w")
    begin
      if f
        _hash.keys.sort.each{|key|
          f.syswrite("#{key}=#{_hash[key]}\n")
        }
      end
    ensure
      f.close unless f.nil?
    end
  end
end

#make_locale_value(_value = '', _locale_hash = nil) ⇒ Object

Deprecated.


1084
1085
1086
1087
1088
1089
1090
# File 'lib/a-commons.rb', line 1084

def make_locale_value(_value='', _locale_hash=nil)
  value = _value.strip if _value
  if value && _locale_hash && value.length > 3 && value[0..2]==LC_SYMBOL
    value = _locale_hash[value[3..-1]] if _locale_hash[value[3..-1]]
  end
  value
end

#make_value(_self_context = self, _value = '') ⇒ Object



1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/a-commons.rb', line 1074

def make_value(_self_context=self, _value='')
  return nil if _value.nil?
  value = _value.strip
  if value[0..0]=='!'
    value=_self_context.instance_eval(value[1..-1])
  end
  value
end

#properties_file2hash(_property_file, _link_hash = nil) ⇒ Object



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'lib/a-commons.rb', line 903

def properties_file2hash(_property_file, _link_hash=nil)
  r_hash = Hash.new
  if _property_file &&  FileTest::exist?(_property_file)
    f = File::open(_property_file,'r')
    begin
      _lines = f.readlines
      _lines.each{|_line|
        _strip_line = _line.strip
        if (_strip_line.length > 0)&&(_strip_line[0,1]!='#')
          var_plat = _line.split('::')
          if var_plat.length > 1
            case var_plat[0]
            when 'WINDOWS'
              plat_enabled =  OS.windows?
            when 'MAC'
              plat_enabled =  OS.mac?
            when 'LINUX'
              plat_enabled =  OS.linux?
            when 'FREEBSD'
              plat_enabled =  OS.freebsd?
            when 'UNIX'
              plat_enabled =  OS.unix?
            when 'ARM'
              plat_enabled =  OS.arm?
            end
            if plat_enabled
              _line = var_plat[1]
              var_plat[2..-1].collect{|x| _line=_line+'::'+x} if var_plat.length > 2
            else
              _line = ''
            end
          end
          var_ruby_version = _line.split(':@:')
          if var_ruby_version.length > 1
            version = var_ruby_version[0]
            if (RUBY_VERSION[0..version.length-1]==version)
              _line = var_ruby_version[1]
            else
              _line = ''
            end
          end

          var = _line.split('=')
          if var.length > 1
            _value = var[1].strip
            var[2..-1].collect{|x| _value=_value+'='+x} if var.length > 2
            if _link_hash
              _value = resolve_value(_value, _link_hash)
            end
            r_hash[var[0].strip]=_value
          end
        end
      }
    ensure
      f.close unless f.nil?
    end
    return r_hash
  else
    puts 'warning--file does not exist', _property_file
  end
end

#resolve_locale_value(_value, _hash_source) ⇒ Object



1025
1026
1027
1028
1029
1030
# File 'lib/a-commons.rb', line 1025

def resolve_locale_value(_value, _hash_source)
  if _value.length > 3 && _value[0..2]==LC_SYMBOL
    _value=_hash_source[_value[3..-1]] if _hash_source[_value[3..-1]]
  end
  return _value
end


1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/a-commons.rb', line 1032

def resolve_properties_link(_hash_target, _hash_source)
  loop_level_max = 10
  #    _hash_adding = Hash.new
  _keys_to_extend = Array.new
  _hash_target.each{|k,value|
    loop_level = 0
    if value.length > 0
      v, vadd = value.split(ADD_SYMBOL)
    else
      v= value
    end
    #      p "value=#{value} class=#{value.class}"
    #      p "v=#{v} class=#{v.class}"
    #      p "vadd=#{vadd}"
    while loop_level < loop_level_max && v.length > 3 && v[0..2]==LINK_SYMBOL
      if k[-1..-1]=='.'
        _keys_to_extend << k
        break
      elsif _hash_source[v[3..-1]]
        v=_hash_source[v[3..-1]]
        v=v+vadd if vadd
      else
        break
      end
      loop_level = loop_level + 1
    end
    _hash_target[k]=v
    if loop_level == loop_level_max
      raise("Link loop found for property : #{k}")
    end
  }
  _keys_to_extend.each do |k|
    v=_hash_target[k]
    g=Configurable.properties_group(v[3..-1], _hash_target)
    g.each do |key,value|
      _hash_target["#{k[0..-2]}.#{key}"]=value if !_hash_target["#{k[0..-2]}.#{key}"]
    end
    _hash_target.delete(k)
    Configurable.clear_properties_group_cache(k[0..-2])
  end
end

#resolve_value(_value, _hash_source) ⇒ Object



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/a-commons.rb', line 1012

def resolve_value(_value, _hash_source)
  if _value.length > 0
    _v, _vadd = _value.split(ADD_SYMBOL)
  else
    _v = _value
  end
  if _v.length > 3 && _v[0..2]==LINK_SYMBOL
    _v=_hash_source[_v[3..-1]] if _hash_source[_v[3..-1]]
    _v=_v+_vadd if _vadd
  end
  return _v
end