Class: Muddyit::Generic

Inherits:
Base
  • Object
show all
Defined in:
lib/muddyit/generic.rb

Constant Summary

Constants inherited from Base

Base::REST_ENDPOINT

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #access_token_secret, #consumer_key, #consumer_secret, #rest_endpoint

Instance Method Summary collapse

Methods inherited from Base

#send_request, #sites

Constructor Details

#initialize(muddyit, attributes = {}) ⇒ Generic

constructor

Params

  • muddyit (Required)

    a muddyit::base object
    
  • attributes (Optional)

    hash of method => value entries used to simulate methods on a real object
    


19
20
21
22
23
# File 'lib/muddyit/generic.rb', line 19

def initialize(muddyit, attributes = {})
  @muddyit = muddyit
  @attributes = attributes.nested_symbolize_keys!
  @info_added = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args = nil) ⇒ Object

request data from muddy.it if we haven’t done so before and we don’t have the attribute requested (acts as getter + setter)

Params

  • method (Required)

    the object method to populate, from attributes or remotely
    
  • args (Optional)

    the value to set the method to
    


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/muddyit/generic.rb', line 34

def method_missing(method, args = nil)
  if @info_added == false and !@attributes.has_key?(method.to_sym)
    #puts "Searching for missing method #{method.to_s}"
    @attributes.merge!(self.fetch)
    @info_added = true
  end
  unless @attributes.has_key?(method.to_sym)
    raise "No method named #{method.to_s}"
  end
  if args.nil?
    @attributes[method.to_sym]
  else
    @attributes[method.to_sym] = args
    return true
  end
end

Instance Attribute Details

#attributesObject

superclass for data objects to inherit

allows us to change the api with little code change via the magic of method missing :)



9
10
11
# File 'lib/muddyit/generic.rb', line 9

def attributes
  @attributes
end