Class: Bluesky::PureComponent
- Inherits:
-
Object
- Object
- Bluesky::PureComponent
- Includes:
- DSL, Clearwater::CachedRender, Clearwater::Component
- Defined in:
- lib/bluesky/pure_component.rb
Overview
A presentation ‘Component`
Class Method Summary collapse
- .attribute(name, *args) ⇒ Object
- .inherited(subclass) ⇒ Object
- .install_hooks(debug = false) ⇒ Object
Instance Method Summary collapse
- #dispatch(action, *payload, &block) ⇒ Object
-
#initialize(data = {}, delegate = nil) ⇒ PureComponent
constructor
A new instance of PureComponent.
- #mount ⇒ Object
- #mounted? ⇒ Boolean
- #shallow_equal?(a, b) ⇒ Boolean
- #should_render?(previous) ⇒ Boolean
- #unmount ⇒ Object
Methods included from DSL
Constructor Details
#initialize(data = {}, delegate = nil) ⇒ PureComponent
Returns a new instance of PureComponent.
146 147 148 149 |
# File 'lib/bluesky/pure_component.rb', line 146 def initialize(data = {}, delegate = nil) @data = data @delegate = delegate end |
Class Method Details
.attribute(name, *args) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bluesky/pure_component.rb', line 122 def self.attribute(name, *args) case args.length when 0 define_method(name) do |&block| if block _data.store(name, block) block else _data.fetch(name) end end when 1 if args[0].respond_to?(:call) define_method(name) { _data.fetch(name) { _data.store(name, args[0].call) } } else define_method(name) { _data.fetch(name, args[0]) } end else raise ArgumentError, %{ wrong number of arguments (#{args.length} for 1..2) } end define_method("#{name}=") { |value| _data.store(name, value) } end |
.inherited(subclass) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bluesky/pure_component.rb', line 76 def self.inherited(subclass) DSL.send(:define_method, subclass.name) do |data = {}, delegate = nil, &block| delegate ||= @delegate component = subclass.new(data, delegate) block.call(component) if block component end @descendants << subclass end |
.install_hooks(debug = false) ⇒ Object
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 112 113 114 115 116 117 118 119 120 |
# File 'lib/bluesky/pure_component.rb', line 87 def self.install_hooks(debug=false) @descendants.each do |subclass| if subclass.instance_methods.include?(:component_did_mount) || subclass.instance_methods.include?(:component_will_unmount) subclass.class_eval { `Opal.defn(self, '$$mountable', true);` } end if subclass.instance_methods.include?(:component_will_mount) subclass.class_eval { `Opal.defn(self, '$$hook_will_mount', true);` } end subclass.send(:alias_method, :do_render, :render) unless subclass.instance_methods.include?(:do_render) subclass.send(:define_method, :render) do begin $$.console.time("#{subclass.name}:render") if debug %x{ var contents = #{do_render}; if (self.$$mountable) contents.properties.ref = self; return contents; } rescue Object => err warn err div({ class: 'broken', style: { display: :none } }, [err.]) ensure $$.console.timeEnd("#{subclass.name}:render") if debug end end end end |
Instance Method Details
#dispatch(action, *payload, &block) ⇒ Object
166 167 168 169 170 171 |
# File 'lib/bluesky/pure_component.rb', line 166 def dispatch(action, *payload, &block) warn 'Missing delegate' unless @delegate root = @delegate root = root.parent while root.respond_to?(:parent) && root.parent root.dispatch(@delegate, action, *payload, &block) end |
#mount ⇒ Object
173 174 175 |
# File 'lib/bluesky/pure_component.rb', line 173 def mount @mounted = true end |
#mounted? ⇒ Boolean
181 182 183 |
# File 'lib/bluesky/pure_component.rb', line 181 def mounted? !!@mounted end |
#shallow_equal?(a, b) ⇒ Boolean
151 152 153 154 155 156 157 158 |
# File 'lib/bluesky/pure_component.rb', line 151 def shallow_equal?(a, b) a.equal?(b) || a.each_pair.all? do |k, v| bk = b[k] v.equal?(bk) || v.eql?(bk) end rescue Object => _ false end |
#should_render?(previous) ⇒ Boolean
160 161 162 163 164 |
# File 'lib/bluesky/pure_component.rb', line 160 def should_render?(previous) # puts "#{self.class.name}:should_render? #{(@delegate && @delegate.force_update?)}, #{!shallow_equal?(_data, previous._data)}" (@delegate && @delegate.force_update?) || !shallow_equal?(_data, previous._data) end |
#unmount ⇒ Object
177 178 179 |
# File 'lib/bluesky/pure_component.rb', line 177 def unmount @mounted = false end |