zfinifile 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bones'
3
+ rescue LoadError
4
+ abort '### please install the "bones" gem ###'
5
+ end
6
+
7
+ ensure_in_path 'lib'
8
+ require 'zfinifile'
9
+
10
+ task :default => 'test:run'
11
+ task 'gem:release' => 'test:run'
12
+
13
+ Bones {
14
+ name 'zfinifile'
15
+ summary 'INI file reader that supports the inheritance available in Zend Framework'
16
+ authors 'Brett Mack'
17
+ email 'brett.mack@bauermedia.co.uk'
18
+ url 'http://rubygems.org/gems/zfinifile'
19
+ version ZFIniFile::VERSION
20
+
21
+ use_gmail
22
+ depend_on 'bones-git', :development => true
23
+ }
@@ -0,0 +1,131 @@
1
+ #
2
+ # This class is used to load and parse the ini file. Much has been borrowed from the existing gem
3
+ # 'inifile' (https://github.com/TwP/inifile) which I would suggest using if you don't need section
4
+ # inheritance.
5
+ #
6
+
7
+ #encoding: UTF-8
8
+
9
+ class ZFIniFile
10
+
11
+ # :stopdoc:
12
+ class Error < StandardError; end
13
+ VERSION = '1.0.0'
14
+ # :startdoc:
15
+
16
+ #
17
+ # call-seq:
18
+ # ZFIniFile.load( filename )
19
+ # ZFIniFile.load( filename, options )
20
+ #
21
+ # Open the given _filename_ and load the contents of the INI file.
22
+ # The following _options_ can be passed to this method:
23
+ #
24
+ # :comment => ';' The line comment character
25
+ # :parameter => '=' The parameter / value seperator
26
+ # :encoding => nil The encoding to use when reading a file (RUBY 1.9)
27
+ # :inheritance => ':' The character within a section that denotes inheritance
28
+ #
29
+ def self.load( filename, opts = {} )
30
+ new(filename, opts)
31
+ end
32
+
33
+ #
34
+ # call-seq:
35
+ # ZFIniFile.new( filename )
36
+ # ZFIniFile.new( filename, options )
37
+ #
38
+ # Open the given _filename_ and load the contents of the INI file.
39
+ # The following _options_ can be passed to this method:
40
+ #
41
+ # :comment => ';' The line comment character
42
+ # :parameter => '=' The parameter / value seperator
43
+ # :encoding => nil The encoding to use when reading a file (RUBY 1.9)
44
+ # :inheritance => ':' The character within a section that denotes inheritance
45
+ #
46
+ def initialize( filename, opts = {} )
47
+ @fn = filename
48
+ @comment = opts.fetch(:comment, ';')
49
+ @param = opts.fetch(:parameter, '=')
50
+ @encoding = opts.fetch(:encoding, nil)
51
+ @inheritance = opts.fetch(:inheritance, ':')
52
+ @ini = Hash.new { |h,k| h[k] = Hash.new }
53
+ @inheritsfrom = Hash.new { |h,k| h[k] = [] }
54
+
55
+ @rgxp_comment = %r/\A\s*\z|\A\s*[#{@comment}]/
56
+ @rgxp_section = %r/\A\s*\[([^\]]+)\]/
57
+ @rgxp_param = %r/[^\\]#{@param}/
58
+
59
+ parse
60
+ end
61
+
62
+ #
63
+ # call-seq:
64
+ # get_section( section )
65
+ #
66
+ # Return the _section_ of the inifile as a Hash.
67
+ #
68
+ def get_section( section )
69
+ return unless @ini[section]
70
+ @ini[section]
71
+ end
72
+
73
+ private
74
+
75
+ # Parse tge ini file contents.
76
+ #
77
+ def parse
78
+ raise Error, "Unable to open file: #{@fn}" unless File.file?(@fn)
79
+
80
+ @_current_section = nil
81
+ @_current_value = nil
82
+ @_current_param = nil
83
+
84
+ fd = (RUBY_VERSION >= '1.9' && @encoding) ?
85
+ File.open(@fn, 'r', :encoding => @encoding) :
86
+ File.open(@fn, 'r')
87
+
88
+ while line = fd.gets
89
+ line = line.chomp
90
+
91
+ if line =~ @rgxp_comment
92
+ next
93
+ end
94
+
95
+ if line =~ @rgxp_section
96
+ section = $1.split("#{@inheritance}")
97
+ @_current_section = @ini[section[0].strip]
98
+ if section.length == 2
99
+ @inheritsfrom[section[1].strip].unshift(section[0].strip)
100
+ end
101
+ next
102
+ end
103
+
104
+ #So it't not a comment and it's not a section. Process the line as param = value
105
+ parse_property line
106
+ end
107
+
108
+ @inheritsfrom.each { |key, val|
109
+ next unless @ini[key]
110
+ @_inherit_base = @ini[key]
111
+ val.each do |d|
112
+ next unless @ini[d]
113
+ @ini[d] = @_inherit_base.merge(@ini[d])
114
+ end
115
+ }
116
+ ensure
117
+ fd.close if defined? fd and fd
118
+ @_current_section = nil
119
+ @_current_param = nil
120
+ @_current_value = nil
121
+ end
122
+
123
+ # Parse the ini file line as param = value
124
+ #
125
+ def parse_property( line )
126
+ return unless @_current_section
127
+ param, value = line.split("#{@param}")
128
+ @_current_section[param.strip] = value.strip.gsub(/["']/, '')
129
+ end
130
+
131
+ end
@@ -0,0 +1,99 @@
1
+ [production]
2
+ phpSettings.display_startup_errors = 0
3
+ phpSettings.display_errors = 0
4
+ includePaths.library = APPLICATION_PATH "/../library"
5
+ bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
6
+ bootstrap.class = "Bootstrap"
7
+ appnamespace = "Application"
8
+
9
+ resources.frontController.params.displayExceptions = 0
10
+
11
+ resources.frontController.defaultModule = "v1"
12
+ resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
13
+ resources.frontController.moduleControllerDirectoryName = "controllers"
14
+
15
+ resources.modules[] = ""
16
+
17
+ ; ------------------------------------------------------------------------------
18
+ ; Autoload Namespaces
19
+ ; ------------------------------------------------------------------------------
20
+ autoloaderNamespaces[] = "Bisna"
21
+ autoloaderNamespaces[] = "Symfony"
22
+ autoloaderNamespaces[] = "Doctrine"
23
+
24
+ ;; added for Doctrine2 Integration
25
+ pluginPaths.Bisna\Application\Resource\ = "Bisna/Application/Resource"
26
+
27
+ ; ------------------------------------------------------------------------------
28
+ ; Doctrine Cache Configuration
29
+ ; ------------------------------------------------------------------------------
30
+
31
+ ; Points to default cache instance to be used. Optional is only one cache is defined
32
+ resources.doctrine.cache.defaultCacheInstance = default
33
+
34
+ ; Cache Instance configuration for "default" cache
35
+ resources.doctrine.cache.instances.default.adapterClass = "Doctrine\Common\Cache\MemcacheCache"
36
+ resources.doctrine.cache.instances.default.namespace = "TestService_"
37
+ resources.doctrine.cache.instances.default.options.servers.0.host = localhost
38
+ resources.doctrine.cache.instances.default.options.servers.0.port = 11211
39
+
40
+ ; ------------------------------------------------------------------------------
41
+ ; Doctrine DBAL Configuration
42
+ ; ------------------------------------------------------------------------------
43
+
44
+ ; Points to default connection to be used. Optional if only one connection is defined
45
+ resources.doctrine.dbal.defaultConnection = default
46
+
47
+ ; Database configuration
48
+ ;resources.doctrine.dbal.connections.default.parameters.wrapperClass = ""
49
+ resources.doctrine.dbal.connections.default.parameters.driver = "pdo_mysql"
50
+ resources.doctrine.dbal.connections.default.parameters.dbname = "[DATABASE_NAME]"
51
+ resources.doctrine.dbal.connections.default.parameters.host = "localhost"
52
+ resources.doctrine.dbal.connections.default.parameters.port = 3306
53
+ resources.doctrine.dbal.connections.default.parameters.user = "[DATABASE_USERNAME]"
54
+ resources.doctrine.dbal.connections.default.parameters.password = "[DATABASE_PASSWORD]"
55
+
56
+ ; ------------------------------------------------------------------------------
57
+ ; Doctrine ORM Configuration
58
+ ; ------------------------------------------------------------------------------
59
+
60
+ ; Points to default EntityManager to be used. Optional if only one EntityManager is defined
61
+ resources.doctrine.orm.defaultEntityManager = default
62
+
63
+ ; EntityManager configuration for "default" manager
64
+ resources.doctrine.orm.entityManagers.default.connection = default
65
+ resources.doctrine.orm.entityManagers.default.proxy.autoGenerateClasses = true
66
+ resources.doctrine.orm.entityManagers.default.proxy.dir = APPLICATION_PATH "/../library/Bauer/Entity/Proxy"
67
+ resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationFiles[] = APPLICATION_PATH "/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"
68
+ resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
69
+ resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/../library/Bauer/Entity"
70
+ resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
71
+ resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default
72
+
73
+ [staging : production]
74
+ app.url.base = "Should be overwritten in testing"
75
+ app.url.tester = "Should still exist in testing"
76
+
77
+ [testing : staging]
78
+ phpSettings.display_startup_errors = 1
79
+ phpSettings.display_errors = 1
80
+
81
+ ; Database configuration
82
+ ;resources.doctrine.dbal.connections.default.parameters.driver = "pdo_sqlite"
83
+ ;resources.doctrine.dbal.connections.default.parameters.path = APPLICATION_PATH "/../tests/data/test.db"
84
+ resources.doctrine.dbal.connections.default.parameters.dbname = "test_testservice_testing"
85
+ resources.doctrine.dbal.connections.default.parameters.user = "root"
86
+ resources.doctrine.dbal.connections.default.parameters.password = "rootpwd"
87
+
88
+ app.url.base = 'http://testservice.testing.mydomain.co.uk'
89
+
90
+ [development : production]
91
+ phpSettings.display_startup_errors = 1
92
+ phpSettings.display_errors = 1
93
+
94
+ ; Database configuration
95
+ resources.doctrine.dbal.connections.default.parameters.dbname = "test_testservice_development"
96
+ resources.doctrine.dbal.connections.default.parameters.user = "root"
97
+ resources.doctrine.dbal.connections.default.parameters.password = "devpwd"
98
+
99
+ app.url.base = 'http://testservice.development.mydomain.co.uk'
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ libpath = File.expand_path '../../lib', __FILE__
3
+ require File.join(libpath, 'zfinifile')
4
+
5
+
6
+ class ZFIniFileTest < Test::Unit::TestCase
7
+
8
+ def test_no_file_given
9
+ assert_raise ArgumentError do
10
+ ZFIniFile.load()
11
+ end
12
+ end
13
+
14
+ def test_ini_file_not_exists
15
+ assert_raise ZFIniFile::Error do
16
+ ZFIniFile.load('filenotexists.ini')
17
+ end
18
+ end
19
+
20
+ def test_instances
21
+ inifile = ZFIniFile.load('test/data/application.ini')
22
+ assert_instance_of ZFIniFile, inifile
23
+ assert_instance_of Hash, inifile.get_section( 'testing' )
24
+ assert_instance_of Hash, inifile.get_section( 'nosuchsection' )
25
+ end
26
+
27
+ def test_inheritance
28
+ inifile = ZFIniFile.load('test/data/application.ini')
29
+ testing = inifile.get_section('testing')
30
+ staging = inifile.get_section('staging')
31
+ production = inifile.get_section('production')
32
+ # Make sure dbname has been overwritten in each section
33
+ testkey = "resources.doctrine.dbal.connections.default.parameters.dbname"
34
+ assert_nil [ testing[testkey], production[testkey] ].uniq!
35
+
36
+ # Make sure the tester val from staging still exists in testing
37
+ assert_equal "Should still exist in testing", testing["app.url.tester"]
38
+
39
+ # Make sure the url.base val from staging was overwritten in testing
40
+ assert_not_equal staging["app.url.base"], testing["app.url.base"]
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zfinifile
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brett Mack
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A ruby gem to pass ini files with inheritance such as those used by the
15
+ Zend Framework
16
+ email: brett.mack@bauermedia.co.uk
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/zfinifile.rb
22
+ - Rakefile
23
+ - test/test_zfinifile.rb
24
+ - test/data/application.ini
25
+ homepage:
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.17
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: ZFIniFile
49
+ test_files: []
50
+ has_rdoc: