Module: HoTModuLe::ShadowEffects

Defined in:
lib/hot_module/shadow_effects.rb

Defined Under Namespace

Modules: ClassMethods, JSPropertyAliases

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ void

This method returns an undefined value.

Parameters:

  • klass (Class)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hot_module/shadow_effects.rb', line 41

def self.included(klass)
  klass.attribute_binding "host-effect", :_shadow_effect_binding

  klass.singleton_class.attr_reader :directives

  klass.extend ClassMethods

  klass.class_eval do
    directive :show do |_, element, value|
      element["hidden"] = "" unless value
    end

    directive :hide do |_, element, value|
      element["hidden"] = "" if value
    end

    directive :classMap do |_, element, obj|
      obj.each do |k, v|
        element.add_class k.to_s if v
      end
    end
  end
end

Instance Method Details

#_shadow_effect_binding(attribute:, node:) ⇒ Object

rubocop:disable Metrics



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
107
108
109
110
111
# File 'lib/hot_module/shadow_effects.rb', line 65

def _shadow_effect_binding(attribute:, node:) # rubocop:disable Metrics
  syntax = attribute.value
  statements = syntax.split(";").map(&:strip)

  statements.each do |statement| # rubocop:disable Metrics
    if statement.start_with?("$el.")
      # property assignment
      expression = statement.split("=").map(&:strip)
      expression[0] = expression[0][4..]

      value = send(expression[1])

      node.send("#{expression[0]}=", value_to_attribute(value))
    elsif statement.start_with?("$")
      # directive
      directive_name, args_str = statement.strip.match(/(.*)\((.*)\)/).captures
      arg_strs = args_str.split(",").map(&:strip)
      arg_strs.unshift("$el")

      if self.class.directives[directive_name.strip[1..]]
        args = arg_strs.map do |arg_str|
          next node if arg_str == "$el"

          next arg_str[1...-1] if arg_str.start_with?("'") # string literal

          send(arg_str)
        end

        self.class.directives[directive_name.strip[1..]]&.(self, *args)
      end
    else
      # method call
      method_name, args_str = statement.strip.match(/(.*)\((.*)\)/).captures
      arg_strs = args_str.split(",").map(&:strip)

      args = arg_strs.map do |arg_str|
        next node if arg_str == "$el"

        next arg_str[1...-1] if arg_str.start_with?("'") # string literal

        send(arg_str)
      end

      send(method_name.strip, *args)
    end
  end
end