mattfawcett-ruby-reads-php 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 0
@@ -0,0 +1,33 @@
1
+ class RubyReadsPHP
2
+ attr_accessor :constants
3
+
4
+ def initialize
5
+ self.constants = {}
6
+ end
7
+
8
+ def self.read(file_name)
9
+ rrp = self.new
10
+ rrp.parse(file_name)
11
+ end
12
+
13
+
14
+ def parse(file_name)
15
+ file_name << '.php' unless file_name.match(/.php/)
16
+ file = File.new(file_name, "r")
17
+ data = parse_file(file)
18
+ self
19
+ end
20
+
21
+ def parse_file(file)
22
+ constant_regex = /define\((\s)*('|")((\w|\s)+)('|")(\s)*,(\s)*('|")?((\w|\s)*)('|")(\s)*\);/
23
+ while (line = file.gets)
24
+ if line.match(constant_regex)
25
+ contant_key = line[constant_regex, 3]
26
+ constant_value = line[constant_regex, 9]
27
+ constants[contant_key] = constant_value
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+ end
@@ -0,0 +1,6 @@
1
+ <?php
2
+
3
+ define('MY_CONSTANT', 'my value');
4
+ define('MY_CONSTANT_NUMBER_2', 'my value 2');
5
+
6
+ ?>
@@ -0,0 +1,5 @@
1
+ <?php
2
+
3
+ define('MY_CONSTANT', 'my value');
4
+
5
+ ?>
@@ -0,0 +1,5 @@
1
+ <?php
2
+
3
+ define("MY_CONSTANT", "my value");
4
+
5
+ ?>
@@ -0,0 +1,5 @@
1
+ <?php
2
+
3
+ define( 'MY_CONSTANT' , 'my value' );
4
+
5
+ ?>
@@ -0,0 +1,35 @@
1
+ require(File.dirname(__FILE__) + "/../lib/ruby_reads_php")
2
+ describe "Ruby reads PHP" do
3
+
4
+ describe "reading PHP constants" do
5
+ it "should read simple constant" do
6
+ rrp = RubyReadsPHP.read(File.dirname(__FILE__) + "/fixtures/simple_constant.php")
7
+ rrp.constants['MY_CONSTANT'].should eql('my value')
8
+ end
9
+
10
+ it "should read simple constant with double quotes" do
11
+ rrp = RubyReadsPHP.read(File.dirname(__FILE__) + "/fixtures/simple_constant_with_double_quotes.php")
12
+ rrp.constants['MY_CONSTANT'].should eql('my value')
13
+ end
14
+
15
+ it "should read simple constant with extra spacing" do
16
+ rrp = RubyReadsPHP.read(File.dirname(__FILE__) + "/fixtures/simple_constant_with_extra_spacing.php")
17
+ rrp.constants['MY_CONSTANT'].should eql('my value')
18
+ end
19
+
20
+ it "should read multiple constants" do
21
+ rrp = RubyReadsPHP.read(File.dirname(__FILE__) + "/fixtures/multiple_constants.php")
22
+ rrp.constants['MY_CONSTANT'].should eql('my value')
23
+ rrp.constants['MY_CONSTANT_NUMBER_2'].should eql('my value 2')
24
+ end
25
+ end
26
+
27
+
28
+ describe "opening files" do
29
+ it "should automatically append .php extension" do
30
+ rrp = RubyReadsPHP.read(File.dirname(__FILE__) + "/fixtures/simple_constant")
31
+ rrp.constants['MY_CONSTANT'].should eql('my value')
32
+ end
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattfawcett-ruby-reads-php
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Fawcett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This gem allows you to read a PHP file from within your ruby code to use the constants.
17
+ email: mail@matthewfawcett.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/ruby_reads_php.rb
27
+ - test/ruby_reads_php.spec
28
+ - test/fixtures
29
+ - test/fixtures/multiple_constants.php
30
+ - test/fixtures/simple_constant.php
31
+ - test/fixtures/simple_constant_with_double_quotes.php
32
+ - test/fixtures/simple_constant_with_extra_spacing.php
33
+ has_rdoc: true
34
+ homepage: http://github.com/mattfawcett/ruby-reads-php
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --inline-source
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: A ruby Gem that reads PHP configuration files
60
+ test_files: []
61
+