proper_properties 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0e73ae3fb455d335998b9ced5a388d03e923384
4
- data.tar.gz: 174890d5265606617411593f037279ef2bbc9c81
3
+ metadata.gz: ded64a75f7f400f69264e135dcc370f07b252e25
4
+ data.tar.gz: 984275d92225e8b14861b2a393dbffb908ddb5fc
5
5
  SHA512:
6
- metadata.gz: b402ae7a76d56183cf457ce71e97ce8f5f68dae4fd64afecb130abdfcf7aa493214a2bc65d56138f48991bd8193643bd2826953c1a22cf7d11134a7b3cc9213e
7
- data.tar.gz: 711cf90f60b7ee205cfe3f921155b3ae3b0fdfb8545b3e886d8a5e717ab2cb62d48a81105ec2afe6ad176ade4e9ce300f6de8b2bbc3b156cd05568b071e7c778
6
+ metadata.gz: 0be5d7ecbe24ebe704ebc8f76ece60cfe8cf467d0b4e1ac13af45859010fd9ef71ff6dad5c026f6cf243abee1d30d4a1e6a46f8984e5c97b658bdbd3862b3e7a
7
+ data.tar.gz: d84a9d989339e7ac198fcab17275da556566fd3e16835a0f58d48b618e213acd2c5b2035e31fe901b6121db33fdee74befc081beec3c79ea40e45a532e4c9eb6
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [properties_documentation]: http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)
2
+
1
3
  # ProperProperties
2
4
 
3
5
  [![Build Status](http://img.shields.io/travis/tnarik/proper_properties.svg)](https://travis-ci.org/tnarik/proper_properties)
@@ -6,7 +8,7 @@
6
8
  [![RubyGems](http://img.shields.io/gem/v/proper_properties.svg)](http://rubygems.org/gems/proper_properties)
7
9
  [![Gemnasium](http://img.shields.io/gemnasium/tnarik/proper_properties.svg)](https://gemnasium.com/tnarik/proper_properties)
8
10
 
9
- A ruby library to read and write [Java properties files](http://en.wikipedia.org/wiki/.properties), which format is better describe [in the Java documentation](http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader\)).
11
+ A ruby library to read and write [Java properties files](http://en.wikipedia.org/wiki/.properties), which format is better describe [in the Java documentation][properties_documentation].
10
12
 
11
13
  ## Installation
12
14
 
@@ -23,6 +25,7 @@ You can load a valid Java properties file from the file system using a path:
23
25
  ```ruby
24
26
  properties = ProperProperties.load("path/to/my.properties")
25
27
  properties[:foo] # => "bar"
28
+ properties['foo'] # => "bar"
26
29
  ```
27
30
 
28
31
  If have already the content of the properties file at hand than parse the content as:
@@ -30,14 +33,15 @@ If have already the content of the properties file at hand than parse the conten
30
33
  ```ruby
31
34
  properties = ProperProperties.load("foo=bar")
32
35
  properties[:foo] # => "bar"
36
+ properties['foo'] # => "bar"
33
37
  ```
34
38
 
35
39
  ## Writing files
36
40
 
37
- You can write any Hash-like structure as a properties file:
41
+ You can write any Hash-like structure (with symbol or string keys) as a properties file:
38
42
 
39
43
  ```ruby
40
- hash = {:foo => "bar"}
44
+ hash = {:foo => "bar", "foobar" => "barfoo"}
41
45
  ProperProperties.write(hash, "path/to/my.properties")
42
46
  ```
43
47
 
@@ -93,11 +97,11 @@ In the opposite direction line breaks will be correctly escaped but the generato
93
97
  ## Author
94
98
 
95
99
  - Tnarik Innael (@tnarik)
96
- - Jonas Thiel (@jonasthiel) : [original project](https://github.com/jnbt/java-properties) from commit [1f2c4b008d69d0eae1084b1adfdea814e2b29899]
100
+ - Jonas Thiel (@jonasthiel) : [original project](https://github.com/jnbt/java-properties) upto [1f2c4b008d69d0eae1084b1adfdea814e2b29899](https://github.com/tnarik/proper_properties/commit/1f2c4b008d69d0eae1084b1adfdea814e2b29899)
97
101
 
98
102
  ## References
99
103
 
100
- For a proper description about the properties file format have a look at the [Java Plattform documentation](http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader\)).
104
+ For a proper description about the properties file format have a look at the [Java Plattform documentation][properties_documentation].
101
105
 
102
106
  ## License
103
107
 
@@ -1,6 +1,64 @@
1
1
  module ProperProperties
2
2
  # Simple representation of a properties file content
3
- # by a simple ruby hash object
3
+ # by an extension of a Hash object a la ActiveSupport::HashWithIndifferentAccess, with underlying symbol keys
4
4
  class Properties < Hash
5
+
6
+ # Assigns a new value to the hash:
7
+ #
8
+ # hash = ProperProperties::Properties.new
9
+ # hash[:key] = 'value'
10
+ #
11
+ # This value can be later fetched using either +:key+ or +'key'+.
12
+ def []=(key, value)
13
+ super(convert_key(key), value)
14
+ end
15
+
16
+ # Checks the hash for a key matching the argument passed in:
17
+ #
18
+ # hash = ProperProperties::Properties.new
19
+ # hash['key'] = 'value'
20
+ # hash.key?(:key) # => true
21
+ # hash.key?('key') # => true
22
+ def key?(key)
23
+ super(convert_key(key))
24
+ end
25
+
26
+ # Same as <tt>Hash#fetch</tt> where the key passed as argument can be
27
+ # either a string or a symbol:
28
+ #
29
+ # counters = ProperProperties::Properties.new
30
+ # counters[:foo] = 1
31
+ #
32
+ # counters.fetch('foo') # => 1
33
+ # counters.fetch(:bar, 0) # => 0
34
+ # counters.fetch(:bar) {|key| 0} # => 0
35
+ # counters.fetch(:zoo) # => KeyError: key not found: "zoo"
36
+ def fetch(key, *extras)
37
+ super(convert_key(key), *extras)
38
+ end
39
+
40
+ # Returns an array of the values at the specified indices:
41
+ #
42
+ # hash = ProperProperties::Properties.new
43
+ # hash[:a] = 'x'
44
+ # hash[:b] = 'y'
45
+ # hash.values_at('a', 'b') # => ["x", "y"]
46
+ def values_at(*indices)
47
+ indices.collect {|key| self[convert_key(key)]}
48
+ end
49
+
50
+ # Removes the specified key from the hash.
51
+ def delete(key)
52
+ super(convert_key(key))
53
+ end
54
+
55
+ def [](key)
56
+ self.fetch(key)
57
+ end
58
+
59
+ protected
60
+ def convert_key(key)
61
+ key.kind_of?(String) ? key.to_sym : key
62
+ end
5
63
  end
6
64
  end
@@ -1,3 +1,3 @@
1
1
  module ProperProperties
2
- VERSION = "0.0.1"
3
- end
2
+ VERSION = "0.0.2"
3
+ end
@@ -4,29 +4,71 @@ require 'tempfile'
4
4
  describe ProperProperties do
5
5
  subject{ ProperProperties }
6
6
 
7
- it "parses from string" do
7
+ it "parses from string to symbol hash" do
8
8
  subject.parse("item1=item1").must_equal({:item1 => "item1"})
9
9
  end
10
10
 
11
11
  it "generates from hash" do
12
12
  subject.generate({:item1 => "item1"}).must_equal("item1=item1")
13
+ subject.generate({"item2" => "item2"}).must_equal("item2=item2")
13
14
  end
14
15
 
15
- it "loads from file" do
16
+ it "loads from file and create a hash" do
16
17
  with_temp_file do |file|
17
18
  file << "item1=item1"
18
19
  file.flush
19
20
 
20
21
  subject.load(file.path).must_equal({:item1 => "item1"})
22
+
23
+ end
24
+ end
25
+
26
+ it "loads from file and allows symbol/string access" do
27
+ with_temp_file do |file|
28
+ file << "item1=item1"
29
+ file.flush
30
+
31
+ properties = subject.load(file.path)
32
+ properties[:item1].must_equal("item1")
33
+ properties['item1'].must_equal("item1")
34
+ end
35
+ end
36
+
37
+ it "loads from file and allows manipulation" do
38
+ with_temp_file do |file|
39
+ file << "item1=item1\n"
40
+ file << "item2=item2\n"
41
+ file << "item3=item3\n"
42
+ file << "item4=item4"
43
+ file.flush
44
+
45
+ properties = subject.load(file.path)
46
+
47
+ properties.values_at(:item1, "item2", 'item3').must_equal(["item1", "item2", "item3"])
48
+
49
+
50
+ properties[:item2].must_equal("item2")
51
+ properties.delete(:item2).must_equal("item2")
52
+ proc{ properties[:item2] }.must_raise(KeyError)
53
+
54
+ properties['item3'].must_equal("item3")
55
+ properties.delete('item3').must_equal("item3")
56
+ proc{ properties['item3'] }.must_raise(KeyError)
57
+
58
+ properties.key?(:item4).must_equal(true)
59
+ properties.key?('item4').must_equal(true)
60
+
61
+ properties.key?(:item5).must_equal(false)
62
+ properties.key?('item5').must_equal(false)
21
63
  end
22
64
  end
23
65
 
24
66
  it "writes to file" do
25
67
  with_temp_file do |file|
26
- subject.write({:item1 => "item1"}, file.path)
68
+ subject.write({:item1 => "item1", "item2" => "item2"}, file.path)
27
69
 
28
70
  file.rewind
29
- file.read.must_equal "item1=item1"
71
+ file.read.must_equal "item1=item1\nitem2=item2"
30
72
  end
31
73
  end
32
74
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proper_properties
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tnarik Innael
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-26 00:00:00.000000000 Z
11
+ date: 2014-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake