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(lambda_or_proc = nil, &block) ⇒ Object
- .association(name, schema_definition = nil, &block) ⇒ Object
- .before_render(lambda_or_proc = nil, &block) ⇒ Object
- .call(*args, **kwargs, &block) ⇒ Object
- .computed(name, lambda_or_proc = nil, &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.
86 87 88 89 90 |
# File 'lib/action_schema/base.rb', line 86 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.
84 85 86 |
# File 'lib/action_schema/base.rb', line 84 def context @context end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
84 85 86 |
# File 'lib/action_schema/base.rb', line 84 def controller @controller end |
#record_or_collection ⇒ Object (readonly)
Returns the value of attribute record_or_collection.
84 85 86 |
# File 'lib/action_schema/base.rb', line 84 def record_or_collection @record_or_collection end |
Class Method Details
.after_render(lambda_or_proc = nil, &block) ⇒ Object
30 31 32 |
# File 'lib/action_schema/base.rb', line 30 def after_render(lambda_or_proc = nil, &block) self.after_render_hooks << (lambda_or_proc || block) end |
.association(name, schema_definition = nil, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/action_schema/base.rb', line 44 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 schema_definition.is_a?(Proc) Class.new(base_schema_class, &Dalambda[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(lambda_or_proc = nil, &block) ⇒ Object
26 27 28 |
# File 'lib/action_schema/base.rb', line 26 def before_render(lambda_or_proc = nil, &block) self.before_render_hooks << (lambda_or_proc || 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, lambda_or_proc = nil, &block) ⇒ Object
63 64 65 |
# File 'lib/action_schema/base.rb', line 63 def computed(name, lambda_or_proc = nil, &block) schema[name] = { computed: true, value: (lambda_or_proc || block) } end |
.field(name, value = nil, **options, &block) ⇒ Object
40 41 42 |
# File 'lib/action_schema/base.rb', line 40 def field(name, value = nil, **, &block) schema[name] = { value: block || value || name, ** } end |
.fields(*names) ⇒ Object
67 68 69 |
# File 'lib/action_schema/base.rb', line 67 def fields(*names) names.each { |name| field(name) } end |
.inherited(subclass) ⇒ Object
34 35 36 37 38 |
# File 'lib/action_schema/base.rb', line 34 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
71 72 73 |
# File 'lib/action_schema/base.rb', line 71 def omit(*names) names.each { |name| schema.delete(name) } end |
.parse(data) ⇒ Object
79 80 81 |
# File 'lib/action_schema/base.rb', line 79 def parse(data) raise NotImplementedError, "Parsing is not yet implemented" end |
Instance Method Details
#render ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/action_schema/base.rb', line 92 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 |