1
0
Fork 0
mirror of https://github.com/nov/fb_graph2 synced 2023-03-27 23:22:15 -04:00

include AttributeAssigner in each FbGraph::Node subclass

This commit is contained in:
nov 2014-05-05 19:52:39 +09:00
parent 9966baddcb
commit 3caf010134
6 changed files with 37 additions and 10 deletions

View file

@ -8,6 +8,13 @@ module FbGraph2
cattr_accessor :registered_attributes
end
module ClassMethods
def register_attributes(attributes)
self.registered_attributes = attributes
send :attr_accessor, *attributes.values.flatten
end
end
def assign(attributes)
self.raw_attributes = attributes
self.class.registered_attributes.each do |type, keys|
@ -37,12 +44,5 @@ module FbGraph2
end
end
end
module ClassMethods
def register_attributes(attributes)
self.registered_attributes = attributes
send :attr_accessor, *attributes.values.flatten
end
end
end
end

View file

@ -0,0 +1,12 @@
module FbGraph2
class Edge
module Accounts
def accounts(params = {})
pages = self.edge :accounts, params
pages.collect do |page|
Page.new(page[:id], page).authenticate page[:access_token]
end
end
end
end
end

View file

@ -1,11 +1,9 @@
module FbGraph2
class Node
include AttributeAssigner
attr_accessor :id, :access_token, :raw_attributes
def initialize(id, attributes = {})
self.id = id
assign attributes
end
def authenticate(access_token)

View file

@ -1,11 +1,15 @@
module FbGraph2
class Page < Node
include AttributeAssigner
register_attributes(
raw: [
:about, :attire, :band_members, :booking_agent, :can_post, :category, :checkins, :company_overview,
:current_location, :description, :directed_by, :founded, :general_info, :general_manager, :hometown,
:is_permanently_closed, :is_published, :is_unclaimed, :likes, :link, :mission, :name, :phone, :press_contact,
:products, :talking_about_count, :username, :website, :were_here_count
:products, :talking_about_count, :username, :website, :were_here_count,
# only within /:user_id/accounts context
:perms
],
date: [:birthday],
page: [:best_page],
@ -17,6 +21,7 @@ module FbGraph2
def initialize(id, attributes = {})
super
assign attributes
# TODO: handle custom attributes.
end

View file

@ -1,5 +1,8 @@
module FbGraph2
class User < Node
include AttributeAssigner
include Edge::Accounts
include Edge::Friends
register_attributes(
@ -18,6 +21,7 @@ module FbGraph2
def initialize(id, attributes = {})
super
assign attributes
# TODO: handle custom attributes.
end

View file

@ -0,0 +1,8 @@
require 'spec_helper'
describe FbGraph2::User do
it do
p FbGraph2::User, FbGraph2::User.registered_attributes
p FbGraph2::Page, FbGraph2::Page.registered_attributes
end
end