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

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:

  • options

  • has_schema_attrs definition(if used)

  • the underscored class name

Parameters:

  • opts (Hash{Symbol=>Mixed}) (defaults to: {})

    passed on to #SchemaTools::Hash.from_schema

Returns:



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.

Parameters:

  • opts (Hash{Symbol=>Mixed}) (defaults to: {})

    passed on to #SchemaTools::Hash.from_schema

Returns:

  • (String)

    json



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