Class: RJGit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb

Overview

Wraps ‘org.eclipse.jgit.lib.Config’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
# File 'lib/config.rb', line 11

def initialize(path)
  @path = path
  @jconfig = org.eclipse.jgit.lib.Config.new
end

Instance Attribute Details

#jconfigObject (readonly)

Returns the value of attribute jconfig.



7
8
9
# File 'lib/config.rb', line 7

def jconfig
  @jconfig
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/config.rb', line 7

def path
  @path
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
31
# File 'lib/config.rb', line 28

def [](key)
  section, subsection = key.split
  build_settings_hash(section, subsection)
end

#add_setting(name, value, section, subsection = "") ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/config.rb', line 81

def add_setting(name, value, section, subsection = "")
  case value
    when Integer then @jconfig.set_int(section, subsection, name, value)
    when TrueClass then @jconfig.set_boolean(section, subsection, name, value)
    when FalseClass then @jconfig.set_boolean(section, subsection, name, value)
    when String then @jconfig.set_string(section, subsection, name, value)
    else nil
  end
end

#build_settings_hash(section, subsection) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/config.rb', line 33

def build_settings_hash(section, subsection)
  names = names(section, subsection)
  settings = {}
  names.each do |name|
    value = @jconfig.get_string(section, subsection, name)
    if is_num?(value)
      value = value.to_i 
    elsif is_bool?(value)
      value = to_boolean(value)
    end
    settings[name] = value
  end
  settings.empty? ? nil : settings
end

#is_bool?(str) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/config.rb', line 56

def is_bool?(str)
  str = str.strip
  str == 'true' || str == 'false'
end

#is_num?(str) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/config.rb', line 48

def is_num?(str)
  begin
    !!Integer(str)
  rescue ArgumentError, TypeError
    false
  end
end

#loadObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/config.rb', line 16

def load
  return self if @loaded
  begin
    @jconfig.from_text(IO.read(@path))
    @loaded = true
  rescue => exception
    @loaded = false
    raise IOException.new(exception.message)
  end
  return self
end

#loaded?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/config.rb', line 65

def loaded?
  @loaded
end

#names(section, subsection) ⇒ Object



91
92
93
# File 'lib/config.rb', line 91

def names(section, subsection)
  @jconfig.get_names(section, subsection).to_array
end

#sectionsObject



69
70
71
# File 'lib/config.rb', line 69

def sections
  @jconfig.get_sections.to_array
end

#subsections(section) ⇒ Object



73
74
75
# File 'lib/config.rb', line 73

def subsections(section)
  @jconfig.get_subsections(section).to_array
end

#to_boolean(str) ⇒ Object



61
62
63
# File 'lib/config.rb', line 61

def to_boolean(str)
  str == "true"
end

#to_sObject



77
78
79
# File 'lib/config.rb', line 77

def to_s
  @jconfig.to_text
end