Permalink
Browse files

+ gem attr_configurable

  • Loading branch information...
1 parent a295164 commit 5a511b864c184138923984904f5a0fce471950f9 Andrew Zinenko committed Sep 24, 2012
View
@@ -2,6 +2,7 @@ PATH
remote: .
specs:
vk-ruby (0.9.0)
+ attr_configurable (~> 0.1.0)
faraday (~> 0.8.0)
faraday_middleware (~> 0.8.8)
multi_json (~> 1.3.6)
@@ -12,6 +13,7 @@ GEM
remote: http://rubygems.org/
specs:
addressable (2.3.2)
+ attr_configurable (0.1.0)
cookiejar (0.3.0)
crack (0.3.1)
em-http-request (1.0.3)
View
@@ -11,6 +11,7 @@
require 'cgi'
require 'forwardable'
require 'openssl'
+require 'attr_configurable'
# Register multi_json parser.
FaradayMiddleware::ParseJson.define_parser do |body|
@@ -19,4 +20,4 @@
%w(validate_utf normalize_utf vk_logger).each{|lib| require "vk-ruby/middleware/response/#{lib}"}
-%w(configurable core upload secure serverside standalone vk_exception version).each{|lib| require "vk-ruby/#{lib}"}
+%w(core upload secure serverside standalone vk_exception version).each{|lib| require "vk-ruby/#{lib}"}
@@ -1,75 +0,0 @@
-# encoding: UTF-8
-
-module VK
- module Configurable
-
- # Constant based configuration.
- #
- # @example
- # module Test
- # A = 'A'
- # B = 'B'
- # D = 'D'
- #
- # class Application
- # extend VK::Configurable
- #
- # C = 'C'
- #
- # attr_configurable :a, :b
- # attr_configurable :c, :d, default: :default_value
- #
- # def initialize(params={})
- # params.each{|key, value| send("#{key}=", value) }
- # end
- # end
- # end
- #
- # @application = Test::Application.new(a: 1, d: 2)
- # @application.a # => 1
- # @application.b # => 'B'
- # @application.c # => :default_value
- # @application.d # => 2
- #
-
- 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
-
- 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 constant = self.class.try_const_get(a_name.upcase)
- constant
- end
-
- end
-
- end
- end
-
- 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
-end
View
@@ -2,7 +2,7 @@
module VK
module Core
- extend Configurable
+ extend ::Configurable
extend Forwardable
# A customized stack of Faraday middleware that will be used to make each request.
View
@@ -4,7 +4,7 @@ class VK::Secure
include VK::Core
include Transformer
- extend ::VK::Configurable
+ extend ::Configurable
# Application secret that will be used to make authorize request.
# @method app_secret
@@ -4,7 +4,7 @@ class VK::Serverside
include VK::Core
include Transformer
- extend ::VK::Configurable
+ extend ::Configurable
# The duration of the token after authorization
attr_accessor :expires_in
@@ -4,7 +4,7 @@ class VK::Standalone
include VK::Core
include Transformer
- extend ::VK::Configurable
+ extend ::Configurable
attr_accessor :expires_in
View
@@ -1,52 +0,0 @@
-require File.expand_path('../helpers', __FILE__)
-
-module Test
- A = 'A'
- B = 'B'
- D = 'D'
-
- class Application
- extend VK::Configurable
-
- C = 'C'
-
- 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 = Test::Application.new(a: 1, d: 2)
- end
-
- def test_a
- assert_equal @application.a, 1
- @application.a = :new_value
- assert_equal @application.a, :new_value
- end
-
- def test_b
- assert_equal @application.b, 'B'
- @application.b = :new_value
- assert_equal @application.b, :new_value
- end
-
- def test_c
- assert_equal @application.c, :default_value
- @application.c = :new_value
- assert_equal @application.c, :new_value
- end
-
- def test_d
- assert_equal @application.d, 2
- @application.d = :new_value
- assert_equal @application.d, :new_value
- end
-
-end
View
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
gem.extra_rdoc_files = ["LICENSE.txt", "README.md" ]
gem.add_runtime_dependency 'transformer', '~> 0.2.2'
+ gem.add_runtime_dependency 'attr_configurable', '~> 0.1.0'
gem.add_runtime_dependency 'multi_json', '~> 1.3.6'
gem.add_runtime_dependency 'faraday', '~> 0.8.0'
gem.add_runtime_dependency 'faraday_middleware', '~> 0.8.8'
@@ -37,6 +38,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'net-http-persistent', '2.7'
gem.require_paths = ['lib']
+ gem.executables << 'vk'
gem.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
gem.files = `git ls-files`.split("\n")

0 comments on commit 5a511b8

Please sign in to comment.