Class: SlimLint::Atom
- Inherits:
-
Object
- Object
- SlimLint::Atom
- Defined in:
- lib/slim_lint/atom.rb
Overview
Instance Attribute Summary collapse
-
#line ⇒ 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) ⇒ Atom
constructor
Creates an atom from the specified value.
-
#inspect ⇒ String
Displays a string representation of this Atom suitable for debugging.
-
#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_s ⇒ String
Displays the string representation the value this Atom wraps.
Constructor Details
#initialize(value) ⇒ Atom
Creates an atom from the specified value.
16 17 18 |
# File 'lib/slim_lint/atom.rb', line 16 def initialize(value) @value = value 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.
70 71 72 73 74 75 76 |
# File 'lib/slim_lint/atom.rb', line 70 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
#line ⇒ 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 line @line 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.
28 29 30 |
# File 'lib/slim_lint/atom.rb', line 28 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.
58 59 60 |
# File 'lib/slim_lint/atom.rb', line 58 def inspect "<#Atom #{@value.inspect}>" 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.
39 40 41 42 43 44 45 46 |
# File 'lib/slim_lint/atom.rb', line 39 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.
90 91 92 93 94 95 96 |
# File 'lib/slim_lint/atom.rb', line 90 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
80 81 82 |
# File 'lib/slim_lint/atom.rb', line 80 def respond_to_missing?(method_name, *args) @value.__send__(:respond_to_missing?, method_name, *args) || super end |
#to_s ⇒ String
Displays the string representation the value this SlimLint::Atom wraps.
51 52 53 |
# File 'lib/slim_lint/atom.rb', line 51 def to_s @value.to_s end |