class Trusty::Utilities::MethodName
Attributes
base[R]
convention[R]
name[R]
Public Class Methods
new(name)
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 7 def initialize(name) @name = name # returns a method name and its ending (e.g. "config?" returns ["config", "?"]) @base, @convention = name.to_s.match(/\A(\w+?)(\W+)?\Z/).to_a.slice(1, 2) end
Public Instance Methods
base_value_for(target, *args, &block)
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 44 def base_value_for(target, *args, &block) method_value_for(target, base, *args, &block) end
boolean?()
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 28 def boolean? convention == '?' end
define_for(target, options = { :on => :all })
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 48 def define_for(target, options = { :on => :all }) class_result = define_with_method :define_method, target unless options[:on] == :instance instance_result = define_with_method :define_singleton_method, target unless options[:on] == :class # indicate if a method was defined class_result == true || instance_result == true end
modifier?()
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 32 def modifier? convention == '!' end
named?()
click to toggle source
has a name and is not shorthand (e.g. “[]”, “[]=”, or “<<” type method)
# File lib/trusty/utilities/method_name.rb, line 19 def named? base != nil end
shorthand?()
click to toggle source
has no name and is probably just “[]”, “[]=”, or “<<” type method
# File lib/trusty/utilities/method_name.rb, line 24 def shorthand? !named? end
special?()
click to toggle source
indicates that it's not a vanilla method name
# File lib/trusty/utilities/method_name.rb, line 14 def special? base == nil || convention != nil end
value_for(target, *args, &block)
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 36 def value_for(target, *args, &block) if target.respond_to?(name) || !target.respond_to?(base) method_value_for(target, name, *args, &block) else base_value_for(target, *args, &block) end end
Private Instance Methods
define_with_method(define_method_name, target)
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 58 def define_with_method(define_method_name, target) # create helper method that sees if a config is blank if boolean? && target.respond_to?(define_method_name) helper = self target.send define_method_name, name do |*args, &block| helper.base_value_for(self, *args, &block) end true else false end end
method_value_for(target, method_name, *args, &block)
click to toggle source
# File lib/trusty/utilities/method_name.rb, line 72 def method_value_for(target, method_name, *args, &block) value = target.send(method_name, *args, &block) if boolean? value != nil && (!value.respond_to?(:empty?) || !value.empty?) else value end end