Class: Trusty::Utilities::MethodName
- Inherits:
-
Object
- Object
- Trusty::Utilities::MethodName
- Defined in:
- lib/trusty/utilities/method_name.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#convention ⇒ Object
readonly
Returns the value of attribute convention.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #base_value_for(target, *args, &block) ⇒ Object
- #boolean? ⇒ Boolean
- #define_for(target, options = { :on => :all }) ⇒ Object
-
#initialize(name) ⇒ MethodName
constructor
A new instance of MethodName.
- #modifier? ⇒ Boolean
-
#named? ⇒ Boolean
has a name and is not shorthand (e.g. “[]”, “[]=”, or “<<” type method).
-
#shorthand? ⇒ Boolean
has no name and is probably just “[]”, “[]=”, or “<<” type method.
-
#special? ⇒ Boolean
indicates that it’s not a vanilla method name.
- #value_for(target, *args, &block) ⇒ Object
Constructor Details
#initialize(name) ⇒ MethodName
Returns a new instance of MethodName.
7 8 9 10 11 |
# 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 |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
5 6 7 |
# File 'lib/trusty/utilities/method_name.rb', line 5 def base @base end |
#convention ⇒ Object (readonly)
Returns the value of attribute convention.
5 6 7 |
# File 'lib/trusty/utilities/method_name.rb', line 5 def convention @convention end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/trusty/utilities/method_name.rb', line 5 def name @name end |
Instance Method Details
#base_value_for(target, *args, &block) ⇒ Object
44 45 46 |
# 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? ⇒ Boolean
28 29 30 |
# File 'lib/trusty/utilities/method_name.rb', line 28 def boolean? convention == '?' end |
#define_for(target, options = { :on => :all }) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/trusty/utilities/method_name.rb', line 48 def define_for(target, = { :on => :all }) class_result = define_with_method :define_method, target unless [:on] == :instance instance_result = define_with_method :define_singleton_method, target unless [:on] == :class # indicate if a method was defined class_result == true || instance_result == true end |
#modifier? ⇒ Boolean
32 33 34 |
# File 'lib/trusty/utilities/method_name.rb', line 32 def modifier? convention == '!' end |
#named? ⇒ Boolean
has a name and is not shorthand (e.g. “[]”, “[]=”, or “<<” type method)
19 20 21 |
# File 'lib/trusty/utilities/method_name.rb', line 19 def named? base != nil end |
#shorthand? ⇒ Boolean
has no name and is probably just “[]”, “[]=”, or “<<” type method
24 25 26 |
# File 'lib/trusty/utilities/method_name.rb', line 24 def shorthand? !named? end |
#special? ⇒ Boolean
indicates that it’s not a vanilla method name
14 15 16 |
# File 'lib/trusty/utilities/method_name.rb', line 14 def special? base == nil || convention != nil end |
#value_for(target, *args, &block) ⇒ Object
36 37 38 39 40 41 42 |
# 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 |