Class: JsonSchemaView::BaseComponent Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/json_schema_view/base_component.rb

Overview

This class is abstract.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props:, validate: Configuration.instance.validate_by_default, **renderer_options) ⇒ BaseComponent

Returns a new instance of BaseComponent.

Parameters:

  • props (Hash, self::Props)

    props of the React component.

  • validate (Boolean) (defaults to: Configuration.instance.validate_by_default)

    if true, validate props besed on its schema before rendering.



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, **renderer_options)
  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, **renderer_options)
end

Instance Attribute Details

#propsself::Props (readonly)

Returns:

  • (self::Props)


48
49
50
# File 'lib/json_schema_view/base_component.rb', line 48

def props
  @props
end

#rendererJsonSchemaView::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_classClass<BaseProps>

Returns:



32
33
34
# File 'lib/json_schema_view/base_component.rb', line 32

def base_props_class
  JsonSchemaView::BaseProps
end

.inherited(child) ⇒ Object

Method override:

  • Class.override



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.

Returns:



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>

Parameters:

  • new_renderer_class (Symbol, Class<Renderer::Base>) (defaults to: nil)

Returns:

  • (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_nameString

Returns:

  • (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