Permalink
Browse files

more improvements in Resource

* support @@associations declaration
* support AM::Dirty attribute_will_change!
* generate methods for associations
  • Loading branch information...
1 parent 6d72d63 commit 9790273b6bb5c295cd0b827be6bacc6d138bde3d @farnoy committed Jan 21, 2012
Showing with 12 additions and 5 deletions.
  1. +4 −3 bin/api-browser.rb
  2. +8 −2 lib/github-api-client/resource.rb
View
@@ -7,11 +7,12 @@
class User
@@attributes = {login: String, name: String, has_repos: true, location: String}
@@pushables = [:name, :location]
+ @@associations = {repositories: [nil, -> { has_many :repositories, :class_name => Repo}]}
include Resource
end
u = User.new
u.name = 'Kuba'
-p u.login
-p u.has_repos = true
-p u.has_repos?
+u.attributes[:has_repos] = true
+u.save
+p u.repositories
@@ -12,6 +12,7 @@ module Resource
define_method :initialize do
instance_variable_set(:@attributes, {})
+ instance_variable_set(:@changed_attributes, {})
end
class_variable_get(:@@attributes).each_pair do |key, value|
@@ -20,11 +21,16 @@ module Resource
return self.instance_variable_get(:@attributes)[key]
end
define_method "#{key}=" do |o|
- p self.methods
- send(:"#{key}_will_change!") unless o == self.instance_variable_get(:@attributes)[key]
+ send(:attribute_will_change!, key) unless o == self.instance_variable_get(:@attributes)[key]
return instance_variable_get(:@attributes)[key] = o
end if class_variable_get(:@@pushables).include? key
end
+
+ class_variable_get(:@@associations).each_pair do |key, value|
+ define_method key do
+ return ::GitHub::Fetchers.const_get(self.class).send(:"association_#{key}")
+ end
+ end
define_method :save do
@changed_attributes.clear

0 comments on commit 9790273

Please sign in to comment.