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

nested_accessors

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nested_accessors

  • 0.1.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status Code Climate

NestedAccessors

This class lets you quickly add serialized, nested hash accessors in ActiveRecord model objects, using regular text database columns.

class Person < ActiveRecord::Base
  include NestedAccessors

  nested_accessor :info, [ :name, :phone ]
end

This would synthesize accessors to the name and phone properties contained in the info hash.

foo = Foo.new
foo.name = "bar"
foo.phone = "+46-123-45678"
foo.info #=> { "name" => "bar", "phone" => "+46-123-45678" }

If we want just a single name accessor we can remove the array:

nested_accessor :info, :name

Installation

  • Add the gem to your Gemfile:

      source :rubygems
      gem 'nested_accessors', :git => 'git@github.com:avocade/nested_accessors.git'
    
  • Run bundle install

Dependencies

Requires ActiveRecord for the serialize class method. (To be reconsidered.)

Nested hash

You can nest accessors one level deep. To add more than one nested hash accessor to address we specify street and city inside an array.

nested_accessor :info, address: [ :street, :city ]

This gives:

object.address_street  # these two are nested inside the "address" hash
object.address_city

To just specify just one accessor, eg address[street], we can simplify:

nested_accessor :info, address: street

Note: The hash keys are always strings, never symbols. This is for consistency of input/output.

Testing

Run the minitest specs using:

rake

TODO

  • Add assertion checking for args

  • Switch to using supermodel gem for testing active_model serialize dependency properly instead of stubbing it out

  • Allow setting default value for each accessor, eg last_name: "Doe"

  • Create object wrappers for each nested hash, so can get accessors like person.address.street for person[address][street]

  • Add two-level nesting, eg foo[bar][name][surname]

  • Array with nested Hash objects inside, eg foo[bar][1][name]

  • Perhaps switch convention to always use symbolic keys instead of stringified keys, so can use new ruby 1.9 hash syntax:

      foo.info = { name: "Anders" }
      foo.info[:name]
    
  • Add typed arguments for each accessor (defaults to String)

      nested_accessor :info, phone: Integer
    

Author

Oskar Boethius Lissheim is a Product Designer and iOS & Ruby on Rails developer. He currently lives in Gothenburg, Sweden.

You can reach him on App.net at @avocade, or Twitter.

License

This code is offered under the MIT License.

Copyright © 2012 Oskar Boethius Lissheim.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Package last updated on 04 Jun 2013

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