mongoid-indifferent-access 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-indifferent-access (0.0.1)
5
+ mongoid
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.2)
11
+ activesupport (= 3.2.2)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.2)
14
+ i18n (~> 0.6)
15
+ multi_json (~> 1.0)
16
+ bson (1.6.1)
17
+ bson (1.6.1-java)
18
+ builder (3.0.0)
19
+ diff-lcs (1.1.3)
20
+ i18n (0.6.0)
21
+ mongo (1.6.1)
22
+ bson (~> 1.6.1)
23
+ mongoid (2.4.6)
24
+ activemodel (~> 3.1)
25
+ mongo (~> 1.3)
26
+ tzinfo (~> 0.3.22)
27
+ multi_json (1.1.0)
28
+ rake (0.9.2.2)
29
+ rspec (2.8.0)
30
+ rspec-core (~> 2.8.0)
31
+ rspec-expectations (~> 2.8.0)
32
+ rspec-mocks (~> 2.8.0)
33
+ rspec-core (2.8.0)
34
+ rspec-expectations (2.8.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.8.0)
37
+ tzinfo (0.3.32)
38
+
39
+ PLATFORMS
40
+ java
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ mongoid-indifferent-access!
45
+ rake
46
+ rspec
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ Mongoid Indifferent Access [![Build Status](https://secure.travis-ci.org/mindscratch/mongoid-indifferent-access.png?branch=master)](http://travis-ci.org/mindscratch/mongoid-indifferent-access)
2
+ ==========================
3
+
4
+ A Mongoid Hash extension enabling "indifferent access" so you can access keys using Strings or Symbols.
5
+
6
+ [BSON](http://bsonspec.org/) stores document keys as strings, so Hash keys are strings when going into MongoDB and coming out. This extension allows
7
+ a developer to not worry as to whether or not the keys are Strings or Symbols. Instead, by relying on ActiveSupport all Hashes can be
8
+ accessed using whichever approach suites the developer.
9
+
10
+ Ruby/JRuby
11
+ ----------
12
+ Thanks to [travis-ci](http://travis-ci.org) this gem is tested against Ruby 1.9.2, 1.9.3 and JRuby 1.6.7.
13
+
14
+ Usage
15
+ -----
16
+
17
+ #### Before
18
+
19
+ Notice the `nil` values when the Hash values are fetched using Symbols.
20
+
21
+ ````ruby
22
+ class QueryResult
23
+ include Mongoid::Document
24
+
25
+ field :data, type: Hash
26
+ end
27
+
28
+ # create a QueryResult
29
+ QueryResult.create(data: {name: "google", value: 1.32})
30
+
31
+ # fetch it back from the database
32
+ result = QueryResult.first
33
+
34
+ # check its values
35
+ puts result.data["name"] # => "google"
36
+ puts result.data[:value] # => nil
37
+ puts result.data[:name] # => nil
38
+ ````
39
+
40
+ #### After
41
+
42
+ Notice the values in the Hash can be accessed using Strings or Symbols.
43
+
44
+ ````ruby
45
+ class QueryResult
46
+ include Mongoid::Document
47
+ include Mongoid::Extensions::Hash::IndifferentAccess
48
+
49
+ field :data, type: Hash
50
+ end
51
+
52
+ # create a QueryResult
53
+ QueryResult.create(data: {name: "google", value: 1.32})
54
+
55
+ # fetch it back from the database
56
+ result = QueryResult.first
57
+
58
+ # check its values
59
+ puts result.data["name"] # => "google"
60
+ puts result.data[:value] # => 1.32
61
+ puts result.data[:name] # => "google"
62
+ puts result.data["value"] # => 1.32
63
+ ````
data/Rakefile ADDED
@@ -0,0 +1,145 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def lib_name
16
+ @lib_name ||= name.gsub /-/, '_'
17
+ end
18
+
19
+ def version
20
+ line = File.read("lib/#{lib_name}.rb")[/^\s*VERSION\s*=\s*.*/]
21
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
22
+ end
23
+
24
+ def date
25
+ Date.today.to_s
26
+ end
27
+
28
+ def rubyforge_project
29
+ name
30
+ end
31
+
32
+ def gemspec_file
33
+ "#{name}.gemspec"
34
+ end
35
+
36
+ def gem_file
37
+ "#{name}-#{version}.gem"
38
+ end
39
+
40
+ def replace_header(head, header_name)
41
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
42
+ end
43
+
44
+ #############################################################################
45
+ #
46
+ # Standard tasks
47
+ #
48
+ #############################################################################
49
+
50
+ require 'rspec'
51
+ require 'rspec/core/rake_task'
52
+
53
+ desc "Run all specs"
54
+ task RSpec::Core::RakeTask.new('spec')
55
+
56
+ task :default => "spec"
57
+
58
+ desc "Open an irb session preloaded with this library"
59
+ task :console do
60
+ SPEC = eval(File.read(gemspec_file))
61
+ require_paths = SPEC.require_paths.map {|path| "-I #{path.strip}"}.join(" ")
62
+ sh "irb -rubygems #{require_paths} -r ./lib/#{name}.rb"
63
+ end
64
+
65
+ #############################################################################
66
+ #
67
+ # Custom tasks (add your own tasks here)
68
+ #
69
+ #############################################################################
70
+
71
+
72
+
73
+ #############################################################################
74
+ #
75
+ # Packaging tasks
76
+ #
77
+ #############################################################################
78
+
79
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
80
+ task :release => :build do
81
+ unless `git branch` =~ /^\* master$/
82
+ puts "You must be on the master branch to release!"
83
+ exit!
84
+ end
85
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
86
+ sh "git tag v#{version}"
87
+ sh "git push origin master"
88
+ sh "git push origin v#{version}"
89
+ sh "gem push pkg/#{name}-#{version}.gem"
90
+ end
91
+
92
+ desc "Build #{gem_file} into the pkg directory"
93
+ task :build => [:gemspec, :update_bundle] do
94
+ sh "mkdir -p pkg"
95
+ sh "gem build #{gemspec_file}"
96
+ sh "mv #{gem_file} pkg"
97
+ end
98
+
99
+ desc "Generate #{gemspec_file}"
100
+ task :gemspec => :validate do
101
+ # read spec file and split out manifest section
102
+ spec = File.read(gemspec_file)
103
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
104
+
105
+ # replace name version and date
106
+ replace_header(head, :name)
107
+ replace_header(head, :version)
108
+ replace_header(head, :date)
109
+ #comment this out if your rubyforge_project has a different name
110
+ replace_header(head, :rubyforge_project)
111
+
112
+ # determine file list from git ls-files
113
+ files = `git ls-files`.
114
+ split("\n").
115
+ sort.
116
+ reject { |file| file =~ /^\./ }.
117
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
118
+ map { |file| " #{file}" }.
119
+ join("\n")
120
+
121
+ # piece file back together and write
122
+ manifest = " s.files = %w[\n#{files}\n ]\n"
123
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
124
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
125
+ puts "Updated #{gemspec_file}"
126
+ end
127
+
128
+ desc "Update #{name} in bundle"
129
+ task :update_bundle => :validate do
130
+ `bundle update #{name}`
131
+ puts "Bundled #{name} version #{version}"
132
+ end
133
+
134
+ desc "Validate #{gemspec_file}"
135
+ task :validate do
136
+ libfiles = Dir['lib/*'] - ["lib/#{lib_name}.rb"] #, "lib/#{name}"]
137
+ unless libfiles.empty?
138
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
139
+ exit!
140
+ end
141
+ unless Dir['VERSION*'].empty?
142
+ puts "A `VERSION` file at root level violates Gem best practices."
143
+ exit!
144
+ end
145
+ end
@@ -0,0 +1,43 @@
1
+ module Mongoid
2
+ module Extensions
3
+ module Hash
4
+ module IndifferentAccess
5
+ VERSION = "0.0.1"
6
+
7
+ def self.included(klass)
8
+ klass.class_eval do
9
+ # @override Mongoid::Field
10
+ def self.field(name, options={})
11
+ field = super(name, options)
12
+
13
+ # why oh why does sometimes Hash != Hash? something with my environment maybe?
14
+ # tried ruby 1.9.2-p290 and jruby 1.6.6 (1.9 mode), so we also try checking the class name
15
+ if options[:type] == Hash || options[:type].name =~ /Hash/
16
+ getter_name = name.is_a?(Symbol) ? name : name.intern
17
+ setter_name = "#{getter_name}=".intern
18
+
19
+ define_method(getter_name) do
20
+ val = super()
21
+ unless val.nil? || val.is_a?(HashWithIndifferentAccess)
22
+ wrapped_hash = val.with_indifferent_access
23
+ self.send(setter_name, wrapped_hash)
24
+ val = wrapped_hash
25
+ end
26
+ val
27
+ end
28
+
29
+ define_method(setter_name) do |hash|
30
+ unless hash.nil? || hash.is_a?(HashWithIndifferentAccess)
31
+ hash = hash.with_indifferent_access
32
+ end
33
+ super(hash)
34
+ end
35
+ end
36
+ field
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,71 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'mongoid-indifferent-access'
16
+ s.version = '0.0.1'
17
+ s.date = '2012-03-13'
18
+ s.rubyforge_project = 'mongoid-indifferent-access'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "A Mongoid Hash extension enabling 'indifferent access' so you can access keys using Strings or Symbols."
23
+ s.description = s.summary
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Craig Wickesser"]
29
+ s.email = 'craig@mindscratch.org'
30
+ s.homepage = 'https://github.com/mindscratch/mongoid-indifferent-access'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib ext/java/jar]
35
+
36
+ ## Specify any RDoc options here. You'll want to add your README and
37
+ ## LICENSE files to the extra_rdoc_files list.
38
+ ## s.rdoc_options = ["--charset=UTF-8"]
39
+ ## s.extra_rdoc_files = %w[README LICENSE]
40
+
41
+ ## List your runtime dependencies here. Runtime dependencies are those
42
+ ## that are needed for an end user to actually USE your code.
43
+ s.add_dependency 'mongoid'
44
+
45
+ ## List your development dependencies here. Development dependencies are
46
+ ## those that are only needed during development
47
+ ## s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
48
+ s.add_development_dependency 'rake'
49
+ s.add_development_dependency 'rspec'
50
+
51
+ ## Leave this section as-is. It will be automatically generated from the
52
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
53
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
54
+ # = MANIFEST =
55
+ s.files = %w[
56
+ Gemfile
57
+ Gemfile.lock
58
+ README.md
59
+ Rakefile
60
+ lib/mongoid_indifferent_access.rb
61
+ mongoid-indifferent-access.gemspec
62
+ spec/mongoid-indifferent-access/indifferent_access_spec.rb
63
+ spec/mongoid.yml
64
+ spec/spec_helper.rb
65
+ ]
66
+ # = MANIFEST =
67
+
68
+ ## Test files will be grabbed from the file list. Make sure the path glob
69
+ ## matches what you actually use.
70
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
71
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'mongoid_indifferent_access'
3
+ require 'mongoid'
4
+
5
+ # mocking some of what Mongoid::Document would provide
6
+ # so the spec doesn't require having MongoDB running
7
+ class MockSuper
8
+ attr_accessor :config
9
+ def self.field(name, options={})
10
+ nil
11
+ end
12
+ end
13
+
14
+ class Guitar < MockSuper
15
+ include Mongoid::Extensions::Hash::IndifferentAccess
16
+
17
+ field :config, :type => Hash
18
+ end
19
+
20
+ module Mongoid::Extensions::Hash
21
+
22
+ describe IndifferentAccess do
23
+ subject {
24
+ g = Guitar.new
25
+ g.config = {:value => 123}
26
+ g
27
+ }
28
+
29
+ it "returns a value given a String as the key" do
30
+ subject.config["value"].should eq(123)
31
+ end
32
+
33
+ it "returns a value given a Symbol as the key" do
34
+ subject.config[:value].should eq(123)
35
+ end
36
+
37
+ it "returns nil given a non-existing key" do
38
+ subject.config[:non_existant].should be_nil
39
+ end
40
+
41
+ end
42
+
43
+ end
data/spec/mongoid.yml ADDED
@@ -0,0 +1,20 @@
1
+ development:
2
+ host: localhost
3
+ database: mongoid_dev
4
+
5
+ test:
6
+ host: localhost
7
+ database: mongoid_test
8
+
9
+ # set these environment variables on your prod server
10
+ # production:
11
+ # host: <%= ENV['MONGOID_HOST'] %>
12
+ # port: <%= ENV['MONGOID_PORT'] %>
13
+ # username: <%= ENV['MONGOID_USERNAME'] %>
14
+ # password: <%= ENV['MONGOID_PASSWORD'] %>
15
+ # database: <%= ENV['MONGOID_DATABASE'] %>
16
+ # # slaves:
17
+ # # - host: slave1.local
18
+ # # port: 27018
19
+ # # - host: slave2.local
20
+ # # port: 27019
@@ -0,0 +1,16 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ # RSpec.configure do |config|
8
+ # config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ # config.run_all_when_everything_filtered = true
10
+ # config.filter_run :focus
11
+ # end
12
+
13
+ require 'rspec/autorun'
14
+ ROOT = File.expand_path('../..', __FILE__)
15
+ Dir[File.join(ROOT, "spec/support/**/*.rb")].each {|f| require f}
16
+ $LOAD_PATH.unshift(File.expand_path('lib', ROOT))
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-indifferent-access
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig Wickesser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: &2176859440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2176859440
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2176858660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2176858660
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2176857780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2176857780
47
+ description: A Mongoid Hash extension enabling 'indifferent access' so you can access
48
+ keys using Strings or Symbols.
49
+ email: craig@mindscratch.org
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - README.md
57
+ - Rakefile
58
+ - lib/mongoid_indifferent_access.rb
59
+ - mongoid-indifferent-access.gemspec
60
+ - spec/mongoid-indifferent-access/indifferent_access_spec.rb
61
+ - spec/mongoid.yml
62
+ - spec/spec_helper.rb
63
+ homepage: https://github.com/mindscratch/mongoid-indifferent-access
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ - ext/java/jar
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project: mongoid-indifferent-access
84
+ rubygems_version: 1.8.11
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: A Mongoid Hash extension enabling 'indifferent access' so you can access
88
+ keys using Strings or Symbols.
89
+ test_files: []