Class: Reduxco::CallableRef
- Inherits:
-
Object
- Object
- Reduxco::CallableRef
- Includes:
- Comparable
- Defined in:
- lib/reduxco/callable_ref.rb
Overview
An immutable class that represents a referrence to a callable in a CallableTable; this class is rarely used directly by clients.
Constant Summary collapse
- MIN_DEPTH =
The minimum depth number allowed.
1
- STR_SEPARATOR =
For string representations (typically used in debugging), this is used as the separator between name and depth (if depth is given).
':'
- STR_LEFT_BRACKET =
For string representations, what is the opening bracket string.
'<'
- STR_RIGHT_BRACKET =
For string representations, what is the opening bracket string.
'>'
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the depth of the reference, or nil if the reference is dynamic.
-
#name ⇒ Object
readonly
Returns the name of the refernce.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Returns the sort order of the reference.
-
#dynamic? ⇒ Boolean
Is true valued when the reference will dynamically bind to an entry in the CallableTable instead of to an entry at a specific depth.
-
#eql?(other) ⇒ Boolean
(also: #==)
Returns true if the refs are equivalent.
-
#hash ⇒ Object
Returns a unique hash value; useful resolving Hash entries.
-
#include?(other) ⇒ Boolean
Returns true if the passed ref is .
-
#initialize(name, depth = nil) ⇒ CallableRef
constructor
- name
-
Typically the name is a symbol, but systems are free to use other objects as types are not coerced into other types at any point.
-
#inspect ⇒ Object
Returns a human readable string form of this CallableReference.
-
#pred ⇒ Object
Returns a CallableRef with the same name, but one depth higher.
-
#static? ⇒ Boolean
Negation of dynamic?.
-
#succ ⇒ Object
(also: #next)
Returns a CallableRef with the same name, but one depth deeper.
-
#to_a ⇒ Object
Returns an array form of this CallableReference.
-
#to_h ⇒ Object
Returns a hash form of this CallableReference.
-
#to_s ⇒ Object
Returns a human readable string form of this CallableReference.
Constructor Details
#initialize(name, depth = nil) ⇒ CallableRef
- name
-
Typically the name is a symbol, but systems are free to use other objects as types are not coerced into other types at any point. If the name is a CallableRef, then this acts as a copy constructor.
- depth
-
The depth is normally not given when used, can be specified for referencing specific shadowed callables when callables are flattend into a CallableTable; this is important for calls to super.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/reduxco/callable_ref.rb', line 27 def initialize(name, depth=nil) case name when self.class @name = name.name @depth = (depth && depth.to_i) || name.depth else @name = name @depth = depth && depth.to_i end raise IndexError, "Depth must be greater than zero", caller if depth && depth<MIN_DEPTH end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the depth of the reference, or nil if the reference is dynamic.
44 45 46 |
# File 'lib/reduxco/callable_ref.rb', line 44 def depth @depth end |
#name ⇒ Object (readonly)
Returns the name of the refernce.
41 42 43 |
# File 'lib/reduxco/callable_ref.rb', line 41 def name @name end |
Instance Method Details
#<=>(other) ⇒ Object
Returns the sort order of the reference. This is primarily useed for sorting references in CallableTable so that shadowed callables are called properly.
Static references are sorted by the following rule: For all sets of static refs with equal names, sort by depth. For all sets of static refs with equal depths, only sort if the names are sortable. This means that there is no requirement for sort order to group by name or by depth, and so no software should be written around an assumption of which comes first.
Refuses to sort dynamic references, as they are not ordered compared to static references.
112 113 114 115 116 117 118 119 |
# File 'lib/reduxco/callable_ref.rb', line 112 def <=>(other) if( dynamic? != other.dynamic? ) nil else depth_eql = depth <=> other.depth (depth_eql==0 ? (name <=> other.name) : nil) || depth_eql end end |
#dynamic? ⇒ Boolean
Is true valued when the reference will dynamically bind to an entry in the CallableTable instead of to an entry at a specific depth.
48 49 50 |
# File 'lib/reduxco/callable_ref.rb', line 48 def dynamic? return depth.nil? end |
#eql?(other) ⇒ Boolean Also known as: ==
Returns true if the refs are equivalent.
90 91 92 93 94 95 96 |
# File 'lib/reduxco/callable_ref.rb', line 90 def eql?(other) if( other.kind_of?(CallableRef) || (other.respond_to?(:name) && other.respond_to?(:depth)) ) other.name == self.name && other.depth == self.depth else false end end |
#hash ⇒ Object
Returns a unique hash value; useful resolving Hash entries.
77 78 79 |
# File 'lib/reduxco/callable_ref.rb', line 77 def hash @hash ||= self.to_a.hash end |
#include?(other) ⇒ Boolean
Returns true if the passed ref is
This method raises an exception when compared to anything that does not ducktype as a reference.
85 86 87 |
# File 'lib/reduxco/callable_ref.rb', line 85 def include?(other) other.name == self.name && (dynamic? ? true : other.depth == self.depth) end |
#inspect ⇒ Object
Returns a human readable string form of this CallableReference.
137 138 139 |
# File 'lib/reduxco/callable_ref.rb', line 137 def inspect to_s end |
#pred ⇒ Object
Returns a CallableRef with the same name, but one depth higher.
68 69 70 71 72 73 74 |
# File 'lib/reduxco/callable_ref.rb', line 68 def pred if( dynamic? ) raise RuntimeError, "Dynamic references cannot undergo relative movement." else self.class.new(name, depth.pred) end end |
#static? ⇒ Boolean
Negation of dynamic?
53 54 55 |
# File 'lib/reduxco/callable_ref.rb', line 53 def static? return !dynamic? end |
#succ ⇒ Object Also known as: next
Returns a CallableRef with the same name, but one depth deeper.
58 59 60 61 62 63 64 |
# File 'lib/reduxco/callable_ref.rb', line 58 def succ if( dynamic? ) raise RuntimeError, "Dynamic references cannot undergo relative movement." else self.class.new(name, depth.succ) end end |
#to_a ⇒ Object
Returns an array form of this CallableReference.
122 123 124 |
# File 'lib/reduxco/callable_ref.rb', line 122 def to_a @array ||= [name, depth] end |
#to_h ⇒ Object
Returns a hash form of this CallableReference.
127 128 129 |
# File 'lib/reduxco/callable_ref.rb', line 127 def to_h @hash ||= {name:name, depth:depth} end |
#to_s ⇒ Object
Returns a human readable string form of this CallableReference.
132 133 134 |
# File 'lib/reduxco/callable_ref.rb', line 132 def to_s @string ||= STR_LEFT_BRACKET + self.to_a.compact.map {|prop| prop.to_s}.join(STR_SEPARATOR) + STR_RIGHT_BRACKET end |