From c423d758989dc8c2b436a77ce7bfa8af6f67315c Mon Sep 17 00:00:00 2001 From: Chris Osterwood Date: Thu, 18 Feb 2016 14:50:48 -0500 Subject: [PATCH] Bring in module attribute_accessors from motion-support --- ...essors.rb => class_attribute_accessors.rb} | 0 lib/yapper/config.rb | 11 +-- lib/yapper/module_attribute_accessors.rb | 72 +++++++++++++++++++ lib/yapper/settings.rb | 11 +-- lib/yapper/watch.rb | 2 +- 5 files changed, 77 insertions(+), 19 deletions(-) rename lib/yapper/{attribute_accessors.rb => class_attribute_accessors.rb} (100%) create mode 100644 lib/yapper/module_attribute_accessors.rb diff --git a/lib/yapper/attribute_accessors.rb b/lib/yapper/class_attribute_accessors.rb similarity index 100% rename from lib/yapper/attribute_accessors.rb rename to lib/yapper/class_attribute_accessors.rb diff --git a/lib/yapper/config.rb b/lib/yapper/config.rb index 8c428eb..63d3311 100644 --- a/lib/yapper/config.rb +++ b/lib/yapper/config.rb @@ -1,18 +1,11 @@ motion_require 'yapper' +motion_require 'module_attribute_accessors' module Yapper::Config COLLECTION = '_config' extend self - @@db_version = nil unless defined? @@db_version - - def self.db_version - @@db_version || 0 - end - - def self.db_version=(db_version) - @@db_version = db_version - end + mattr_accessor :db_version def self.get(key) Yapper::DB.instance.execute do |txn| diff --git a/lib/yapper/module_attribute_accessors.rb b/lib/yapper/module_attribute_accessors.rb new file mode 100644 index 0000000..9162c7a --- /dev/null +++ b/lib/yapper/module_attribute_accessors.rb @@ -0,0 +1,72 @@ +########################################################################### +# Pulled from motion-support at: +# https://github.com/rubymotion/motion-support +# +# There was a conflict with motion-require, and Yapper only needs the +# concern portion of motion-support. +########################################################################### + +class Module + def mattr_reader(*syms) + receiver = self + options = syms.extract_options! + syms.each do |sym| + raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/ + class_exec do + unless class_variable_defined?("@@#{sym}") + class_variable_set("@@#{sym}", nil) + end + + define_singleton_method sym do + class_variable_get("@@#{sym}") + end + end + + unless options[:instance_reader] == false || options[:instance_accessor] == false + class_exec do + define_method sym do + receiver.class_variable_get("@@#{sym}") + end + end + end + end + end + + def mattr_writer(*syms) + receiver = self + options = syms.extract_options! + syms.each do |sym| + raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/ + class_exec do + define_singleton_method "#{sym}=" do |obj| + class_variable_set("@@#{sym}", obj) + end + end + + unless options[:instance_writer] == false || options[:instance_accessor] == false + class_exec do + define_method "#{sym}=" do |obj| + receiver.class_variable_set("@@#{sym}", obj) + end + end + end + end + end + + # Extends the module object with module and instance accessors for class attributes, + # just like the native attr* accessors for instance attributes. + # + # module AppConfiguration + # mattr_accessor :google_api_key + # + # self.google_api_key = "123456789" + # end + # + # AppConfiguration.google_api_key # => "123456789" + # AppConfiguration.google_api_key = "overriding the api key!" + # AppConfiguration.google_api_key # => "overriding the api key!" + def mattr_accessor(*syms) + mattr_reader(*syms) + mattr_writer(*syms) + end +end \ No newline at end of file diff --git a/lib/yapper/settings.rb b/lib/yapper/settings.rb index 7d64e1c..b8272b6 100644 --- a/lib/yapper/settings.rb +++ b/lib/yapper/settings.rb @@ -1,18 +1,11 @@ motion_require 'yapper' +motion_require 'module_attribute_accessors' module Yapper::Settings PREFIX = 'yapper-' extend self - @@db_version = nil unless defined? @@db_version - - def self.db_version - @@db_version || 0 - end - - def self.db_version=(db_version) - @@db_version = db_version - end + mattr_accessor :db_version def get(key) value = storage.objectForKey(storage_key(key)) diff --git a/lib/yapper/watch.rb b/lib/yapper/watch.rb index 33b5fc8..c22bde4 100644 --- a/lib/yapper/watch.rb +++ b/lib/yapper/watch.rb @@ -1,4 +1,4 @@ -motion_require 'attribute_accessors' +motion_require 'class_attribute_accessors' class Yapper::Watch cattr_accessor :_observer