Class: Texas::Build::Config

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

Overview

This class holds the config information for a build.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Config

Returns a new instance of Config.



6
7
8
# File 'lib/texas/build/config.rb', line 6

def initialize(hash)
  @hash = hash.stringify_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



66
67
68
# File 'lib/texas/build/config.rb', line 66

def method_missing(m, *args, &block)
  self[m] || super
end

Class Method Details

.create(hash, merge_key = nil) ⇒ Object

Returns a Config object.

Example:

# .texasrc

document:
  title: "My Document (DRAFT!)"
final:
  document:
    title: "My Document"

config = Config.create YAML.load_file(".texasrc"), :final
# => #<Texas::Build::Config ...>

config.document.title
# => "My Document"


111
112
113
114
115
# File 'lib/texas/build/config.rb', line 111

def self.create(hash, merge_key = nil)
  config = self.new hash
  config.merge! merge_key if merge_key
  config
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
# File 'lib/texas/build/config.rb', line 10

def [](key)
  @hash[key.to_s]
end

#documentObject

Returns an object for the document config key.

Example:

# .texasrc

document:
  title: "My Document"

config = Config.new YAML.load_file(".texasrc") 
# => #<Texas::Build::Config ...>

config.document.title
# => "My Document"


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

def document
  @document ||= OpenStruct.new self[:document]
end

#merge!(key) ⇒ Object

Returns an object for the document config key.

Example:

# .texasrc

document:
  title: "My Document (DRAFT!)"
final:
  document:
    title: "My Document"

config = Config.new YAML.load_file(".texasrc") 
# => #<Texas::Build::Config ...>

config.document.title
# => "My Document (DRAFT!)"

config.merge! :final
# => #<Texas::Build::Config ...>

config.document.title
# => "My Document"


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

def merge!(key)
  @document = nil
  merge_hash = @hash[key.to_s]
  if merge_hash
    @hash.deep_merge! merge_hash.stringify_keys
    self
  else
    raise "Trying to merge config with none existing key #{key.inspect}"
  end
end

#script(key) ⇒ Object

Returns an element from the script config key

Example:

# .texasrc

script:
  before: "touch some_file"
document:
  title: "My Document"

config = Config.new YAML.load_file(".texasrc") 
# => #<Texas::Build::Config ...>

config.script(:before)
# => "touch some_file"

config.script(:after)
# => nil


89
90
91
92
# File 'lib/texas/build/config.rb', line 89

def script(key)
  hash = self[:script] || {}
  hash[key.to_s]
end