Class: ActionSchema::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_schema/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_or_collection, context: {}, controller: nil) ⇒ Base

Returns a new instance of Base.



78
79
80
81
82
# File 'lib/action_schema/base.rb', line 78

def initialize(record_or_collection, context: {}, controller: nil)
  @record_or_collection = record_or_collection
  @context = context
  @controller = controller
end

Class Attribute Details

.after_render_hooksObject

Returns the value of attribute after_render_hooks.



12
13
14
# File 'lib/action_schema/base.rb', line 12

def after_render_hooks
  @after_render_hooks
end

.before_render_hooksObject

Returns the value of attribute before_render_hooks.



12
13
14
# File 'lib/action_schema/base.rb', line 12

def before_render_hooks
  @before_render_hooks
end

.schemaObject

Returns the value of attribute schema.



12
13
14
# File 'lib/action_schema/base.rb', line 12

def schema
  @schema
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



76
77
78
# File 'lib/action_schema/base.rb', line 76

def context
  @context
end

#controllerObject (readonly)

Returns the value of attribute controller.



76
77
78
# File 'lib/action_schema/base.rb', line 76

def controller
  @controller
end

#record_or_collectionObject (readonly)

Returns the value of attribute record_or_collection.



76
77
78
# File 'lib/action_schema/base.rb', line 76

def record_or_collection
  @record_or_collection
end

Class Method Details

.after_render(&block) ⇒ Object



23
24
25
26
# File 'lib/action_schema/base.rb', line 23

def after_render(&block)
  self.after_render_hooks ||= []
  after_render_hooks << block
end

.association(name, schema_definition = nil, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_schema/base.rb', line 38

def association(name, schema_definition = nil, &block)
  base_schema_class = ActionSchema.configuration.base_class

  resolved_schema =
    if schema_definition.is_a?(Symbol)
      ->(controller) { controller.resolve_schema(schema_definition) }
    elsif schema_definition.is_a?(Class)
      schema_definition
    elsif block_given?
      Class.new(base_schema_class, &block)
    else
      raise ArgumentError, "An association schema or block must be provided"
    end

  schema[name] = { association: resolved_schema }
end

.before_render(&block) ⇒ Object



18
19
20
21
# File 'lib/action_schema/base.rb', line 18

def before_render(&block)
  self.before_render_hooks ||= []
  before_render_hooks << block
end

.call(*args, **kwargs, &block) ⇒ Object



14
15
16
# File 'lib/action_schema/base.rb', line 14

def call(*args, **kwargs, &block)
  new(*args, **kwargs, &block).render
end

.computed(name, &block) ⇒ Object



55
56
57
# File 'lib/action_schema/base.rb', line 55

def computed(name, &block)
  schema[name] = { computed: true, value: block }
end

.field(name, value = nil, **options, &block) ⇒ Object



34
35
36
# File 'lib/action_schema/base.rb', line 34

def field(name, value = nil, **options, &block)
  schema[name] = { value: block || value || name, **options }
end

.fields(*names) ⇒ Object



59
60
61
# File 'lib/action_schema/base.rb', line 59

def fields(*names)
  names.each { |name| field(name) }
end

.inherited(subclass) ⇒ Object



28
29
30
31
32
# File 'lib/action_schema/base.rb', line 28

def inherited(subclass)
  subclass.before_render_hooks = (before_render_hooks || []).dup
  subclass.after_render_hooks = (after_render_hooks || []).dup
  subclass.schema = schema.dup
end

.omit(*names) ⇒ Object



63
64
65
# File 'lib/action_schema/base.rb', line 63

def omit(*names)
  names.each { |name| schema.delete(name) }
end

.parse(data) ⇒ Object

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/action_schema/base.rb', line 71

def parse(data)
  raise NotImplementedError, "Parsing is not yet implemented"
end

Instance Method Details

#renderObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/action_schema/base.rb', line 84

def render
  renderable = @record_or_collection
  renderable = apply_hooks(:before_render, @record_or_collection)

  output =
    if renderable.respond_to?(:map)
      renderable.map { |record| render_record(record) }
    else
      render_record(renderable)
    end

  apply_hooks(:after_render, output)
end