class SirTrevorRails::Block

Constants

DEFAULT_FORMAT

Attributes

parent[R]
type[R]

Public Class Methods

custom_block_types() click to toggle source

Sets a list of custom block types to speed up lookup at runtime.

# File lib/sir_trevor_rails/block.rb, line 17
def self.custom_block_types
  []
end
from_hash(hash, parent) click to toggle source
# File lib/sir_trevor_rails/block.rb, line 7
def self.from_hash(hash, parent)
  hash = hash.deep_dup.with_indifferent_access
  self.type_klass(hash).new(hash, parent)
end
new(hash, parent) click to toggle source
Calls superclass method
# File lib/sir_trevor_rails/block.rb, line 21
def initialize(hash, parent)
  @raw_data = hash
  @parent  = parent
  @type    = hash[:type].to_sym
  super(hash[:data])
end

Private Class Methods

block_class(type) click to toggle source

Infers the block class. Safe lookup that tries to identify user created block class.

@param [Symbol] type

# File lib/sir_trevor_rails/block.rb, line 47
def self.block_class(type)
  type_name = type.to_s.camelize
  block_name = "#{type_name}Block"
  if custom_block_types.include?(type_name)
    begin
      block_name.constantize
    rescue NameError
      block_class!(block_name)
    end
  else
    block_class!(block_name)
  end
end
block_class!(block_name) click to toggle source

Infers the block class. Failover from block_class. Safe lookup against the SirTevor::Blocks namespace If no block is found, create one with given name and inherit from Block class

@param [Constant] block_name

# File lib/sir_trevor_rails/block.rb, line 67
def self.block_class!(block_name)
  begin
    SirTrevorRails::Blocks.const_get(block_name)
  rescue NameError
    SirTrevorRails::Blocks.const_set(block_name, Class.new(Block))
  end
end
type_klass(hash) click to toggle source
# File lib/sir_trevor_rails/block.rb, line 75
def self.type_klass(hash)
  if self == Block
    block_class(hash[:type].to_sym)
  else
    self
  end
end

Public Instance Methods

as_json(*attrs) click to toggle source
# File lib/sir_trevor_rails/block.rb, line 34
def as_json(*attrs)
  {
    type: @type.to_s,
    data: marshal_dump
  }
end
format() click to toggle source
# File lib/sir_trevor_rails/block.rb, line 12
def format
  send(:[], :format).present? ? send(:[], :format).to_sym : DEFAULT_FORMAT
end
to_partial_path() click to toggle source
# File lib/sir_trevor_rails/block.rb, line 30
def to_partial_path
  "sir_trevor/blocks/" << self.class.name.demodulize.underscore
end