class Wordpress::OpenStruct

Public Class Methods

new(hash = {}) click to toggle source
# File lib/wordpress/ostruct.rb, line 7
def initialize(hash = {})
  assign(hash)
end

Public Instance Methods

[](k) click to toggle source
# File lib/wordpress/ostruct.rb, line 29
def [](k)
  send(k.to_s)
end
[]=(k, v) click to toggle source
# File lib/wordpress/ostruct.rb, line 33
def []=(k, v)
  send("#{k}=", v)
end
assign(hash = {}) click to toggle source
# File lib/wordpress/ostruct.rb, line 11
def assign(hash = {})
  @hash = Hash[hash.map{ |k, v| [k.to_s, v] }]
  self
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/wordpress/ostruct.rb, line 16
def respond_to_missing?(method_name, include_private = false)
  key = method_name.to_s
  if @hash.include?(key)
    define_accessor(key)
  else
    super
  end
end
to_hash() click to toggle source
# File lib/wordpress/ostruct.rb, line 37
def to_hash
  @hash
end
to_s() click to toggle source
# File lib/wordpress/ostruct.rb, line 25
def to_s
  "#<#{self.class.name} #{@hash.keys.map{ |k| "#{k}=#{send(k).to_s}" }.join(" ")}>"
end

Private Instance Methods

define_accessor(key) click to toggle source
# File lib/wordpress/ostruct.rb, line 54
def define_accessor(key)
  metaclass.send(:define_method, key, Proc.new{
    v = @hash[key]
    v.is_a?(Hash) ? Wordpress::OpenStruct.new(v) : v
  })
  metaclass.send(:define_method, "#{key}=", Proc.new{ |v|
    @hash[key] = v
  })
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/wordpress/ostruct.rb, line 43
def method_missing(method_name, *args, &block)
  key = method_name.to_s
  key = key[-1] == '=' ? key[0...-1] : key
  if @hash.include?(key)
    define_accessor(key)
    send(method_name, *args, &block)
  else
    super
  end
end