Class: Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash, source_path = nil) ⇒ Config

Returns a new instance of Config.



41
42
43
44
45
46
47
# File 'lib/ccios/config.rb', line 41

def initialize(config_hash, source_path = nil)
  @source_path = source_path
  validate config_hash
  @app = AppConfig.new config_hash["app"]
  @core = CoreConfig.new config_hash["core"]
  @data = DataConfig.new config_hash["data"]
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/ccios/config.rb', line 5

def app
  @app
end

#coreObject (readonly)

Returns the value of attribute core.



5
6
7
# File 'lib/ccios/config.rb', line 5

def core
  @core
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/ccios/config.rb', line 5

def data
  @data
end

Class Method Details

.defaultObject



37
38
39
# File 'lib/ccios/config.rb', line 37

def self.default
  self.new default_config_hash
end

.default_config_hashObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ccios/config.rb', line 17

def self.default_config_hash
  project = "*.xcodeproj"
  {
    "app" => {
      "project" => project,
      "presenter" => {"group" => "Classes/App"},
      "coordinator" => {"group" => "Classes/Coordinator"}
    },
    "core" => {
      "project" => project,
      "interactor" => {"group" => "Classes/Core/Interactor"},
      "repository" => {"group" => "Classes/Core/Data"}
    },
    "data" => {
      "project" => project,
      "repository" => {"group" => "Classes/Data"}
    }
  }
end

.parse(source_path) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/ccios/config.rb', line 7

def self.parse(source_path)
  if File.exist?(source_path)
    config = YAML.load_file(source_path)
    self.new config, source_path
  else
    puts "File #{source_path} does not exists. Using default config."
    self.default
  end
end

Instance Method Details

#validate(hash) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ccios/config.rb', line 49

def validate(hash)
  validate_path hash, "app.project"
  validate_path hash, "app.presenter.group"
  validate_path hash, "app.coordinator.group"

  validate_path hash, "core.project"
  validate_path hash, "core.interactor.group"
  validate_path hash, "core.repository.group"

  validate_path hash, "data.project"
  validate_path hash, "data.repository.group"
end

#validate_path(hash, path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ccios/config.rb', line 62

def validate_path(hash, path)
  components = path.split(".")
  keys = []
  components.each do |component|
    hash = hash[component]
    keys << component
    if hash.nil?
      message = "Key \"#{keys.join(".")}\" is missing"
      message += " in #{@source_path}" unless @source_path.nil?
      raise message
    end
  end
end