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



86
87
88
# File 'lib/json/schematized/basic_wrapper.rb', line 86

def self.model_superclass
  ::Hash
end

.modularize(json_schema) ⇒ Object



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
66
67
68
69
70
71
# File 'lib/json/schematized/basic_wrapper.rb', line 32

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

    def self.attribute_set
      unless defined?(@attribute_set)
        set = []
        v1x = ::Virtus.respond_to?(:module)
        json_schema[:properties].each_pair do |field_name, meta|
          args = [field_name, BasicWrapper.meta_type(self, field_name, meta), {:name => field_name}]
          if v1x
            args.shift
          else
            args.pop
          end
          set << Virtus::Attribute.build(*args)
        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
      if const_defined?(class_name)
        klass = const_get(class_name)
      else
        klass = const_set(class_name, Module.new)
        _mod = self
        klass.module_eval do
          define_method(:submodels_scope){ _mod }
        end
      end
      base.extend klass
      BasicWrapper::SchematizedHash.ensure_structure!(base, base.json_schema)
    end
  end
end

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/json/schematized/basic_wrapper.rb', line 73

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



27
28
29
30
# File 'lib/json/schematized/basic_wrapper.rb', line 27

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