Class: Blacklight::NestedOpenStructWithHashAccess

Inherits:
OpenStructWithHashAccess show all
Defined in:
lib/blacklight/utils.rb

Overview

An OpenStruct refinement that converts any hash-keys into additional instances of NestedOpenStructWithHashAccess

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OpenStructWithHashAccess

#sort_by, #sort_by!, #to_h

Constructor Details

#initialize(klass, *args) ⇒ NestedOpenStructWithHashAccess

Returns a new instance of NestedOpenStructWithHashAccess.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/blacklight/utils.rb', line 67

def initialize klass, *args
  @nested_class = klass
  hash = {}

  hashes_and_keys = args.flatten
  lazy_configs = hashes_and_keys.extract_options!

  hashes_and_keys.each do |v|
    if v.is_a? Hash
      key = v.first
      value = v[key]

      hash[key] = nested_class.new value
    else
      hash[v] = nested_class.new
    end
  end

  lazy_configs.each do |k,v|
    hash[k] = if v.is_a? nested_class
                v
              else
                nested_class.new v
              end
  end

  super hash
  set_default_proc!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object

Override #method_missing from OpenStruct to ensure the default_proc logic gets triggered.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/blacklight/utils.rb', line 160

def method_missing(mid, *args)
  len = args.length

  if len.zero?
    if respond_to?(:new_ostruct_member!, true)
      new_ostruct_member!(mid)
    else
      new_ostruct_member(mid)
    end
    @table[mid]
  else
    super
  end
end

Instance Attribute Details

#nested_classObject (readonly)

Returns the value of attribute nested_class.



64
65
66
# File 'lib/blacklight/utils.rb', line 64

def nested_class
  @nested_class
end

Instance Method Details

#<<(key) ⇒ Object

Add an new key to the object, with a default default



99
100
101
# File 'lib/blacklight/utils.rb', line 99

def << key
  @table[key]
end

#[]=(key, value) ⇒ Object

Add a new key/value to the object; if it's a Hash, turn it into another NestedOpenStructWithHashAccess



106
107
108
109
110
111
112
# File 'lib/blacklight/utils.rb', line 106

def []=(key, value)
  if value.is_a? Hash
    send "#{key}=", nested_class.new(value)
  else
    super
  end
end

#deep_dupObject



133
134
135
# File 'lib/blacklight/utils.rb', line 133

def deep_dup
  self.class.new self.nested_class, @table.deep_dup
end

#marshal_dumpObject

Before serializing, we need to reset the default proc so it can be serialized appropriately



117
118
119
120
121
122
# File 'lib/blacklight/utils.rb', line 117

def marshal_dump
  h = to_h.dup
  h.default = nil

  [nested_class, h]
end

#marshal_load(x) ⇒ Object

After deserializing, we need to re-add the default proc to the internal hash



127
128
129
130
131
# File 'lib/blacklight/utils.rb', line 127

def marshal_load x
  @nested_class = x.first
  super x.last
  set_default_proc!
end

#merge(other_hash) ⇒ OpenStructWithHashAccess

Merge the values of this OpenStruct with another OpenStruct or Hash

Parameters:

  • other_hash (Hash, #to_h)

Returns:



145
146
147
# File 'lib/blacklight/utils.rb', line 145

def merge other_hash
  self.class.new nested_class, to_h.merge((other_hash if other_hash.is_a? Hash) || other_hash.to_h)
end

#merge!(other_hash) ⇒ OpenStructWithHashAccess

Merge the values of another OpenStruct or Hash into this object

Parameters:

  • other_hash (Hash, #to_h)

Returns:



153
154
155
# File 'lib/blacklight/utils.rb', line 153

def merge! other_hash
  @table.merge!(nested_class, (other_hash if other_hash.is_a? Hash) || other_hash.to_h)
end

#select(*args, &block) ⇒ Object



137
138
139
# File 'lib/blacklight/utils.rb', line 137

def select *args, &block
  self.class.new nested_class, to_h.select(*args, &block)
end