Class: SlimLint::Atom
- Inherits:
-
Object
- Object
- SlimLint::Atom
- Defined in:
- lib/slim_lint/atom.rb
Overview
Instance Attribute Summary collapse
-
#finish ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
-
#start ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
-
#value ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns whether this atom is equivalent to another object.
-
#initialize(value, pos:) ⇒ Atom
constructor
Creates an atom from the specified value.
-
#inspect ⇒ String
Displays a string representation of this Atom suitable for debugging.
- #line ⇒ Object
- #location ⇒ Object
-
#match?(pattern) ⇒ Boolean
Returns whether this atom matches the given Sexp pattern.
-
#method_missing(method_sym, *args) { ... } ⇒ Object
Redirect methods to the value this Atom wraps.
-
#respond_to?(method_sym, include_private = false) ⇒ Boolean
Return whether this Atom or the value it wraps responds to the given message.
- #respond_to_missing?(method_name, *args) ⇒ Boolean
- #to_array ⇒ Object
-
#to_s ⇒ String
Displays the string representation the value this Atom wraps.
Constructor Details
#initialize(value, pos:) ⇒ Atom
Creates an atom from the specified value.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/slim_lint/atom.rb', line 16 def initialize(value, pos:) @value = value @start = pos if value.is_a?(String) lines = value.count("\n") chars = 0 chars = value.lines.last.size unless value.empty? @finish = [pos[0] + lines, chars + (lines == 0 ? pos[1] : 0)] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args) { ... } ⇒ Object
Redirect methods to the value this SlimLint::Atom wraps.
Again, this is for convenience so we don’t need to manually unwrap the value ourselves. It’s pretty magical, but results in much DRYer code.
101 102 103 104 105 106 107 |
# File 'lib/slim_lint/atom.rb', line 101 def method_missing(method_sym, *args, &block) if @value.respond_to?(method_sym) @value.send(method_sym, *args, &block) else super end end |
Instance Attribute Details
#finish ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
11 12 13 |
# File 'lib/slim_lint/atom.rb', line 11 def finish @finish end |
#start ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
11 12 13 |
# File 'lib/slim_lint/atom.rb', line 11 def start @start end |
#value ⇒ Object
Stores the line number of the code in the original document that this Atom came from.
11 12 13 |
# File 'lib/slim_lint/atom.rb', line 11 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Returns whether this atom is equivalent to another object.
This defines a helper which unwraps the inner value of the atom to compare against a literal value, saving us having to do it ourselves everywhere else.
50 51 52 |
# File 'lib/slim_lint/atom.rb', line 50 def ==(other) @value == (other.is_a?(Atom) ? other.instance_variable_get(:@value) : other) end |
#inspect ⇒ String
Displays a string representation of this SlimLint::Atom suitable for debugging.
84 85 86 87 88 89 90 91 |
# File 'lib/slim_lint/atom.rb', line 84 def inspect range = +"" range << start.join(":") if start range << " => " if start && finish range << finish.join(":") if finish "A(#{range}) #{@value.inspect}" end |
#line ⇒ Object
29 30 31 |
# File 'lib/slim_lint/atom.rb', line 29 def line start[0] if start end |
#location ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/slim_lint/atom.rb', line 33 def location SourceLocation.new( start_line: start[0], start_column: start[1], last_line: (finish || start)[0], last_column: (finish || start)[1] ) end |
#match?(pattern) ⇒ Boolean
Returns whether this atom matches the given Sexp pattern.
This exists solely to make an SlimLint::Atom quack like a Sexp, so we don’t have to manually check the type when doing comparisons elsewhere.
61 62 63 64 65 66 67 68 |
# File 'lib/slim_lint/atom.rb', line 61 def match?(pattern) # Delegate matching logic if we're comparing against a matcher if pattern.is_a?(SlimLint::Matcher::Base) return pattern.match?(@value) end @value == pattern end |
#respond_to?(method_sym, include_private = false) ⇒ Boolean
Return whether this SlimLint::Atom or the value it wraps responds to the given message.
121 122 123 124 125 126 127 |
# File 'lib/slim_lint/atom.rb', line 121 def respond_to?(method_sym, include_private = false) if super true else @value.respond_to?(method_sym, include_private) end end |
#respond_to_missing?(method_name, *args) ⇒ Boolean
111 112 113 |
# File 'lib/slim_lint/atom.rb', line 111 def respond_to_missing?(method_name, *args) @value.__send__(:respond_to_missing?, method_name, *args) || super end |
#to_array ⇒ Object
77 78 79 |
# File 'lib/slim_lint/atom.rb', line 77 def to_array @value end |
#to_s ⇒ String
Displays the string representation the value this SlimLint::Atom wraps.
73 74 75 |
# File 'lib/slim_lint/atom.rb', line 73 def to_s @value.to_s end |