Class: Wordpress::Object

Inherits:
OpenStruct show all
Defined in:
lib/wordpress/object.rb

Direct Known Subclasses

Array, Comment, LikeResult, Post, Site, Test, User

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenStruct

#[], #[]=, #to_s

Methods inherited from Base

attr_accessor, #metaclass

Constructor Details

#initialize(params = {}) ⇒ Object

Returns a new instance of Object.



30
31
32
# File 'lib/wordpress/object.rb', line 30

def initialize(params = {})
  params ? super(Hash[params.map { |k, v| [k.to_s.downcase, v] }]) : super
end

Class Method Details

.attr_parameter(*args) ⇒ Object



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

def attr_parameter(*args)
  options = args[-1].is_a?(Hash) ? args.pop : {}
  args.each do |arg|
    arg = arg.to_s

    define_method arg do
      send("#{arg}=", @hash[arg])
    end

    define_method "#{arg}=" do |v|
      if as = options[:as]
        @hash[arg] = (as.is_a?(Proc) ? as.call(v) : as.new(v))
      elsif v.is_a?(Hash)
        @hash[arg] = Wordpress::OpenStruct.new(v)
      else
        @hash[arg] = v
      end
      @hash[arg]
    end
  end
end