Module: SchemaTools::Modules::AsSchema
- Extended by:
- ActiveSupport::Concern
- Included in:
- Attributes
- Defined in:
- lib/schema_tools/modules/as_schema.rb
Overview
Extend a class so it can be rendered as json from a named schema
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #as_hash(opts = {}) ⇒ Object
-
#as_schema_hash(opts = {}) ⇒ Hash
The object as hash with fields detected from its schema.
-
#as_schema_json(opts = {}) ⇒ String
convert object to a schema markup.
Instance Method Details
#as_hash(opts = {}) ⇒ Object
29 |
# File 'lib/schema_tools/modules/as_schema.rb', line 29 def as_hash(opts={}) as_schema_hash(opts); end |
#as_schema_hash(opts = {}) ⇒ Hash
The object as hash with fields detected from its schema. The schema name is derived from:
-
has_schema_attrs definition(if used)
-
the underscored class name
22 23 24 25 26 27 28 |
# File 'lib/schema_tools/modules/as_schema.rb', line 22 def as_schema_hash(opts={}) # detect schema name from class method, else class name or opts is used. if self.class.schema_name opts[:class_name] ||= self.class.schema_name end SchemaTools::Hash.from_schema(self, opts) end |
#as_schema_json(opts = {}) ⇒ String
convert object to a schema markup.
11 12 13 |
# File 'lib/schema_tools/modules/as_schema.rb', line 11 def as_schema_json(opts={}) ActiveSupport::JSON.encode(as_schema_hash(opts)) end |