Class: Reflection::Config

Inherits:
Object
Defined in:
lib/reflection/config.rb

Attribute Summary

Method Summary

Attribute Details

- (Object) command

Returns the value of attribute command



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

def command
  @command
end

- (Object) directory

Returns the value of attribute directory



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

def directory
  @directory
end

- (Object) rails_environment

Returns the value of attribute rails_environment



15
16
17
# File 'lib/reflection/config.rb', line 15

def rails_environment
  @rails_environment
end

- (Object) rails_root

Returns the value of attribute rails_root



14
15
16
# File 'lib/reflection/config.rb', line 14

def rails_root
  @rails_root
end

- (Object) repository

Returns the value of attribute repository



13
14
15
# File 'lib/reflection/config.rb', line 13

def repository
  @repository
end

- (Object) store_configuration_path

Returns the value of attribute store_configuration_path



16
17
18
# File 'lib/reflection/config.rb', line 16

def store_configuration_path
  @store_configuration_path
end

Method Details

+ (Object) parse(args = [])



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reflection/config.rb', line 18

def self.parse(args = [])
  config = Config.new
  case
    when !args.empty? && File.exists?(args.first)
      config.read!(args.first)
    when !args.empty?
      config.parse_command_line_options(args)
      config.write(config.store_configuration_path) if config.store_configuration_path
    else
      config.read!(File.join(Dir.pwd, 'reflection.yml'))
  end
  config
end

- (Object) from_hash(hash)



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

def from_hash(hash)
  self.command = hash[:command]
  self.directory = hash[:directory]
  self.repository = hash[:repository]
  self.rails_root = hash[:rails_root]
  self.rails_environment = hash[:rails_environment]
end

- (Object) parse_command_line_options(args)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/reflection/config.rb', line 72

def parse_command_line_options(args)
  opt_parser = OptionParser.new do |opts|
    opts. =   "Usage: reflection --COMMAND --repository=GIT_REPO --directory=PATH\n"
    opts. <<  " -or-\n"
    opts. <<  "Usage: reflection /path/to/reflection-config-file.yml"
    
    opts.separator " "
    opts.separator "On the server side:"

    opts.on("-s", "--stash", "Store your assets and/or a database dump in a git-repository.") do |stash|
      self.command = :stash
    end

    opts.separator "On the client side:"
    opts.on("-a", "--apply", "Apply assets and/or a database dump loaded from a git-repository.") do |apply|
      self.command = :apply
    end

    opts.separator " "
    opts.separator "Required options for both:"
    opts.on("-r", "--repository GIT_URL", "A Git repository(url) to be used as storage") do |repository|
      self.repository = repository
    end

    opts.on("-d", "--directory PATH", "Path to your asset directory") do |directory|
      self.directory = directory
    end

    opts.separator " "
    opts.separator "Additional options for both:"

    opts.on("--rails [RAILS_ROOT]", "Enable dumping/applying of a Rails managed MySQL database") do |path|
      self.rails_root = path || nil
    end        

    opts.on("--rails-env [ENV]", "Rails environment to instrument") do |environment|
      self.rails_environment = environment || nil
    end        
    
    opts.on("--write [FILE]", "Create a configuration FILE from the current commandline options") do |config_file_path|
      self.store_configuration_path = config_file_path if config_file_path
    end
  end

  opt_parser.parse!(args)
end

- (Object) read!(path)



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

def read!(path)
  begin
    if File.exist?(path)
      options = YAML.load_file(path)
      raise(Reflection::ConfigArgumentError, "Config file is invalid.") unless options.is_a?(Hash)
      self.from_hash(options)
    end
  rescue => e
    Reflection::Support.exit_with_error("Parsing of config file '#{path}' failed: #{e.message}")
  end
end

- (Object) to_hash



32
33
34
35
36
37
38
39
40
# File 'lib/reflection/config.rb', line 32

def to_hash
  { 
    :command => self.command, 
    :repository => self.repository, 
    :directory => self.directory,
    :rails_root => self.rails_root,
    :rails_environment => self.rails_environment
  }
end

- (Object) write(path)



50
51
52
53
54
55
56
57
58
# File 'lib/reflection/config.rb', line 50

def write(path)
  begin
    io = File.open(path, 'w')
    YAML.dump(self.to_hash, io)
    io.close
  rescue => e
    Reflection::Support.exit_with_error("Writing of config file to '#{path}' failed: #{e.message}")
  end
end