Class: ActionSchema::Base
- Inherits:
-
Object
- Object
- ActionSchema::Base
- Defined in:
- lib/action_schema/base.rb
Class Attribute Summary collapse
-
.after_render_hooks ⇒ Object
Returns the value of attribute after_render_hooks.
-
.before_render_hooks ⇒ Object
Returns the value of attribute before_render_hooks.
-
.schema ⇒ Object
Returns the value of attribute schema.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#record_or_collection ⇒ Object
readonly
Returns the value of attribute record_or_collection.
Class Method Summary collapse
- .after_render(&block) ⇒ Object
- .association(name, schema_definition = nil, &block) ⇒ Object
- .before_render(&block) ⇒ Object
- .call(*args, **kwargs, &block) ⇒ Object
- .computed(name, &block) ⇒ Object
- .field(name, value = nil, **options, &block) ⇒ Object
- .fields(*names) ⇒ Object
- .inherited(subclass) ⇒ Object
- .omit(*names) ⇒ Object
- .parse(data) ⇒ Object
Instance Method Summary collapse
-
#initialize(record_or_collection, context: {}, controller: nil) ⇒ Base
constructor
A new instance of Base.
- #render ⇒ Object
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_hooks ⇒ Object
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_hooks ⇒ Object
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 |
.schema ⇒ Object
Returns the value of attribute schema.
12 13 14 |
# File 'lib/action_schema/base.rb', line 12 def schema @schema end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
76 77 78 |
# File 'lib/action_schema/base.rb', line 76 def context @context end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
76 77 78 |
# File 'lib/action_schema/base.rb', line 76 def controller @controller end |
#record_or_collection ⇒ Object (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, **, &block) schema[name] = { value: block || value || name, ** } 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
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
#render ⇒ Object
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 |