Giter Club home page Giter Club logo

metahash's Introduction

DEPRECATED

Ruby's OpenStruct offers the same functionality.

MetaHash

Gem Version Build Status Code Climate Test Coverage Dependency Status security

Provides a subclass of Hash and a wrapper around Rails' serialize attribute for object-like access to hashes without validating existence of nested hashes

Examples

Access nested hashes using method / object syntax

Arand new Metadata objects act just like hashes:

h = Metadata.new
h
=> {}

Accessing nested data requires no wrapping conditions checking for existence of the rquested data:

h.outer.inner
=> {}

Possible real-word example:

if (min_numbers = h.password_rules.formats.numbers.minimum).present?
  # some code using min_numbers
else
  # data doesn't exist in h

  h.password_rules.formats.numbers.minimum = 1
  # h
  # => { password_rules: { formats: { numbers: { minimum: 1 } } } }
end

Convert an existing hash to metadata

{a: 1}.to_metadata.a
# => 1

Access to values stored in nested hashes via method call syntax

h = Metadata.new( { outer: { inner: { hash_key: "value" } } } )

h.outer.inner.hash_key
=> "value"

Set values for nested hash structures without the nested hashes having to be initially defined

h = Metadata.new
h
=> {}

h.outer.inner = 2
h
=> { outer: { inner: 2 } }

Using with ActiveRecord

In your Gemfile

gem "metahash-rb", require: "metahash"

in your ActiveRecord model

has_metadata

or

has_metadata :field_not_called_metadata

Support

This gem has been tested with Ruby 2.0, and rails 3.2, 4.1

Contributing

  1. Fork the project
  2. Create a new, descriptively named branch
  3. Add Test(s)!
  4. Commit your proposed changes
  5. Submit a pull request

Analytics

metahash's People

Contributors

nullvoxpopuli avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.