Class: Reflection::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command ⇒ Object

Returns the value of attribute command.



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

def command
  @command
end

#directory ⇒ Object

Returns the value of attribute directory.



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

def directory
  @directory
end

#force ⇒ Object

Returns the value of attribute force.



18
19
20
# File 'lib/reflection/config.rb', line 18

def force
  @force
end

#rails_environment ⇒ Object

Returns the value of attribute rails_environment.



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

def rails_environment
  @rails_environment
end

#rails_root ⇒ Object

Returns the value of attribute rails_root.



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

def rails_root
  @rails_root
end

#repository ⇒ Object

Returns the value of attribute repository.



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

def repository
  @repository
end

#store_configuration_path ⇒ Object

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

#verbose ⇒ Object

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Class Method Details

.parse(args = []) ⇒ Object



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

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

Instance Method Details

#from_hash(hash) ⇒ Object



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

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]
  self.verbose = Reflection.verbose = hash[:verbose]
  self.force = hash[:force]
end

#parse_command_line_options(args) ⇒ Object



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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/reflection/config.rb', line 78

def parse_command_line_options(args)
  opt_parser = OptionParser.new do |opts|
    opts.banner =   "Usage: reflection --COMMAND --repository=GIT_REPO --directory=PATH\n"
    opts.banner <<  " -or-\n"
    opts.banner <<  "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

    opts.on("--force", "Hide tedious warnings") do
      self.force = true
    end

    opts.on("-v", "--verbose", "Include debug information in output") do
      self.verbose = true
      Reflection.verbose = true
    end

    opts.on("--version", "Show version") do
      self.command = :show_version
    end
  end

  opt_parser.parse!(args)
end

#read!(path) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/reflection/config.rb', line 66

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

#to_hash ⇒ Object



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

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

#write(path) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/reflection/config.rb', line 56

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