-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the implementation of virtus with a combined getter and setter.
- Loading branch information
Showing
2 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,24 +1,108 @@ | ||
module ClassOptions | ||
module Options | ||
def options(*names) | ||
names.each do |option| | ||
create_option(option) | ||
# Returns default options hash for a given attribute class | ||
# | ||
# @example | ||
# Virtus::Attribute::String.options | ||
# # => {:primitive => String} | ||
# | ||
# @return [Hash] | ||
# a hash of default option values | ||
# | ||
# @api public | ||
def options | ||
accepted_options.each_with_object({}) do |option_name, options| | ||
option_value = send(option_name) | ||
options[option_name] = option_value unless option_value.nil? | ||
end | ||
end | ||
|
||
protected | ||
# Returns an array of valid options | ||
# | ||
# @example | ||
# Virtus::Attribute::String.accepted_options | ||
# # => [:primitive, :accessor, :reader, :writer] | ||
# | ||
# @return [Array] | ||
# the array of valid option names | ||
# | ||
# @api public | ||
def accepted_options | ||
@accepted_options ||= [] | ||
end | ||
|
||
# Defines which options are valid for a given attribute class | ||
# | ||
# @example | ||
# class MyAttribute < Virtus::Attribute::Object | ||
# accept_options :foo, :bar | ||
# end | ||
# | ||
# @return [self] | ||
# | ||
# @api public | ||
def accept_options(*new_options) | ||
add_accepted_options(new_options) | ||
new_options.each { |option| define_option_method(option) } | ||
# descendants.each { |descendant| descendant.add_accepted_options(new_options) } | ||
self | ||
end | ||
|
||
def create_option(option) | ||
protected | ||
|
||
# Adds a reader/writer method for the give option name | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def define_option_method(option) | ||
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | ||
def self.#{option}=(value = ClassOptions::Undefined) | ||
@#{option} = value | ||
self | ||
end | ||
def self.#{option} | ||
@#{option} ||= ClassOptions::Undefined | ||
end | ||
def self.#{option}(value = Undefined) # def self.primitive(value = Undefined) | ||
return @#{option} || Undefined if value.equal?(Undefined) # return @primitive if value.equal?(Undefined) | ||
@#{option} = value # @primitive = value | ||
self # self | ||
end # end | ||
RUBY | ||
end | ||
|
||
# Sets default options | ||
# | ||
# @param [#each] new_options | ||
# options to be set | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
def set_options(new_options) | ||
new_options.each { |pair| send(*pair) } | ||
self | ||
end | ||
|
||
# Adds new options that an attribute class can accept | ||
# | ||
# @param [#to_ary] new_options | ||
# new options to be added | ||
# | ||
# @return [self] | ||
# | ||
# @api private | ||
def add_accepted_options(new_options) | ||
accepted_options.concat(new_options) | ||
self | ||
end | ||
|
||
private | ||
|
||
# Adds descendant to descendants array and inherits default options | ||
# | ||
# @param [Class] descendant | ||
# | ||
# @return [undefined] | ||
# | ||
# @api private | ||
def inherited(descendant) | ||
super | ||
descendant.add_accepted_options(accepted_options).set_options(options) | ||
end | ||
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