Module: JSON::Schematized::BasicWrapper

Extended by:
Wrapper
Defined in:
lib/json/schematized/basic_wrapper.rb

Defined Under Namespace

Modules: ClassMethods, Collections, Models, SchematizedArray, SchematizedHash

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Wrapper

add_attribute!, build_class_name, build_collection, build_model, collection_superclass, extended, meta_type, model_superclass, modularize, parse_json_schema_type, prepare_model, prepare_schema!

Class Method Details

.included(base) ⇒ Object

Raises:

  • (TypeError)


10
11
12
13
14
15
# File 'lib/json/schematized/basic_wrapper.rb', line 10

def self.included(base)
  raise TypeError, "#{base.inspect} should inherits #{model_superclass}" unless base < model_superclass
  base.send(:include, Models)
  base.extend ClassMethods
  prepare_schema!(base, base.json_schema, :complex_types)
end

.model_superclassObject



80
81
82
# File 'lib/json/schematized/basic_wrapper.rb', line 80

def self.model_superclass
  ::Hash
end

.modularize(json_schema) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/json/schematized/basic_wrapper.rb', line 28

def self.modularize(json_schema)
  super(json_schema) do
    include BasicWrapper::SchematizedHash

    def json_schema=(*args); end

    def self.attribute_set
      unless defined?(@attribute_set)
        set = []
        json_schema[:properties].each_pair do |field_name, meta|
          set << Virtus::Attribute.build(field_name, BasicWrapper.meta_type(self, field_name, meta))
        end
        @attribute_set = Virtus::AttributeSet.new(nil, set)
      end
      @attribute_set
    end

    def self.extend_object(base)
      super
      return if base.class.include? BasicWrapper::Models
      class_name = :ComplexTypes
      (const_defined?(class_name) ?
        const_get(class_name) :
        const_set(class_name, Module.new)
      ).tap do |klass|
        unless klass.include?(self)
          klass.send(:include, self)
          klass.module_eval do
            define_method :subclasses_namespace do
              klass
            end
          end
        end
        base.extend klass
      end
    end
  end
end

.prepare_model(ref, field_name, model_class, json_schema) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/json/schematized/basic_wrapper.rb', line 67

def self.prepare_model(ref, field_name, model_class, json_schema)
  model_class.extend BasicWrapper::ClassMethods
  parent_namespace = {:ref => ref, :field => field_name.to_sym}
  model_class.instance_variable_set(:@parent_namespace, parent_namespace)
  def model_class.json_schema
    json_schema = @parent_namespace[:ref].json_schema
    meta = json_schema[:properties][@parent_namespace[:field]] || {}
    meta = meta[:items] if meta[:type] == "array"
    meta
  end
  prepare_schema!(model_class, json_schema, :complex_types)
end

Instance Method Details

#initialize(attrs = nil) ⇒ Object



23
24
25
26
# File 'lib/json/schematized/basic_wrapper.rb', line 23

def initialize(attrs = nil)
  self.json_schema = self.class.json_schema
  mass_assign!(attrs)
end