forked from kareemk/yapper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring in module attribute_accessors from motion-support
- Loading branch information
Chris Osterwood
committed
Feb 18, 2016
1 parent
d050689
commit c423d75
Showing
5 changed files
with
77 additions
and
19 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters