Class: JsonSchemaView::BaseComponent Abstract
- Inherits:
-
Object
- Object
- JsonSchemaView::BaseComponent
- Extended by:
- Forwardable
- Defined in:
- lib/json_schema_view/base_component.rb
Overview
This class is abstract.
Instance Attribute Summary collapse
- #props ⇒ self::Props readonly
- #renderer ⇒ JsonSchemaView::Renderers::Base readonly
Class Method Summary collapse
- .base_props_class ⇒ Class<BaseProps>
- .inherited(child) ⇒ Object
-
.props_class(&definition) ⇒ Class<BaseProps>
(Re)define Props class under the class to express interface of props of the React component.
- .renderer_class(new_renderer_class = nil) ⇒ Class<Renderer::Base>
Instance Method Summary collapse
- #component_name ⇒ String
-
#initialize(props:, validate: Configuration.instance.validate_by_default, **renderer_options) ⇒ BaseComponent
constructor
A new instance of BaseComponent.
- #render_in(view_context) ⇒ Object
- #validate_props! ⇒ void
Constructor Details
#initialize(props:, validate: Configuration.instance.validate_by_default, **renderer_options) ⇒ BaseComponent
Returns a new instance of BaseComponent.
57 58 59 60 61 62 63 |
# File 'lib/json_schema_view/base_component.rb', line 57 def initialize(props:, validate: Configuration.instance.validate_by_default, **) raise "Component instance cannot be created if no renderer is not defined" unless self.class.renderer_class @props = props.is_a?(self.class.props_class) ? props : self.class.props_class.new(**props) @validate = validate || @renderer = self.class.renderer_class.new(self, **) end |
Instance Attribute Details
#props ⇒ self::Props (readonly)
48 49 50 |
# File 'lib/json_schema_view/base_component.rb', line 48 def props @props end |
#renderer ⇒ JsonSchemaView::Renderers::Base (readonly)
51 52 53 |
# File 'lib/json_schema_view/base_component.rb', line 51 def renderer @renderer end |
Class Method Details
.base_props_class ⇒ Class<BaseProps>
32 33 34 |
# File 'lib/json_schema_view/base_component.rb', line 32 def base_props_class JsonSchemaView::BaseProps end |
.inherited(child) ⇒ Object
13 14 15 16 |
# File 'lib/json_schema_view/base_component.rb', line 13 def inherited(child) super child.renderer_class(renderer_class) end |
.props_class(&definition) ⇒ Class<BaseProps>
(Re)define Props class under the class to express interface of props of the React component.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json_schema_view/base_component.rb', line 20 def props_class(&definition) klass = if const_defined?(:Props, false) const_get(:Props, false) else const_set(:Props, Class.new(base_props_class)) end klass.class_eval(&definition) if block_given? klass end |
.renderer_class(new_renderer_class = nil) ⇒ Class<Renderer::Base>
38 39 40 41 42 43 44 |
# File 'lib/json_schema_view/base_component.rb', line 38 def renderer_class(new_renderer_class = nil) if new_renderer_class new_renderer_class = Renderers.find_by_name(new_renderer_class) if new_renderer_class.is_a?(Symbol) @renderer_class = new_renderer_class end @renderer_class end |
Instance Method Details
#component_name ⇒ String
78 79 80 |
# File 'lib/json_schema_view/base_component.rb', line 78 def component_name ActiveSupport::Inflector.demodulize(self.class.name) end |
#render_in(view_context) ⇒ Object
Note:
This method is used when the component is rendered.
67 68 69 70 |
# File 'lib/json_schema_view/base_component.rb', line 67 def render_in(view_context) validate_props! if @validate view_context.render(renderer) end |
#validate_props! ⇒ void
This method returns an undefined value.
73 74 75 |
# File 'lib/json_schema_view/base_component.rb', line 73 def validate_props! props.validate_json! end |