Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add travis support

  • Loading branch information...
commit 2ad1d5ad3e528e94c61b2a1a0d5d11b8b915a5c7 1 parent 345758a
Guten Ye authored
7 .travis.yml
View
@@ -0,0 +1,7 @@
+language: ruby
+rvm:
+ - 1.9.2
+ - 1.9.3
+ - jruby-19mode
+ - rbx-19mode
+script: bundle exec rspec spec
2  Gemfile
View
@@ -1,10 +1,10 @@
source :rubygems
+gem "pd"
gem "hike", "~>1.2.0"
group :development do
gem "rspec"
gem "watchr"
- gem "pd"
end
5 README.md
View
@@ -1,5 +1,4 @@
-Optimism, a configuration gem for Ruby
-====================================
+# Optimism, a configuration gem for Ruby [![Build Status](https://secure.travis-ci.org/GutenYe/optimism.png)](http://travis-ci.org/GutenYe/optimism)
| Homepage: | https://github.com/GutenYe/optimism
|----------------|--------------------------------------
@@ -8,7 +7,7 @@ Optimism, a configuration gem for Ruby
| Documentation: | http://rubydoc.info/gems/optimism/frames
| Issue Tracker: | https://github.com/GutenYe/optimism/issues
-Compatibility: MRI-1.9, not support MRI-1.8.
+Note: not support ruby1.8
Features
--------
77 lib/optimism.rb
View
@@ -5,7 +5,6 @@
%w(
semantics
- hash_method_fix
parser
require
).each { |n| require "optimism/#{n}" }
@@ -119,7 +118,6 @@ def convert(hash, options={})
undef_method *BUILTIN_METHODS
include Semantics
- include HashMethodFix
include RequireInstanceMethod
# the node name, a Symbol
@@ -412,10 +410,83 @@ def inspect(indent=" ")
alias to_s inspect
- def to_hash
+ def _to_hash
_data
end
+ # deep merge new data IN PLACE
+ #
+ # @params [Hash,Optimism,String] other
+ # @return [self]
+ def _merge!(other)
+ other = case other
+ when String
+ Optimism.new(other)
+ else
+ Optimism.convert(other)
+ end
+
+ other._each { |k, v|
+ if Optimism === self[k] and Optimism === other[k]
+ self[k]._merge!(other[k])
+ else
+ self[k] = other[k]
+ end
+ }
+
+ self
+ end
+
+ alias << _merge!
+
+ # deep merge new data
+ #
+ # @params [Hash,Optimism] obj
+ # @return [Optimism] new <#Optimism>
+ def _merge(other)
+ self._dup._merge!(other)
+ end
+
+ alias + _merge
+
+ # support path
+ #
+ # @example
+ #
+ # o = Optimism do |c|
+ # c.a = 1
+ # c.b.c = 2
+ # end
+ #
+ # o._get("not_exitts") #=> nil
+ # o._get("b.c") #=> 2
+ # o._get("c.d") #=> nil. path doesn't exist.
+ # o._get("a.b") #=> nil. path is wrong
+ #
+ # @param [String] key
+ # @return [Object] value
+ def _get(path)
+ value = self
+ if path =~ /\./
+ path.split(".").each { |k|
+ return nil unless Optimism === value # wrong path
+ value = value[k.to_sym]
+ return nil if value.nil? # path doesn't exist.
+ }
+ return value
+ else
+ return value[path.to_sym]
+ end
+ end
+
+ def _has_key?(key)
+ if String===key and !@options[:only_symbol_key]
+ key = key.to_sym
+ end
+
+ _child.has_key?(key)
+ end
+
private
# convert block to method.
87 lib/optimism/hash_method_fix.rb
View
@@ -1,87 +0,0 @@
-class Optimism
- #
- # make some hash methods works for <#Optimism>
- #
- module HashMethodFix
-
- # deep merge new data IN PLACE
- #
- # @params [Hash,Optimism] obj
- # @return [self]
- def _merge!(other)
- other = Optimism.convert(other)
- other._each { |k, v|
- if Optimism === self[k] and Optimism === other[k]
- self[k]._merge!(other[k])
- else
- self[k] = other[k]
- end
- }
-
- self
- end
-
- alias << _merge!
-
- # deep merge new data
- #
- # @params [Hash,Optimism] obj
- # @return [Optimism] new <#Optimism>
- def _merge other
- target = self._dup
- other = Optimism.convert(other)
-
- other._each { |k, v|
- if Optimism === target[k] and Optimism === other[k]
- target[k]._merge(other[k])
- else
- target[k] = other[k]
- end
- }
-
- target
- end
-
- alias + _merge
-
- # support path
- #
- # @example
- #
- # o = Optimism do |c|
- # c.a = 1
- # c.b.c = 2
- # end
- #
- # o._get("not_exitts") #=> nil
- # o._get("b.c") #=> 2
- # o._get("c.d") #=> nil. path doesn't exist.
- # o._get("a.b") #=> nil. path is wrong
- #
- # @param [String] key
- # @return [Object] value
- def _get(path)
- value = self
- if path =~ /\./
- path.split(".").each { |k|
- return nil unless Optimism === value # wrong path
- value = value[k.to_sym]
- return nil if value.nil? # path doesn't exist.
- }
- return value
- else
- return value[path.to_sym]
- end
- end
-
-
- def _has_key?(key)
- if String===key and !@options[:only_symbol_key]
- key = key.to_sym
- end
-
- _child.has_key?(key)
- end
-
- end
-end
2  lib/optimism/require.rb
View
@@ -67,7 +67,7 @@ def require_file(*paths)
end
begin
- new = Optimism(File.read(path))
+ new = Optimism.new(File.read(path))
rescue error
end
4 optimism.watchr
View
@@ -14,8 +14,8 @@ Signal.trap('QUIT') do
test "spec"
end
-def test path
- cmd = "rspec #{path}"
+def test(path)
+ cmd = "bundle exec rspec #{path}"
puts cmd
system cmd
end
97 spec/optimism_spec.rb
View
@@ -201,20 +201,22 @@ class Optimism
it "default is false" do
o = Optimism.new
o[:a] = 1
+
o[:a].should == 1
o["a"].should == 1
+
o["a"] = 2
o["a"].should == 2
o[:a].should == 2
- o._has_key?("a").should be_true
end
it "if true" do
o = Optimism.new(only_symbol_key: true)
o[:a] = 1
- o._has_key?("a").should be_false
+
o[:a].should == 1
o["a"].should_not == 1
+
o["a"] = 2
o["a"].should == 2
o[:a].should_not == 2
@@ -428,9 +430,9 @@ class Optimism
end
end
- describe "#to_hash" do
+ describe "#_to_hash" do
o = Optimism.convert({a: {b: {c: 1}}})
- o.to_hash.should == {a: Optimism.convert({b: {c: 1}})}
+ o._to_hash.should == {a: Optimism.convert({b: {c: 1}})}
end
describe "#_walk" do
@@ -542,4 +544,91 @@ class Optimism
ret.should == rc
end
end
+
+ describe "#_merge!" do
+ it "works" do
+ o = Optimism.new
+
+ o.b._merge! a: 1
+ o.should == Optimism[b: Optimism[a: 1]]
+
+ o.b._merge! Optimism[a: 2]
+ o.should == Optimism[b: Optimism[a: 2]]
+ end
+
+ #
+ # o.
+ # a.b = 1
+ # a.c = "foo"
+ # b.c = "x"
+ #
+ # o2
+ # a.b = 2
+ # a.d = "bar"
+ #
+ # o << o2
+ # #=>
+ # o.a.b = 2
+ # o.a.c = "foo"
+ # o.a.d = "bar"
+ # o.b.c = "x"
+ it "is deep merge" do
+ o = Optimism do
+ a.b = 1
+ a.c = "foo"
+ b.c = "x"
+ end
+
+ o2 = Optimism do
+ a.b = 2
+ a.d = "bar"
+ end
+
+ o._merge! o2
+ o.should == Optimism[a: Optimism[b: 2, c: "foo", d: "bar"], b: Optimism[c: "x"]]
+ end
+
+ it "(string)" do
+ o = Optimism.new
+ o._merge! <<-EOF
+ a = 1
+ EOF
+
+ o.should == Optimism.convert({a: 1})
+ end
+ end
+
+ describe "#_merge" do
+ it "works" do
+ o = Optimism.new
+ new = o._merge a: 1
+ o.should == o
+ new.should == Optimism[a: 1]
+ end
+ end
+
+ describe "#_get" do
+ before(:all) {
+ @o = Optimism do |c|
+ c.a = 1
+ c.b.c = 2
+ end
+ }
+
+ it "works" do
+ @o._get("a").should == 1
+ end
+
+ it "support path" do
+ @o._get("b.c").should == 2
+ end
+
+ it "return nil if path is not exists" do
+ @o._get("c.d").should == nil
+ end
+
+ it "return nil if path is wrong" do
+ @o._get("a.b").should == nil
+ end
+ end
end
3  spec/spec_helper.rb
View
@@ -1,3 +1,6 @@
+require "bundler/setup"
+require "stringio"
+require "pd"
require "optimism"
$spec_dir = File.expand_path("..", __FILE__)
Please sign in to comment.
Something went wrong with that request. Please try again.