Project

hashtring

0.0
No commit activity in last 3 years
No release in over 3 years
An interesting class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.7
~> 10.0
>= 3.2.0, ~> 3.2
 Project Readme

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

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