class Hashie::Clash
A Clash is a “Chainable Lazy Hash”. Inspired by libraries such as Arel, a Clash allows you to chain together method arguments to build a hash, something that’s especially useful if you’re doing something like constructing a complex options hash. Here’s a basic example:
c = Hashie::Clash.new.conditions(:foo => 'bar').order(:created_at) c # => {:conditions => {:foo => 'bar'}, :order => :created_at}
Clash provides another way to create sub-hashes by using bang notation. You can dive into a sub-hash by providing a key with a bang and dive back out again with the _end! method. Example:
c = Hashie::Clash.new.conditions!.foo('bar').baz(123)._end!.order(:created_at) c # => { conditions: { foo: 'bar', baz: 123 }, order: :created_at}
Because the primary functionality of Clash is to build options objects, all keys are converted to symbols since many libraries expect symbols explicitly for keys.
@example Build an options hash through chained calls
options = Hashie::Clash.new.conditions!.active(true)._end!.limit(10) options # => { conditions: { active: true }, limit: 10 }
Attributes
Public Class Methods
Initialize a new clash by passing in a Hash to convert and, optionally, the parent to which this Clash is chained.
@example Normalize initial keys to symbols
Hashie::Clash.new('limit' => 10) # => { limit: 10 }
# File lib/hashie/clash.rb, line 54 def initialize(other_hash = {}, parent = nil) @_parent = parent other_hash.each_pair do |k, v| self[k.to_sym] = v end end
Public Instance Methods
Jump back up a level if you are using bang method chaining. For example:
c = Hashie::Clash.new.foo(‘bar’) c.baz!.foo(123) # => c c.baz!._end! # => c
@example Continue building the parent hash
Hashie::Clash.new.conditions!.active(true)._end!.limit(10) # => { conditions: { active: true }, limit: 10 }
# File lib/hashie/clash.rb, line 70 def _end! _parent end
@example Serialize a Clash through YAML’s encoding callback
require 'yaml' options = Hashie::Clash.new.limit(10) YAML.unsafe_load(YAML.dump(options)) # => { limit: 10 }
# File lib/hashie/clash.rb, line 78 def encode_with(coder) coder['hash'] = to_h coder['parent'] = _parent end
@example Restore a chained Clash through YAML’s loading callback
require 'yaml' root = Hashie::Clash.new.conditions!.active(true)._end! restored = YAML.unsafe_load(YAML.dump(root)) restored[:conditions]._parent.equal?(restored) # => true
# File lib/hashie/clash.rb, line 88 def init_with(coder) initialize(coder['hash'], coder['parent']) end
@example Discover supported builder calls
options = Hashie::Clash.new(limit: 10) options.respond_to?(:conditions!) # => true options.respond_to?(:limit!) # => false options.respond_to?(:order) # => true
# File lib/hashie/clash.rb, line 141 def respond_to_missing?(method_name, _include_private = false) method_name = method_name.to_s return super if %w[marshal_dump marshal_load _dump _load].include?(method_name) if method_name.end_with?('!') key = method_name[0...-1].to_sym [NilClass, Clash, Hash].include?(self[key].class) else true end end