Class: Xmvc::Config

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

Defined Under Namespace

Classes: ConfigBlock

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

:api: private



103
104
105
# File 'lib/xmvc/config.rb', line 103

def configuration
  @configuration
end

Class Method Details

.[](key) ⇒ Object

Retrieve the value of a config entry.

Parameters

key<Object>

The key to retrieve the parameter for.

Returns

Object

The value of the configuration parameter.

:api: public



29
30
31
# File 'lib/xmvc/config.rb', line 29

def [](key)
  (@configuration ||= setup)[key]
end

.[]=(key, val) ⇒ Object

Set the value of a config entry.

Parameters

key<Object>

The key to set the parameter for.

val<Object>

The value of the parameter.

:api: public



40
41
42
# File 'lib/xmvc/config.rb', line 40

def []=(key, val)
  (@configuration ||= setup)[key] = val
end

.add(key, item) ⇒ Object

write a newly generated controller, model or view to environment.json Unfortunately, we cannot JSON.parse the entire config/environment.json, since it contains comments and such. Have to RegExp, pick-out the requested key, and JSON.parse the returned chunk. Is this Regexp satisfactory?? Could probably be made better.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/xmvc/config.rb', line 82

def add (key, item)
  key = key.to_s
  @configuration[key] = [] unless @configuration[key] && configuration[key].kind_of?(Array)
  
  unless key == "views"
    @configuration[key] << item
  else
    if controller = @configuration[key].find {|i| i[item[:package]]}
      controller[item[:package]] << item[:filename]
    else
      @configuration[key] << {
        item[:package] => [
          item[:filename]
        ]
      }.to_hash
    end
  end
  save
end

.configure(&block) ⇒ Object

Set configuration parameters from a code block, where each method evaluates to a config parameter.

Parameters

&block

Configuration parameter block.

Examples

# Set environment and log level.
Xmvc::Config.configure do
  environment "development"
end

Returns

nil

:api: public



121
122
123
124
# File 'lib/xmvc/config.rb', line 121

def configure(&block)
  ConfigBlock.new(self, &block) if block_given?
  nil
end

.defaultsObject



4
5
6
7
8
9
10
11
12
# File 'lib/xmvc/config.rb', line 4

def defaults
  @defaults ||= {
    "environment"        => "development",
    "javascript_loader"  => true,
    "name"               => "xmvc",
    "javascripts"        => [],
    "stylesheets"       => []
  }
end

.renderObject



67
68
69
70
71
72
73
74
# File 'lib/xmvc/config.rb', line 67

def render
  File.open("config/environment.json", "w") {|file|
    file << @configuration.to_json
  }
  #File.open("#{@configuration[:environment]}.json", "w") {|file|
  #  file << @configuration.to_json
  #}
end

.setup(settings = {}) ⇒ Object

Sets up the configuration by storing the given settings.

Parameters

settings<Hash>

Configuration settings to use. These are merged with the defaults.

Returns

The configuration as a hash.

:api: private



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

def setup(settings = {})
  
  # Merge new settings with any existing configuration settings
  settings = @configuration.merge(settings) unless @configuration.nil?

  # Merge new settings with default settings
  config = defaults.merge(settings)
  config = config.merge(load(config[:environment]))
    
  dev_mode = config[:environment] == "development"  
  @configuration = config.to_mash
end

.to_yamlObject



126
127
128
# File 'lib/xmvc/config.rb', line 126

def to_yaml
  @configuration.to_yaml
end

.use {|@configuration| ... } ⇒ Object

Yields:



14
15
16
17
18
# File 'lib/xmvc/config.rb', line 14

def use
  @configuration ||= {}
  yield @configuration
  nil
end