Skip to content

Instantly share code, notes, and snippets.

@catmando
Last active June 24, 2021 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catmando/35cd5a48c742b318eb1544adbaa5389b to your computer and use it in GitHub Desktop.
Save catmando/35cd5a48c742b318eb1544adbaa5389b to your computer and use it in GitHub Desktop.
A simple Hyperstack Component

Here is a simple Rails Model and a Hyperstack Component. The Hyperstack Component uses Opal Ruby. Opal is a version of Ruby that runs on the Browser.

Hyperstack is a gem that wraps React with a Ruby DSL.

class Order < ApplicationRecord
  ...
  scope :shipped, -> () { where(status: :shipped) }
  ...
end
class OrdersShipped < HyperComponent
  def format_number(number)
    number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
  end

  render(DIV, class: 'orders-shipped') do
    format_number Order.shipped.count
  end
end

Can you figure out what HTML will be generated by the OrdersShipped component if there are 5327 shipped orders?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment