Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

~ fix configurable

  • Loading branch information...
commit 109214f9eaa20e176bf6255bc6f98a994d5097fa 1 parent 62a6869
Andrew Zinenko authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- vk-ruby (0.8.5)
+ vk-ruby (0.9.0)
faraday (~> 0.8.0)
faraday_middleware (~> 0.8.8)
multi_json (~> 1.3.6)
View
31 lib/vk-ruby/configurable.rb
@@ -7,26 +7,39 @@ def attr_configurable(*arr)
params = arr.pop if arr.last.is_a?(Hash)
arr.each do |a_name|
+
+ attr_writer a_name.to_sym
+
define_method(a_name) do
- instance_variable_set(:"@#{a_name}", params[:default]) if !instance_variable_get(:"@#{a_name}") && params && params[:default]
+
+ if !instance_variable_get(:"@#{a_name}") && !instance_variable_get(:"@set_default_value_#{a_name}") && params && params[:default]
+ instance_variable_set(:"@#{a_name}", params[:default])
+ instance_variable_set(:"@set_default_value_#{a_name}", true)
+ end
if variable = instance_variable_get(:"@#{a_name}")
variable
- elsif self.class.const_defined?(a_name.upcase, true)
- self.class.const_get(a_name.upcase, true)
+ elsif constant = self.class.try_const_get(a_name.upcase)
+ constant
end
- end
- define_method("#{a_name}=") do |value|
- instance_variable_set(:"@#{a_name}", value)
end
end
-
end
- def extended(cls)
- cls.include self
+ def try_const_get(const_name)
+ self.name.split('::').inject([]) do |result, name|
+ result << eval([result.last, name].join('::'))
+ result
+ end.inject(nil) do |result, clas|
+ begin
+ value = clas.const_get(const_name)
+ break value
+ rescue NameError
+ nil
+ end
+ end
end
end
View
10 lib/vk-ruby/serverside.rb
@@ -22,17 +22,17 @@ def initialize(params = {})
def authorize(code, auto_save = true)
params = {host: 'https://oauth.vk.com',
- client_id: self.app_id,
- client_secret: self.app_secret,
- code: code,
+ client_id: self.app_id,
+ client_secret: self.app_secret,
+ code: code,
verb: :get}
response = request("/access_token", params)
raise VK::AuthorizeException.new(response) if response.body['error']
- response.body['response'].each{|k,v| instance_variable_set(:"@#{k}", v) } if auto_save
+ response.body.each{|k,v| instance_variable_set(:"@#{k}", v) } if auto_save
- response.body['response']
+ response.body
end
end
View
39 tests/configurable.rb
@@ -1,39 +1,52 @@
require File.expand_path('../helpers', __FILE__)
-class Application
- extend VK::Configurable
+module Test
+ A = 'A'
+ B = 'B'
+ D = 'D'
- A = 'a'
- C = 'c'
+ class Application
+ extend VK::Configurable
- attr_configurable :a, :b
- attr_configurable :c, :d, default: :default_value
+ C = 'C'
- def initialize(params={})
- params.each{|key, value| send("#{key}=", value) }
+ attr_configurable :a, :b
+ attr_configurable :c, :d, default: :default_value
+
+ def initialize(params={})
+ params.each{|key, value| send("#{key}=", value) }
+ end
end
end
class TestConfigurable < MiniTest::Unit::TestCase
def setup
- @application = Application.new(a: 1, d: 2)
+ @application = Test::Application.new(a: 1, d: 2)
end
def test_a
- assert_equal 1, @application.a
+ assert_equal @application.a, 1
+ @application.a = :new_value
+ assert_equal @application.a, :new_value
end
def test_b
- assert_equal nil, @application.b
+ assert_equal @application.b, 'B'
+ @application.b = :new_value
+ assert_equal @application.b, :new_value
end
def test_c
- assert_equal :default_value, @application.c
+ assert_equal @application.c, :default_value
+ @application.c = :new_value
+ assert_equal @application.c, :new_value
end
def test_d
- assert_equal 2, @application.d
+ assert_equal @application.d, 2
+ @application.d = :new_value
+ assert_equal @application.d, :new_value
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.