Module: Zuck::FbObject::DSL::ClassMethods

Defined in:
lib/zuck/fb_object/dsl.rb

Instance Method Summary collapse

Instance Method Details

#connections(*args) ⇒ Object

Defines which other classes might have this one as their parent.

If you do something like

ruby class Foo < RawFbObject ... connections :dings, :dongs end

then your Foo instances will have a #dings and #dongs methods, which will call Ding.all and Dong.call on the appropriate graph object.

Also, you will get a #create_ding and #create_dong methods that forward to Ding.new and Dong.new.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zuck/fb_object/dsl.rb', line 99

def connections(*args)
  args.each do |c|
    class_resolver = lambda{"Zuck::#{c.to_s.singularize.camelize}".constantize}

    # Define getter for connections
    define_method(c.to_s.pluralize) do
      class_resolver.call.all(graph, self)
    end

    # Define create method for connections
    define_method("create_#{c.to_s.singularize}") do |data|
      class_resolver.call.create(graph, data, self)
    end
  end
end

#list_path(path = nil) ⇒ Object

Part of our little DSL, sets the part of the path that fetches the list of objects from facebook.

class Foo < FbObject
   ...
   list_path :foos
 end

Zuck::FbObject uses this to construct a path together with this class’ parent object’s path method (which is usually just it’s ID property)

Parameters:

  • path (String, Symbol) (defaults to: nil)

    Pass a value if you want to set the list_path for this object.

Returns:

  • The object’s list_path



43
44
45
46
# File 'lib/zuck/fb_object/dsl.rb', line 43

def list_path(path = nil)
  @list_path = path if path
  @list_path
end

#parent_object(type, options = {}) ⇒ Object

Pretty much like a belongs_to, but is used to construct paths to access the facebook api.

It also defines a getter method. Look

class AdCampaign < FbObject
  ...
  parent_object :ad_account
end

Now on instances you can call my_campaign.ad_account to fetch the ad account your campaign is part of.

Parameters:

  • type (Symbol)

    Pass an underscored symbol here, for example :ad_account



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zuck/fb_object/dsl.rb', line 63

def parent_object(type, options = {})

  # The `Read` module uses this
  @parent_object_type = type.to_s

  define_method(type) do
    # Why a lambda? Because it gets evaluated on runtime, not now. This is a
    # good thing because it allows for randomly loading files with classes
    # that inherit from FbObject.
    class_resolver = lambda{"Zuck::#{type.to_s.singularize.camelize}".constantize}

    if options[:as]
      @parent_object ||= class_resolver.call.new(@graph, {id: send(options[:as])}, nil)
    else
      @parent_object
    end
  end
end

#read_onlyObject

Don’t allow create/update/delete



20
21
22
# File 'lib/zuck/fb_object/dsl.rb', line 20

def read_only
  @read_only = true
end

#read_only?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/zuck/fb_object/dsl.rb', line 24

def read_only?
  !!@read_only
end