Class: SirTrevorRails::Block
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- SirTrevorRails::Block
- Defined in:
- lib/sir_trevor_rails/block.rb
Direct Known Subclasses
SirTrevorRails::Blocks::TweetBlock, SirTrevorRails::Blocks::VideoBlock
Constant Summary collapse
- DEFAULT_FORMAT =
:markdown
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.block_class(type) ⇒ Object
Infers the block class.
-
.block_class!(block_name) ⇒ Object
Infers the block class.
- .from_hash(hash, parent) ⇒ Object
- .type_klass(hash) ⇒ Object
Instance Method Summary collapse
- #as_json(*attrs) ⇒ Object
- #format ⇒ Object
-
#initialize(hash, parent) ⇒ Block
constructor
A new instance of Block.
- #to_partial_path ⇒ Object
Constructor Details
#initialize(hash, parent) ⇒ Block
Returns a new instance of Block.
16 17 18 19 20 21 |
# File 'lib/sir_trevor_rails/block.rb', line 16 def initialize(hash, parent) @raw_data = hash @parent = parent @type = hash[:type].to_sym super(hash[:data]) end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
23 24 25 |
# File 'lib/sir_trevor_rails/block.rb', line 23 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/sir_trevor_rails/block.rb', line 23 def type @type end |
Class Method Details
.block_class(type) ⇒ Object
Infers the block class. Safe lookup that tries to identify user created block class.
42 43 44 45 46 47 48 49 |
# File 'lib/sir_trevor_rails/block.rb', line 42 def self.block_class(type) block_name = "#{type.to_s.camelize}Block" begin block_name.constantize rescue NameError block_class!(block_name) end end |
.block_class!(block_name) ⇒ Object
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
57 58 59 60 61 62 63 |
# File 'lib/sir_trevor_rails/block.rb', line 57 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 |
.from_hash(hash, parent) ⇒ Object
7 8 9 10 |
# 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 |
.type_klass(hash) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/sir_trevor_rails/block.rb', line 65 def self.type_klass(hash) if self == Block block_class(hash[:type].to_sym) else self end end |
Instance Method Details
#as_json(*attrs) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/sir_trevor_rails/block.rb', line 29 def as_json(*attrs) { type: @type.to_s, data: marshal_dump } end |
#format ⇒ Object
12 13 14 |
# File 'lib/sir_trevor_rails/block.rb', line 12 def format send(:[], :format).present? ? send(:[], :format).to_sym : DEFAULT_FORMAT end |
#to_partial_path ⇒ Object
25 26 27 |
# File 'lib/sir_trevor_rails/block.rb', line 25 def to_partial_path "sir_trevor/blocks/" << self.class.name.demodulize.underscore end |