Class: Hashie::Clash

Inherits:
Hash
  • Object
show all
Defined in:
lib/hashie/clash.rb

Overview

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.

Defined Under Namespace

Classes: ChainError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(other_hash = {}, parent = nil) ⇒ Clash

Initialize a new clash by passing in a Hash to convert and, optionally, the parent to which this Clash is chained.



32
33
34
35
36
37
# File 'lib/hashie/clash.rb', line 32

def initialize(other_hash = {}, parent = nil)
  @_parent = parent
  other_hash.each_pair do |k, v|
    self[k.to_sym] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hashie/clash.rb', line 66

def method_missing(name, *args) #:nodoc:
  if args.empty? && name.to_s.end_with?('!')
    key = name[0...-1].to_sym

    case self[key]
    when NilClass
      self[key] = self.class.new({}, self)
    when Clash
      self[key]
    when Hash
      self[key] = self.class.new(self[key], self)
    else
      raise ChainError, 'Tried to chain into a non-hash key.'
    end
  elsif args.any?
    merge_store(name, *args)
  else
    super
  end
end

Instance Attribute Details

#_parentObject (readonly)

The parent Clash if this Clash was created via chaining.



27
28
29
# File 'lib/hashie/clash.rb', line 27

def _parent
  @_parent
end

Instance Method Details

#_end!Object

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[:baz] c.baz!._end! # => c



45
46
47
# File 'lib/hashie/clash.rb', line 45

def _end!
  _parent
end

#id(*args) ⇒ Object

:nodoc:



49
50
51
# File 'lib/hashie/clash.rb', line 49

def id(*args) #:nodoc:
  method_missing(:id, *args)
end

#merge_store(key, *args) ⇒ Object

:nodoc:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hashie/clash.rb', line 53

def merge_store(key, *args) #:nodoc:
  case args.length
  when 1
    val = args.first
    val = self.class.new(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

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
# File 'lib/hashie/clash.rb', line 87

def respond_to_missing?(method_name, _include_private = false)
  method_name = method_name.to_s

  if method_name.end_with?('!')
    key = method_name[0...-1].to_sym
    [NilClass, Clash, Hash].include?(self[key].class)
  else
    true
  end
end