Socket
Book a DemoInstallSign in
Socket

hashtring

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hashtring

0.0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

Hashtring

A Hashstring is an object that I wanted to experiment with. I was annoyed that i18n translations don't have a nice way of nesting under logical trees.

For example, given:

# en.yml

reports:
  marketing: Marketing Reports
  sales: Sales Reports

Trying to call I18n.t(:reports) returns the hash of marketing and sales with their values. The solution would be to have two keys: an actual name and a namespace:

# en.yml

reports: Reports
report_types:
  marketing: Marketing Reports
  sales: Sales Reports

I wanted to experiment with a pattern that wouldn't require sacrifice. Enter the Hashtring:

reports = "Reports" ** {
  marketing: "Marketing Reports",
  sales: "Sales Reports"
}

puts reports # "Reports"
puts reports.marketing # "Marketing Reports"

Sure, it may not be entirely practical, and it probably doesn't solve any real problems, but it was something I wanted to play around with.

Installation

Add this line to your application's Gemfile:

gem 'hashtring'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hashtring

Usage

To create a Hashtring, simply call the #** method on a string and give it a hash.

"label" ** {
  key1: "value1",
  key2: "value2"
}

One-liner:

"label" ** key: "value"

You can even nest Hashtrings:

orders = "Orders" ** {
  line_items: "Line Items",
  additional_charges: "Summary" ** {
    tax: "NYS Tax",
    shipping: "UPS Shipping"
  }
}

Call up values just like you would with a normal hash, but root nodes can be output as strings.

puts orders # orders
puts orders[:line_items] # "Line Items"
puts orders[:additional_charges] # "Summary"
puts orders[:additional_charges][:tax] # "NYS Tax"

For fun, I leveraged method_missing to give an i18n-like syntax

puts orders.line_items # "Line Items"
puts orders.additional_charges.tax # "NYS Tax"

Contributing

  • Fork it ( https://github.com/KyleMacey/Hashtring/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

FAQs

Package last updated on 04 Mar 2015

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.