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

refactor attribute registration logic

This commit is contained in:
nov 2014-05-05 19:19:45 +09:00
parent 3b3fcc5614
commit 9966baddcb
5 changed files with 33 additions and 18 deletions

View file

@ -3,10 +3,13 @@ module FbGraph2
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
extend ClassMethods
attr_accessor :raw_attributes
cattr_accessor :registered_attributes cattr_accessor :registered_attributes
end end
def assign(attributes) def assign(attributes)
self.raw_attributes = attributes
self.class.registered_attributes.each do |type, keys| self.class.registered_attributes.each do |type, keys|
keys.each do |key| keys.each do |key|
raw = attributes[key] raw = attributes[key]
@ -18,13 +21,28 @@ module FbGraph2
Date.parse raw Date.parse raw
when :time when :time
Time.parse raw Time.parse raw
when :page
Page.new raw[:id], raw
when :pages
raw.each do |_raw_|
Page.new _raw_[:id], _raw_
end
when :user
User.new raw[:id], raw
when :custom when :custom
# TODO: # NOTE: handle custom attributes in each class
end end
self.send :"#{key}=", attributes[key] self.send :"#{key}=", attributes[key]
end end
end end
end end
end end
module ClassMethods
def register_attributes(attributes)
self.registered_attributes = attributes
send :attr_accessor, *attributes.values.flatten
end
end
end end
end end

View file

@ -4,9 +4,7 @@ module FbGraph2
def friends(params = {}) def friends(params = {})
users = self.edge :friends, params users = self.edge :friends, params
users.collect do |user| users.collect do |user|
User.new(user[:id], user).authenticate( User.new(user[:id], user).authenticate self.access_token
params[:access_token] || self.access_token
)
end end
end end
end end

View file

@ -5,7 +5,7 @@ module FbGraph2
def initialize(id, attributes = {}) def initialize(id, attributes = {})
self.id = id self.id = id
self.raw_attributes = attributes assign attributes
end end
def authenticate(access_token) def authenticate(access_token)

View file

@ -1,6 +1,6 @@
module FbGraph2 module FbGraph2
class Page < Node class Page < Node
self.registered_attributes = { register_attributes(
raw: [ raw: [
:about, :attire, :band_members, :booking_agent, :can_post, :category, :checkins, :company_overview, :about, :attire, :band_members, :booking_agent, :can_post, :category, :checkins, :company_overview,
:current_location, :description, :directed_by, :founded, :general_info, :general_manager, :hometown, :current_location, :description, :directed_by, :founded, :general_info, :general_manager, :hometown,
@ -8,16 +8,16 @@ module FbGraph2
:products, :talking_about_count, :username, :website, :were_here_count :products, :talking_about_count, :username, :website, :were_here_count
], ],
date: [:birthday], date: [:birthday],
page: [:best_page],
custom: [ custom: [
:best_page, :category_list, :cover, :context, :hours, :location, :parking, :price_range, :restaurant_services, :category_list, :cover, :context, :hours, :location, :parking, :price_range, :restaurant_services,
:restaurant_specialties :restaurant_specialties
] ]
} )
attr_accessor *registered_attributes.values.flatten
def initialize(id, attributes = {}) def initialize(id, attributes = {})
super super
assign attributes # TODO: handle custom attributes.
end end
def self.me(access_token) def self.me(access_token)

View file

@ -2,7 +2,7 @@ module FbGraph2
class User < Node class User < Node
include Edge::Friends include Edge::Friends
self.registered_attributes = { register_attributes(
raw: [ raw: [
:about, :bio, :email, :first_name, :gender, :installed, :is_verified, :link, :locale, :about, :bio, :email, :first_name, :gender, :installed, :is_verified, :link, :locale,
:middle_name, :name, :name_format, :political, :quotes, :relationship_status, :religion, :middle_name, :name, :name_format, :political, :quotes, :relationship_status, :religion,
@ -10,16 +10,15 @@ module FbGraph2
], ],
time: [:updated_time], # NOTE: undocumented attribute time: [:updated_time], # NOTE: undocumented attribute
date: [:birthday], date: [:birthday],
custom: [ page: [:hometown, :location],
:age_range, :context, :cover, :currency, :education, :favorite_athletes, :favorite_teams, pages: [:favorite_athletes, :favorite_teams, :inspirational_people, :languages],
:hometown, :inspirational_people, :languages, :location, :significant_other, :work user: [:significant_other],
] custom: [:age_range, :context, :cover, :currency, :education, :work]
} )
attr_accessor *registered_attributes.values.flatten
def initialize(id, attributes = {}) def initialize(id, attributes = {})
super super
assign attributes # TODO: handle custom attributes.
end end
def self.me(access_token) def self.me(access_token)