Sha256: fed76a66fbf792b08df36452a50905af5f517bc8fac485cc136435dbcdcb1403

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'hashie/mash'

# @private
class Hashie::Mash

  # Convert results to Ruby / Rails friendly attributes
  def to_friendly_hash
    out = {}
    keys.each do |k|
      out[genability_to_ruby_friendly(k)] = Hashie::Hash === self[k] ? self[k].to_hash : self[k]
    end
    out
  end

  # Modified Hashie::Mash method missing
  def method_missing(method_name, *args, &blk)
    begin
      method_name = ruby_to_genability_friendly(method_name)
    rescue; end
    return self.[](method_name, &blk) if key?(method_name)
    match = method_name.to_s.match(/(.*?)([?=!]?)$/)
    case match[2]
    when "="
      self[match[1]] = args.first
    when "?"
      !!self[match[1]]
    when "!"
      initializing_reader(match[1])
    else
      default(method_name, *args, &blk)
    end
  end

  def ruby_to_genability_friendly(method_name)
    method_name.to_s.gsub(/(?:^|_)(.)/){ $1.upcase }.gsub(/^[A-Z]/){ $&.downcase }.to_sym
  end

  def genability_to_ruby_friendly(method_name)
    method_name.to_s.gsub(/^[A-Z]/){ $&.downcase }.gsub(/[A-Z]/){ "_#{$&.downcase}" }.to_sym
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
genability-0.2.0 lib/mashie_extensions.rb