Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improvements in the configuration

- Improvements in configuration class for load attributes from YAML
  file.
- Adding of extensions for core and standard libraries.
  • Loading branch information...
commit 14b458b94bb302ff23167d8ca54b64b08909f3e6 1 parent fb5e8fe
@hallison hallison authored
View
39 lib/git/webby.rb
@@ -1,11 +1,16 @@
-# See <b>Git::Webby</b> for documentation.
-module Git
+# Standard requirements
+require "yaml"
+
+# 3rd part requirements
+require "sinatra/base"
+require "json"
- # 3rd part requirements
- require "sinatra/base"
+# Internal requirements
+require "git/webby/extensions"
+require "git/webby/version"
- # Internal requirements
- require "git/webby/version"
+# See <b>Git::Webby</b> for documentation.
+module Git
# The main goal of the <b>Git::Webby</b> is implement the following useful
# features.
@@ -137,12 +142,26 @@ def fsize(str, scale = 1)
# *receive_pack* :: Like <tt>http.receivepack</tt> configuration.
class Config
- # Configuration for HTTP Backend variables
- attr_accessor :http_backend
+ DEFAULTS = {
+ :repository => {
+ :project_root => "/home/git",
+ :git_path => "/usr/bin/git",
+ :authenticate => false
+ },
+ :http_backend => {
+ :get_any_file => true,
+ :upload_pack => true,
+ :receive_pack => false
+ }
+ }
+
+ DEFAULTS.keys.map do |attribute|
+ attr_reader attribute
+ end
def initialize(attributes = {}) # :yields: config
- attributes.each do |key, value|
- self.send("#{key}=", value) if self.respond_to? key
+ DEFAULTS.update(attributes.symbolize_keys).map do |attrib, values|
+ self.instance_variable_set("@#{attrib}", values.to_struct)
end
yield self if block_given?
end
View
58 lib/git/webby/extensions.rb
@@ -0,0 +1,58 @@
+class Object
+ # Set instance variables by key and value only if object respond
+ # to access method for variable.
+ def instance_variables_set_from(hash)
+ hash.collect do |variable, value|
+ self.instance_variable_set("@#{variable}", value) if self.respond_to? variable
+ end
+ self
+ end
+
+end
+
+class Symbol
+
+ # Method for comparison between symbols.
+ def <=>(other)
+ self.to_s <=> other.to_s
+ end
+
+end
+
+class Hash
+
+ # Only symbolize all keys, including all key in sub-hashes.
+ def symbolize_keys
+ return self.clone if self.empty?
+ self.inject({}) do |h, (k, v)|
+ h[k.to_sym] = (v.kind_of? Hash) ? v.symbolize_keys : v
+ h
+ end
+ end
+
+ # Convert to Struct including all values that are Hash class.
+ def to_struct
+ keys = self.keys.sort
+ members = keys.map(&:to_sym)
+ Struct.new(*members).new(*keys.map do |key|
+ (self[key].kind_of? Hash) ? self[key].to_struct : self[key]
+ end) unless self.empty?
+ end
+
+end
+
+class String
+ def to_semver_h
+ tags = [:major, :minor, :patch, :status]
+ values = self.split(".").map do |key|
+ # Check pre-release status
+ if key.match(/^(\d{1,})([a-z]+[\d\w]{1,}.*)$/i)
+ [ $1.to_i, $2 ]
+ else
+ key.to_i
+ end
+ end.flatten
+ Hash[tags.zip(values)]
+ end
+end
+
View
24 lib/git/webby/version.rb
@@ -1,13 +1,25 @@
# The objective of this class is to implement various ideas proposed by the
# Semantic Versioning Specification (see reference[http://semver.org/]).
-module Git::Webby #:nodoc:
+module Git
- VERSION = "0.2.0"
- RELEASE = "2011-07-16"
- TIMESTAMP = "2011-07-05 12:32:36 -04:00"
+ module Webby #:nodoc:
+
+ VERSION = "0.2.0"
+ RELEASE = "2011-07-16"
+ TIMESTAMP = "2011-07-05 12:32:36 -04:00"
+
+ def self.info
+ "#{name} v#{VERSION} (#{RELEASE})"
+ end
+
+ def self.to_h
+ { :name => name,
+ :version => VERSION,
+ :release => RELEASE,
+ :timestamp => TIMESTAMP,
+ :info => info }
+ end
- def self.info
- "#{name} v#{VERSION} (#{RELEASE})"
end
end
View
37 test/config_test.rb
@@ -5,26 +5,37 @@
class ConfigTest < Test::Unit::TestCase
def setup
- @http_backend = {
- :project_root => "/var/git",
- :git_path => "/usr/bin/git",
- :get_any_file => true,
- :upload_pack => true,
- :receive_pack => false
+ @attributes = {
+ :repository => {
+ :project_root => "/var/git",
+ :git_path => "/usr/bin/git",
+ :authenticate => true
+ },
+ :http_backend => {
+ :get_any_file => true,
+ :upload_pack => true,
+ :receive_pack => false
+ }
}
- @config = Git::Webby::Config.new do |config|
- config.http_backend = @http_backend
- end
+ @config = Git::Webby::Config.new @attributes
end
- should "config for HTTP backend" do
- assert_hash_equal @http_backend, @config.http_backend
+ should "config by hash" do
+ @attributes.keys.each do |method|
+ @attributes[method].each do |key, value|
+ assert_equal value, @config.send(method).send(key)
+ end
+ end
end
should "load from YAML file" do
+ yaml = YAML.load_file(fixtures("config.yml"))
config = Git::Webby::Config.load_file(fixtures("config.yml"))
- assert_not_nil config.http_backend
- assert_hash_equal @http_backend, config.http_backend
+ yaml.keys.each do |method|
+ yaml[method].each do |key, value|
+ assert_equal value, @config.send(method).send(key)
+ end
+ end
end
end
View
15 test/fixtures/config.yml
@@ -1,7 +1,10 @@
-:http_backend:
- :project_root : /var/git
- :git_path : /usr/bin/git
- :get_any_file : true
- :upload_pack : true
- :receive_pack : false
+repository:
+ project_root: /var/git
+ git_path : /usr/bin/git
+ authenticate: true
+
+http_backend:
+ get_any_file: true
+ upload_pack : true
+ receive_pack: false
Please sign in to comment.
Something went wrong with that request. Please try again.