Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoid-indifferent-access

Package Overview
Dependencies
Maintainers
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoid-indifferent-access

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
3
Created
Source

Mongoid Indifferent Access Build Status

A Mongoid Hash extension enabling "indifferent access" so you can access keys using Strings or Symbols.

BSON stores document keys as strings, so Hash keys are strings when going into MongoDB and coming out. This extension allows 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 accessed using whichever approach suites the developer.

Ruby/JRuby

Thanks to travis-ci this gem is tested against Ruby 1.9.3 and JRuby 1.6.7.

It should also continue work with Ruby 1.9.2 only if you're using Mongoid 2.4.x. As of Mongoid 3.x, Ruby 1.9.3 or newer is required.

Usage

Before

Notice the nil values when the Hash values are fetched using Symbols.

class QueryResult
  include Mongoid::Document

  field :data, type: Hash
end

# create a QueryResult
QueryResult.create(data: {name: "google", value: 1.32})

# fetch it back from the database
result = QueryResult.first

# check its values
puts result.data["name"]  # => "google"
puts result.data[:value]  # => nil
puts result.data[:name]   # => nil
After

Notice the values in the Hash can be accessed using Strings or Symbols.

class QueryResult
  include Mongoid::Document
  include Mongoid::Extensions::Hash::IndifferentAccess

  field :data, type: Hash
end

# create a QueryResult
QueryResult.create(data: {name: "google", value: 1.32})

# fetch it back from the database
result = QueryResult.first

# check its values
puts result.data["name"]  # => "google"
puts result.data[:value]  # => 1.32
puts result.data[:name]   # => "google"
puts result.data["value"] # => 1.32

Contributors

I'd like to give a special shout out to those people who've submitted pull requests (which have been approved and merged):

FAQs

Package last updated on 04 Oct 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc