Skip to content

Commit

Permalink
Bring in module attribute_accessors from motion-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Osterwood committed Feb 18, 2016
1 parent d050689 commit c423d75
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
File renamed without changes.
11 changes: 2 additions & 9 deletions 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|
Expand Down
72 changes: 72 additions & 0 deletions 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
11 changes: 2 additions & 9 deletions 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))
Expand Down
2 changes: 1 addition & 1 deletion lib/yapper/watch.rb
@@ -1,4 +1,4 @@
motion_require 'attribute_accessors'
motion_require 'class_attribute_accessors'

class Yapper::Watch
cattr_accessor :_observer
Expand Down

0 comments on commit c423d75

Please sign in to comment.