Class: Gmail::GmailObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Hooks
Defined in:
lib/gmail/gmail_object.rb

Direct Known Subclasses

APIResource

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ GmailObject

Returns a new instance of GmailObject.



12
13
14
15
# File 'lib/gmail/gmail_object.rb', line 12

def initialize(hash={})
  @values = Hashie::Mash.new hash
  run_hook :after_initialize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)

def remove_accessors(keys)

metaclass.instance_eval do
  keys.each do |k|
    next if @@permanent_attributes.include?(k)
    k_eq = :"#{k}="
    remove_method(k) if method_defined?(k)
    remove_method(k_eq) if method_defined?(k_eq)
  end
end

end

def add_accessors(keys)

metaclass.instance_eval do
  keys.each do |k|
    next if @@permanent_attributes.include?(k)
    k_eq = :"#{k}="
    define_method(k) { @values[k] }
    define_method(k_eq) do |v|
      if v == ""
        raise ArgumentError.new(
                  "You cannot set #{k} to an empty string." +
                      "We interpret empty strings as nil in requests." +
                      "You may set #{self}.#{k} = nil to delete the property.")
      end
      @values[k] = v
      @unsaved_values.add(k)
    end
  end
end

end



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gmail/gmail_object.rb', line 124

def method_missing(name, *args)

  if @values.send(name.to_s + "?")
    @values.send(name)
  else
    begin
      @values.send(name.to_s, args[0])
    rescue
      begin
        super.send(name)
      rescue
       nil
      end
    end
  end
end

Instance Method Details

#[](k) ⇒ Object



26
27
28
# File 'lib/gmail/gmail_object.rb', line 26

def [](k)
  @values[k.to_sym]
end

#[]=(k, v) ⇒ Object



30
31
32
# File 'lib/gmail/gmail_object.rb', line 30

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

#as_json(*a) ⇒ Object



38
39
40
# File 'lib/gmail/gmail_object.rb', line 38

def as_json(*a)
  @values.as_json(*a)
end

#detailedObject



42
43
44
45
46
47
48
# File 'lib/gmail/gmail_object.rb', line 42

def detailed
  if self.class == GmailObject
    raise "Can't detail a generic GmailObject. It needs to be a Thread, Message, Draft or Label"
  end

  self.class.get(id)
end

#inspectObject



22
23
24
# File 'lib/gmail/gmail_object.rb', line 22

def inspect
 "#<#{self.class}:0x#{self.object_id.to_s(16)}>  " + to_s
end

#refreshObject Also known as: detailed!



50
51
52
53
54
55
56
# File 'lib/gmail/gmail_object.rb', line 50

def refresh
  if self.class == GmailObject
    raise "Can't refresh a generic GmailObject. It needs to be a Thread, Message, Draft or Label"
  end
  @values = self.class.get(id).values
  self
end

#to_hashObject



60
61
62
# File 'lib/gmail/gmail_object.rb', line 60

def to_hash
  Util.symbolize_names(@values.to_hash)
end

#to_json(*a) ⇒ Object



34
35
36
# File 'lib/gmail/gmail_object.rb', line 34

def to_json(*a)
  JSON.generate(@values)
end

#to_s(*args) ⇒ Object



18
19
20
# File 'lib/gmail/gmail_object.rb', line 18

def to_s(*args)
  JSON.pretty_generate(@values.to_hash)
end

#valuesObject



64
65
66
# File 'lib/gmail/gmail_object.rb', line 64

def values
  @values
end