Skip to content

Instantly share code, notes, and snippets.

@Haegin
Last active August 29, 2015 14:02
Show Gist options
  • Save Haegin/ccdcab135b55281f495f to your computer and use it in GitHub Desktop.
Save Haegin/ccdcab135b55281f495f to your computer and use it in GitHub Desktop.

Array to Hash

This vim function turns Ruby arrays of symbols into new-style Ruby hashes where the symbol is the key and the value is the symbol converted to a MixedCase string.

Change Hash Style

This vim function switches Ruby hashes from the old hash-rocket style to the newer JSONesque style and back. It'll try and guess what the current style is and switch to the other one.

Dependencies

Examples

NODE_ORDER = [:employee_details, :employment]

becomes

NODE_ORDER = { employee_details: "EmployeeDetails",
               employment: "Employment" }

      NODE_ORDER = [
        :tax_code,
        :taxable_pay,
        :benefits_taxed_via_payroll,
        :employee_pension_contributions_paid,
        :employee_pension_contributions_not_paid,
        :student_loan_recovered,
        :tax,
        :statutory_sick_pay,
        :statutory_maternity_pay,
        :ordinary_statutory_paternity_pay,
        :statutory_adoption_pay,
        :additional_statutory_paternity_pay,
        :trivial_commutation_payment,
      ]

becomes

      NODE_ORDER = { tax_code: "TaxCode",
                     taxable_pay: "TaxablePay",
                     benefits_taxed_via_payroll: "BenefitsTaxedViaPayroll",
                     employee_pension_contributions_paid: "EmployeePensionContributionsPaid",
                     employee_pension_contributions_not_paid: "EmployeePensionContributionsNotPaid",
                     student_loan_recovered: "StudentLoanRecovered",
                     tax: "Tax",
                     statutory_sick_pay: "StatutorySickPay",
                     statutory_maternity_pay: "StatutoryMaternityPay",
                     ordinary_statutory_paternity_pay: "OrdinaryStatutoryPaternityPay",
                     statutory_adoption_pay: "StatutoryAdoptionPay",
                     additional_statutory_paternity_pay: "AdditionalStatutoryPaternityPay",
                     trivial_commutation_payment: "TrivialCommutationPayment",
      }
function! ArrayToHash()
"mark the start and end of the array
normal f[ms%me
" one item per line
's,'es/, /,
/g
" re-indent
normal vi[=
" update the ending mark as it might be wrong now
normal! `s%me
" change ':foo,' to 'foo: "foo",'
's,'es/\v:([a-z0-9_]+)/\1: "\1"/
" select the contents of the array/hash
normal! vi[
" use tpope's abolish.vim coerce command to convert the contents of the
" quotes to MixedCase
'<,'>normal f"lcrm
" change the array square brackets to a hashes braces
normal cs[{
endfunction
" This is a utility function to switch between the old style hash rocket syntax
" and the newer json style syntax for Ruby hashes.
function! ChangeHashStyle()
" Set start and end marks
normal vi{
let line = getline(".")
echom line
if line =~ '=>'
echom "Converting from hash rockets to json style"
" From hash rockets to JSON style
'<,'>s/\v:([a-z0-9_]+) *\=\> */\1: /g
else
echom "Converting from json style to hash rockets"
" From JSON style to hash rockets
'<,'>s/\v([a-z0-9_]+):/:\1 =>/g
endif
endfunction
@sarcas
Copy link

sarcas commented Jun 10, 2014

Should I write a switching version that gives the old hashrocket syntax? 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment