mostash 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.21"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.9.2.2)
11
+ rcov (0.9.11)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.21)
26
+ jeweler (~> 1.6.4)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Joel Friedman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = mostash
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to mostash
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Joel Friedman. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile CHANGED
@@ -1,25 +1,49 @@
1
- require 'rubygems'
2
- require 'spec/rake/spectask'
1
+ # encoding: utf-8
3
2
 
3
+ require 'rubygems'
4
+ require 'bundler'
4
5
  begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gemspec|
7
- gemspec.name = "mostash"
8
- gemspec.summary = "A combo of OpenStruct and a ruby hash"
9
- gemspec.description = "You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct"
10
- gemspec.email = "asher.friedman@gmail.com"
11
- #gemspec.homepage = "http://github.com/technicalpickles/the-perfect-gem"
12
- gemspec.authors = ["Joel Friedman"]
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mostash"
18
+ gem.homepage = "http://github.com/joelash/mostash"
19
+ gem.license = "MIT"
20
+ gem.summary = "A combo of OpenStruct and a ruby hash"
21
+ gem.description = "You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct"
22
+ gem.email = "asher.friedman@gmail.com"
23
+ gem.authors = ["Joel Friedman"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
13
27
 
14
- gemspec.add_development_dependency "rspec"
15
- end
16
- rescue LoadError
17
- puts "Jeweler not available. Install it with: gem install jeweler"
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
18
32
  end
19
33
 
20
- desc "run all specs"
21
- Spec::Rake::SpecTask.new( "spec" ) do |t|
22
- t.spec_files = FileList["spec/**/*_spec.rb"]
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
23
37
  end
24
38
 
25
39
  task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "mostash #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
@@ -3,8 +3,11 @@ require 'ostruct'
3
3
  require File.join(File.dirname(__FILE__), "mostash", "mostash")
4
4
  MoStash = Mostash
5
5
 
6
- def dbg( msg )
6
+ def dbg(obj, msg=nil)
7
7
  file, line, method_raw = caller[0].split('/').last.split(':')
8
8
  method = method_raw.match(/^in `(.+)'/)[1]
9
- puts "#{method} (#{file}##{line}): #{msg}"
9
+ #puts "#{method} (#{file}##{line}): #{msg}"
10
+ msg += " " if msg
11
+ puts "#{msg}#{obj.inspect}"
12
+ obj
10
13
  end
@@ -1,75 +1,60 @@
1
- class Mostash < OpenStruct
2
- def initialize(init={})
3
- super({})
4
- @initial_hash = init
5
- __init__ init
6
- end
7
-
8
- def new_ostruct_member(name)
9
- name = name.to_sym
10
- unless self.respond_to?(name)
1
+ class Mostash < Hash
2
+ alias_method :orig_init, :initialize
3
+ def initialize(init={}, &def_proc)
4
+ if init.is_a? Hash
5
+ __init__ init
6
+ self.send(:default=, init.default) if init.default
7
+ self.send(:default_proc=, init.default_proc) if init.default_proc
8
+ else
11
9
  super
12
- eigenclass.class_eval do
13
- remove_method("#{name}") if self.respond_to?(name)
14
- define_method("#{name}=") { |x| modifiable[name] = __adjusted_value__ x }
15
- end
16
10
  end
17
- name
11
+ self.send(:default_proc=, def_proc) if block_given?
18
12
  end
19
13
 
14
+ def clone
15
+ Mostash.new(self)
16
+ end
17
+ alias_method :dup, :clone
18
+
20
19
  def method_missing(method_name, *args, &block)
21
20
  #dbg "#{method_name} was sent #{args.inspect}, and block #{block.inspect}"
22
- if @table.respond_to? method_name
23
- @table.send method_name, *args, &block
24
- elsif __is_setter__( method_name )
25
- super method_name, __adjusted_value__( args.first )
21
+ if __is_setter__( method_name )
22
+ method_name = method_name.to_s.gsub! '=', ''
23
+ self[method_name] = args.first
26
24
  else
27
- super || @initial_hash.send(:[], method_name, *args, &block)
25
+ self[method_name]
28
26
  end
29
27
  end
30
28
 
29
+ alias_method :__set__, :[]=
31
30
  def []=(key, value)
32
- self.send "#{key.to_s}=", __adjusted_value__(value)
31
+ __set__ key.to_sym, __adjusted_value__(value)
33
32
  end
34
33
 
34
+ alias_method :__get__, :[]
35
35
  def [](key)
36
- self.send "#{key.to_s}"
36
+ __get__ key.to_sym
37
37
  end
38
38
 
39
- def merge(new_hash)
40
- new_mo = @table.merge( new_hash ) do |key, oldval, newval|
41
- if oldval.class == Mostash
42
- oldval.merge newval
43
- else
44
- newval
45
- end
39
+ def merge!(from={})
40
+ from.each_pair do |key, value|
41
+ new_value = case value
42
+ when Hash then Mostash.new(self[key] || {}).merge(value)
43
+ else value
44
+ end
45
+ self[key] = new_value
46
46
  end
47
- Mostash.new( new_mo )
48
- end
49
-
50
- #TODO: HACK!!!!!
51
- def merge!(new_hash)
52
- @table = self.merge( new_hash ).instance_variable_get( '@table' )
53
47
  self
54
48
  end
55
49
 
56
- def to_hash
57
- hash = {}
58
- @table.each_pair do |key, value|
59
- hash[key] = if value.class == Mostash
60
- value.to_hash
61
- else
62
- value
63
- end
64
- end
65
- hash
50
+ def merge(from={})
51
+ self.clone.merge! from
66
52
  end
67
53
 
68
54
  private
69
-
70
55
  def __init__(hash)
71
56
  hash.each_pair do |key, value|
72
- self.send "#{key.to_s}=", __adjusted_value__( value )
57
+ self[key] = __adjusted_value__( value )
73
58
  end
74
59
  end
75
60
 
@@ -78,7 +63,7 @@ class Mostash < OpenStruct
78
63
  when Hash then Mostash.new( value )
79
64
  when Array then value.map{ |v| __adjusted_value__( v ) }
80
65
  else value
81
- end
66
+ end#.tap { |ret| dbg "calc adj value for #{value.inspect} = #{ret}" }
82
67
  end
83
68
 
84
69
  def __is_setter__(method_name)
@@ -17,6 +17,18 @@ describe 'MoStash as Hash' do
17
17
  end
18
18
 
19
19
  context 'initialization' do
20
+ it "should support initializtion from default value" do
21
+ mo = Mostash.new 123
22
+
23
+ mo.doesnt_exist.should == 123
24
+ end
25
+
26
+ it "should support initialzation from default proc" do
27
+ mo = Mostash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
28
+
29
+ mo.doesnt_exist.should == "Go Fish: doesnt_exist"
30
+ end
31
+
20
32
  it "should support initialzation from hash with default value" do
21
33
  mo = Mostash.new(Hash.new(123))
22
34
 
@@ -84,6 +96,15 @@ describe 'MoStash as Hash' do
84
96
  end
85
97
 
86
98
  context 'merge' do
99
+ it "should not change without bang" do
100
+ mo = MoStash.new :foo => 'bar', :hello => 'world'
101
+
102
+ new_mo = mo.merge( :hello => 'tester' )
103
+
104
+ new_mo.hello.should == 'tester'
105
+ mo.hello.should == 'world'
106
+ end
107
+
87
108
  it "should support merge when single level" do
88
109
  mo = MoStash.new :foo => 'bar', :hello => 'world'
89
110
 
@@ -60,11 +60,4 @@ describe MoStash do
60
60
  mo.foo[1].oh.should == 'hai'
61
61
  end
62
62
 
63
- it "should create method when new method called" do
64
- mo = Mostash.new
65
- mo.foo = "bar"
66
-
67
- mo.methods.should include(:foo)
68
- end
69
-
70
63
  end
@@ -1,4 +1,11 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "lib", "mostash")
2
2
 
3
- require 'rubygems'
4
- require 'spec'
3
+ require 'rspec'
4
+
5
+ # Requires supporting files with custom matchers and macros, etc,
6
+ # in ./support/ and its subdirectories.
7
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+
9
+ RSpec.configure do |config|
10
+
11
+ end
metadata CHANGED
@@ -1,45 +1,74 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mostash
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 5
9
- version: 0.0.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Joel Friedman
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-04-21 00:00:00 -05:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rspec
16
+ requirement: &70178437964520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70178437964520
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70178437964040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.21
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70178437964040
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70178437963560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
22
45
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
46
+ version_requirements: *70178437963560
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &70178437958980 !ruby/object:Gem::Requirement
24
50
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
31
55
  type: :development
32
- version_requirements: *id001
33
- description: You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct
56
+ prerelease: false
57
+ version_requirements: *70178437958980
58
+ description: You can treat an object as either a hash or as an OpenStruct. In additon
59
+ to this you can create them nested, unlike OpenStruct
34
60
  email: asher.friedman@gmail.com
35
61
  executables: []
36
-
37
62
  extensions: []
38
-
39
- extra_rdoc_files:
40
- - README.markdown
41
- files:
42
- - README.markdown
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .rspec
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.rdoc
43
72
  - Rakefile
44
73
  - VERSION
45
74
  - lib/mostash.rb
@@ -47,39 +76,32 @@ files:
47
76
  - spec/as_hash_spec.rb
48
77
  - spec/mostash_spec.rb
49
78
  - spec/spec_helper.rb
50
- has_rdoc: true
51
- homepage:
52
- licenses: []
53
-
79
+ homepage: http://github.com/joelash/mostash
80
+ licenses:
81
+ - MIT
54
82
  post_install_message:
55
83
  rdoc_options: []
56
-
57
- require_paths:
84
+ require_paths:
58
85
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
86
+ required_ruby_version: !ruby/object:Gem::Requirement
60
87
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- segments:
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
65
93
  - 0
66
- version: "0"
67
- required_rubygems_version: !ruby/object:Gem::Requirement
94
+ hash: 3141743245854038340
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
96
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
- version: "0"
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
75
101
  requirements: []
76
-
77
102
  rubyforge_project:
78
- rubygems_version: 1.3.7
103
+ rubygems_version: 1.8.6
79
104
  signing_key:
80
105
  specification_version: 3
81
106
  summary: A combo of OpenStruct and a ruby hash
82
- test_files:
83
- - spec/as_hash_spec.rb
84
- - spec/mostash_spec.rb
85
- - spec/spec_helper.rb
107
+ test_files: []
File without changes