Module: SexpExtensions
- Included in:
- Sexp
- Defined in:
- lib/wherelizer/sexp_extensions.rb
Instance Method Summary collapse
- #check_type(expected_type) ⇒ Object
- #extract_assignment_target ⇒ Object
- #extract_const ⇒ Object
-
#extract_method_name ⇒ Object
Extract the method name of a method call.
-
#extract_receiver ⇒ Object
Extract the receiver of a method call.
- #extract_val ⇒ Object
- #is_type?(expected_type) ⇒ Boolean
-
#to_array ⇒ Object
Turns a sexp “array” node into an actual Ruby array of just the elements.
-
#to_hash(process_keys = true) ⇒ Object
Turn a sexp “hash” node into an actual Ruby hash.
Instance Method Details
#check_type(expected_type) ⇒ Object
8 9 10 |
# File 'lib/wherelizer/sexp_extensions.rb', line 8 def check_type(expected_type) raise "Unexpected type: #{expected_type} in #{self}" unless is_type?(expected_type) end |
#extract_assignment_target ⇒ Object
22 23 24 25 |
# File 'lib/wherelizer/sexp_extensions.rb', line 22 def extract_assignment_target check_type [:lasgn, :iasgn] self[1].to_s end |
#extract_const ⇒ Object
17 18 19 20 |
# File 'lib/wherelizer/sexp_extensions.rb', line 17 def extract_const check_type :const self[1] end |
#extract_method_name ⇒ Object
Extract the method name of a method call.
36 37 38 39 |
# File 'lib/wherelizer/sexp_extensions.rb', line 36 def extract_method_name check_type :call self[2] end |
#extract_receiver ⇒ Object
Extract the receiver of a method call.
29 30 31 32 |
# File 'lib/wherelizer/sexp_extensions.rb', line 29 def extract_receiver check_type :call self[1] end |
#extract_val ⇒ Object
12 13 14 15 |
# File 'lib/wherelizer/sexp_extensions.rb', line 12 def extract_val check_type [:lit, :str] self[1] end |
#is_type?(expected_type) ⇒ Boolean
2 3 4 5 6 |
# File 'lib/wherelizer/sexp_extensions.rb', line 2 def is_type?(expected_type) type = self[0] expected_type = [expected_type] unless expected_type.respond_to?(:include?) expected_type.include? type end |
#to_array ⇒ Object
Turns a sexp “array” node into an actual Ruby array of just the elements
57 58 59 60 |
# File 'lib/wherelizer/sexp_extensions.rb', line 57 def to_array check_type :array self[1..-1] end |
#to_hash(process_keys = true) ⇒ Object
Turn a sexp “hash” node into an actual Ruby hash. If process_keys is true, then we assume the hash keys are strings or symbols,
and extract the actual string or symbol to be the key in the resulting hash.
Otherwise, we make the key the whole sexp node that is the key.
46 47 48 49 50 51 52 53 |
# File 'lib/wherelizer/sexp_extensions.rb', line 46 def to_hash(process_keys=true) check_type :hash result = {} self[1..-1].each_slice(2) do |key, val| result[process_keys ? key.extract_val : key] = val end result end |