Class: Trusty::Utilities::MethodName

Inherits:
Object
  • Object
show all
Defined in:
lib/trusty/utilities/method_name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/trusty/utilities/method_name.rb', line 5

def base
  @base
end

#conventionObject (readonly)

Returns the value of attribute convention.



5
6
7
# File 'lib/trusty/utilities/method_name.rb', line 5

def convention
  @convention
end

#nameObject (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

Returns:

  • (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, 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?Boolean

Returns:

  • (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)

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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