Class: Wordpress::OpenStruct

Inherits:
Base
  • Object
show all
Defined in:
lib/wordpress/ostruct.rb

Direct Known Subclasses

Object

Instance Method Summary collapse

Methods inherited from Base

attr_accessor, #metaclass

Constructor Details

#initialize(hash = {}) ⇒ OpenStruct

Returns a new instance of OpenStruct.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wordpress/ostruct.rb', line 7

def initialize(hash = {})
  @hash = Hash[hash.map do |key, value|
    key = key.to_s
    unless respond_to?(key)
      metaclass.send(:define_method, key, Proc.new do
        v = @hash[key]
        @hash[key] = (v.is_a?(Hash) ? Wordpress::OpenStruct.new(v) : v)
      end)
    end
    unless respond_to?("#{key}=")
      metaclass.send(:define_method, "#{key}=", Proc.new do |v|
        @hash[key] = (v.is_a?(Hash) ? Wordpress::OpenStruct.new(v) : v)
        @hash[key]
      end)
    end
    [key, (value.is_a?(Hash) ? Wordpress::OpenStruct.new(value) : value)]
  end]
end

Instance Method Details

#[](k) ⇒ Object



36
37
38
# File 'lib/wordpress/ostruct.rb', line 36

def [](k)
  send(k)
end

#[]=(k, v) ⇒ Object



40
41
42
# File 'lib/wordpress/ostruct.rb', line 40

def []=(k, v)
  send("#{k}=", v)
end

#to_sObject

def respond_to_missing?(method_name, include_private = false)

  method_name = method_name.to_s
  @hash.include?(method_name) || super
end


32
33
34
# File 'lib/wordpress/ostruct.rb', line 32

def to_s
  "#<#{self.class.name} #{map { |k, v| "#{k}=#{v}" }.join(" ")}>"
end