Permalink
Browse files

continuing to work

  • Loading branch information...
1 parent ac01d77 commit 384c009795cc2e376fccf982dffaf6411426ac21 Craig Wickesser committed Mar 13, 2012
Showing with 90 additions and 25 deletions.
  1. +2 −0 .rspec
  2. +32 −25 lib/mongoid_indifferent_access.rb
  3. +40 −0 spec/mongoid-indifferent-access/indifferent_access_spec.rb
  4. +16 −0 spec/spec_helper.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
57 lib/mongoid_indifferent_access.rb
@@ -1,35 +1,42 @@
-module Mongoid::Extensions::Hash::IndifferentAccess
- VERSION = "0.0.1"
+module Mongoid
+ module Extensions
+ module Hash
+ module IndifferentAccess
+ VERSION = "0.0.1"
- def self.included(klass)
- klass.class_eval do
- # @override Mongoid::Field
- def self.field(name, options={})
- field = super(name, options)
+ def self.included(klass)
+ klass.class_eval do
+ # @override Mongoid::Field
+ def self.field(name, options={})
+ field = super(name, options)
- field_type =
- if options.fetch(:type, nil) == Hash
- getter_name = name.is_a?(Symbol) ? name : name.intern
- setter_name = "#{getter_name}=".intern
+ # why oh why does sometimes Hash != Hash? something with my environment maybe?
+ # tried ruby 1.9.2-p290 and jruby 1.6.6 (1.9 mode), so we also try checking the class name
+ if options[:type] == Hash || options[:type].name =~ /Hash/
+ getter_name = name.is_a?(Symbol) ? name : name.intern
+ setter_name = "#{getter_name}=".intern
- define_method(getter_name) do
- val = super()
- unless val.nil? || val.is_a?(HashWithIndifferentAccess)
- wrapped_hash = val.with_indifferent_access
- self.send(setter_name, wrapped_hash)
- val = wrapped_hash
- end
- val
- end
+ define_method(getter_name) do
+ val = super()
+ unless val.nil? || val.is_a?(HashWithIndifferentAccess)
+ wrapped_hash = val.with_indifferent_access
+ self.send(setter_name, wrapped_hash)
+ val = wrapped_hash
+ end
+ val
+ end
- define_method(setter_name) do |hash|
- unless hash.nil? || hash.is_a?(HashWithIndifferentAccess)
- hash = hash.with_indifferent_access
+ define_method(setter_name) do |hash|
+ unless hash.nil? || hash.is_a?(HashWithIndifferentAccess)
+ hash = hash.with_indifferent_access
+ end
+ super(hash)
+ end
+ end
+ field
end
- super(hash)
end
end
- field
end
end
end
View
40 spec/mongoid-indifferent-access/indifferent_access_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+require 'mongoid_indifferent_access'
+require 'mongoid'
+
+class MockSuper
+ attr_accessor :config
+ def self.field(name, options={})
+ nil
+ end
+end
+
+class Guitar < MockSuper
+
+
+ # mock Mongoid::Document.field
+ def self.field(name, options={})
+ nil
+ end
+
+ include Mongoid::Extensions::Hash::IndifferentAccess
+
+ field :config, :type => Hash
+end
+
+module Mongoid::Extensions::Hash
+
+ describe IndifferentAccess do
+ subject {
+ g = Guitar.new
+ g.config = {:value => 123}
+ g
+ }
+
+ it "returns a value for a given key as a String" do
+ subject.config["value"].should eq(123)
+ end
+
+ end
+
+end
View
16 spec/spec_helper.rb
@@ -0,0 +1,16 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+# RSpec.configure do |config|
+# config.treat_symbols_as_metadata_keys_with_true_values = true
+# config.run_all_when_everything_filtered = true
+# config.filter_run :focus
+# end
+
+require 'rspec/autorun'
+ROOT = File.expand_path('../..', __FILE__)
+Dir[File.join(ROOT, "spec/support/**/*.rb")].each {|f| require f}
+$LOAD_PATH.unshift(File.expand_path('lib', ROOT))

0 comments on commit 384c009

Please sign in to comment.