class Hashie::Clash
for keys.
all keys are converted to symbols since many libraries expect symbols explicitly
Because the primary functionality of Clash is to build options objects,
c # => {:conditions => {:foo => ‘bar’, :baz => 123}, :order => :created_at}
c = Hashie::Clash.new.conditions!.foo(‘bar’).baz(123)._end!.order(:created_at)
back out again with the _end! method. Example:
You can dive into a sub-hash by providing a key with a bang and dive
Clash provides another way to create sub-hashes by using bang notation.
c # => {:conditions => {:foo => ‘bar’}, :order => :created_at}
c = Hashie::Clash.new.conditions(:foo => ‘bar’).order(:created_at)
like constructing a complex options hash. Here’s a basic example:
hash, something that’s especially useful if you’re doing something
a Clash allows you to chain together method arguments to build a
A Clash is a “Chainable Lazy Hash”. Inspired by libraries such as Arel,
def _end!
c.baz!.foo(123) # => c[:baz]
c = Hashie::Clash.new.foo('bar')
chaining. For example:
Jump back up a level if you are using bang method
def _end! self._parent end
def id(*args) #:nodoc:
def id(*args) #:nodoc: method_missing(:id, *args) end
def initialize(other_hash = {}, parent = nil)
convert and, optionally, the parent to which this
Initialize a new clash by passing in a Hash to
def initialize(other_hash = {}, parent = nil) @_parent = parent other_hash.each_pair do |k, v| self[k.to_sym] = v end end
def merge_store(key, *args) #:nodoc:
def merge_store(key, *args) #:nodoc: case args.length when 1 val = args.first val = self[key].merge(val) if self[key].is_a?(::Hash) && val.is_a?(::Hash) else val = args end self[key.to_sym] = val self end
def method_missing(name, *args) #:nodoc:
def method_missing(name, *args) #:nodoc: name = name.to_s if name.match(/!$/) && args.empty? key = name[0...-1].to_sym if self[key].nil? self[key] = Clash.new({}, self) elsif self[key].is_a?(::Hash) && !self[key].is_a?(Clash) self[key] = Clash.new(self[key], self) else raise ChainError, "Tried to chain into a non-hash key." end self[key] elsif args.any? key = name.to_sym self.merge_store(key, *args) end end