Class: Hyperspeed::DefinitionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperspeed.rb

Overview

rubocop:enable Metrics/AbcSize

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag_or_method, *args) ⇒ Object

rubocop:disable Style/MethodMissingSuper



50
51
52
53
54
55
56
# File 'lib/hyperspeed.rb', line 50

def method_missing(tag_or_method, *args)
  if @self_before_instance_eval.respond_to?(tag_or_method, _include_private_methods = true)
    @self_before_instance_eval.send(tag_or_method, *args)
  else
    build_ast(tag_or_method, *args)
  end
end

Instance Method Details

#build_ast(tag, properties_or_children_or_text = nil, children_or_text = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyperspeed.rb', line 64

def build_ast(tag, properties_or_children_or_text = nil, children_or_text = nil)
  definition = {
    type: :ELEMENT,
    tag: tag
  }
  if properties_or_children_or_text && children_or_text
    if children_or_text.is_a?(Array)
      unless properties_or_children_or_text.is_a?(Hash)
        fail Error, 'when `children` are defined as second argument, first argument must be a `properties` Hash'
      end

      definition.merge(properties: properties_or_children_or_text,
                       children: children_or_text)
    elsif children_or_text.is_a?(String)
      unless properties_or_children_or_text.is_a?(Hash)
        fail Error, 'when `text` is defined as second argument, first argument must be a `properties` Hash'
      end

      definition.merge(properties: properties_or_children_or_text,
                       children: [{
                         type: :TEXT,
                         value: children_or_text
                       }])
    else
      # rubocop:disable Layout/LineLength
      fail Error, 'when first argument is a `properties` Hash, second argument must be either `Array` of `children` or a `String`'
      # rubocop:enable Layout/LineLength
    end
  elsif properties_or_children_or_text.is_a?(Hash)
    definition.merge(properties: properties_or_children_or_text)
  elsif properties_or_children_or_text.is_a?(Array)
    definition.merge(children: properties_or_children_or_text)
  elsif properties_or_children_or_text.is_a?(String)
    definition.merge(children: [{
                       type: :TEXT,
                       value: properties_or_children_or_text
                     }])
  elsif properties_or_children_or_text.nil?
    definition
  else
    fail Error, 'first argument must be a `properties` Hash, `children` Array, text `String`, or empty'
  end
end

#evaluate(&block) ⇒ Object



40
41
42
43
# File 'lib/hyperspeed.rb', line 40

def evaluate(&block)
  @self_before_instance_eval = eval('self', block.binding, __FILE__, __LINE__)
  instance_eval(&block)
end

#p(*args) ⇒ Object



45
46
47
# File 'lib/hyperspeed.rb', line 45

def p(*args)
  build_ast(:p, *args)
end

#respond_to_missing?(_tag_or_method, *_args) ⇒ Boolean

rubocop:enable Style/MethodMissingSuper

Returns:

  • (Boolean)


59
60
61
# File 'lib/hyperspeed.rb', line 59

def respond_to_missing?(_tag_or_method, *_args)
  true
end