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.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ccios/config.rb', line 46

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"]
  if config_hash["templates"].nil?
    @templates = TemplatesConfig.new Config.default_templates_hash
  else
    @templates = TemplatesConfig.new config_hash["templates"]
  end
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

#templatesObject (readonly)

Returns the value of attribute templates.



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

def templates
  @templates
end

Class Method Details

.defaultObject



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

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
36
# 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"}
    },
    "templates" => self.default_templates_hash
  }
end

.default_templates_hashObject



42
43
44
# File 'lib/ccios/config.rb', line 42

def self.default_templates_hash
  { "path" => File.join(File.dirname(__FILE__), "templates") }
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 exist. Using default config."
    self.default
  end
end

Instance Method Details

#validate(hash) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ccios/config.rb', line 59

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ccios/config.rb', line 72

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