Giter VIP home page Giter VIP logo

nested_send's Introduction

Gem Version

nested_send

An extension for Ruby's Object class to allow for nested send functionality using a naturally formatted string.

This makes it possible to access deep attributes and use method chaining using send without having to establish your own recursive strategy.

Currently, there is no support for method arguments. You will not be able to use: @object.nested_send("join('')") for example. This will be coming at a slightly later date when I have the time to update it.

However, you can chain for as long as you like otherwise: @object.nested_send("to_sym.to_s.to_sym.to_s") and such.

Installation

Gemfile:

gem 'nested_send'

Local:

gem install nested_send

Usage:

require 'nested_send'

Examples

require 'nested_send'

class TestingObject
  attr_accessor :name, :books, :traits, :preferences

  def initialize(args = {})
    @name = args[:name] ||= "Will"
    @books = args[:books] ||= ["Moby Dick", "The Two Towers", "Hank the Cowdog"]
    @traits = args[:traits] ||= { height: 180, weight: 175, age: 30}
    @preferences = args[:preferences] ||= { "email" => true }
  end

  def itself
    self
  end

end

# Methods
@object = TestingObject.new
@object.nested_send("name.to_sym")
# => :Will

# Arrays
@object.nested_send("books[0]")
# => "Moby Dick"

# Works with symbolized hash keys
@object.nested_send("traits[:age]")
# => 30

# Works with stringified hash keys
@object.nested_send("preferences['email']")
# => true

# Works with methods that return self
@object.nested_send("itself.books[-1]")
# => "Hank the Cowdog"

nested_send's People

Contributors

rhoxio avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

nested_send's Issues

Option for Indifferent Access in Hashes

Technically you can pass as a second argument, but need to document it and set up a config block in case they want this to be part of default functionality.

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.