Module: JSON::Schematized::Wrapper
- Included in:
- BasicWrapper, VirtusWrapper
- Defined in:
- lib/json/schematized/wrapper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_attribute!(ref, field_name, meta, kind) ⇒ Object
- #build_class_name(field_name) ⇒ Object
- #build_collection(ref, field_name, meta) ⇒ Object
- #build_model(ref, field_name, json_schema, singularize = false) ⇒ Object
- #collection_superclass ⇒ Object
- #meta_type(ref, field_name, meta, singularize = false) ⇒ Object
- #model_superclass ⇒ Object
- #modularize(json_schema, &block) ⇒ Object
- #parse_json_schema_type(ref, field_name, type) ⇒ Object
- #prepare_model(ref, field_name, model_class, json_schema) ⇒ Object
- #prepare_schema!(ref, json_schema, mode) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
6 7 8 9 |
# File 'lib/json/schematized/wrapper.rb', line 6 def self.extended(base) base.const_set(:Models, Module.new).send(:include, Schematized::Models) base.const_set(:Collections, Module.new).send(:include, Schematized::Collections) end |
Instance Method Details
#add_attribute!(ref, field_name, meta, kind) ⇒ Object
58 59 |
# File 'lib/json/schematized/wrapper.rb', line 58 def add_attribute!(ref, field_name, , kind) end |
#build_class_name(field_name) ⇒ Object
84 85 86 |
# File 'lib/json/schematized/wrapper.rb', line 84 def build_class_name(field_name) field_name.to_s.gsub(/(?:\A_*|_)([^_])/){ $1.upcase } end |
#build_collection(ref, field_name, meta) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/json/schematized/wrapper.rb', line 92 def build_collection(ref, field_name, ) class_name = [build_class_name(field_name), "Collection"].join.to_sym (ref.const_defined?(class_name) ? ref.const_get(class_name) : ref.const_set(class_name, Class.new(collection_superclass)) ).tap do |klass| unless klass.include?(Schematized::Collections) klass.send(:include, self::Collections) end end[(ref, field_name, , true)] end |
#build_model(ref, field_name, json_schema, singularize = false) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/json/schematized/wrapper.rb', line 107 def build_model(ref, field_name, json_schema, singularize = false) name = field_name name = ::ActiveSupport::Inflector.singularize(field_name.to_s).to_sym if singularize class_name = build_class_name(name).to_sym (ref.const_defined?(class_name) ? ref.const_get(class_name) : ref.const_set(class_name, Class.new(*[model_superclass].compact)) ).tap do |klass| unless klass.include?(Schematized::Models) klass.send(:include, self::Models) prepare_model(ref, field_name, klass, json_schema) end end end |
#collection_superclass ⇒ Object
88 89 90 |
# File 'lib/json/schematized/wrapper.rb', line 88 def collection_superclass Array end |
#meta_type(ref, field_name, meta, singularize = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/json/schematized/wrapper.rb', line 61 def (ref, field_name, , singularize = false) case [:type] when "array" build_collection(ref, field_name, [:items]) when "object" build_model(ref, field_name, , singularize) when "boolean" TrueClass when "integer" Integer else parse_json_schema_type ref, field_name, [:type] end end |
#model_superclass ⇒ Object
104 105 |
# File 'lib/json/schematized/wrapper.rb', line 104 def model_superclass end |
#modularize(json_schema, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/json/schematized/wrapper.rb', line 11 def modularize(json_schema, &block) json_schema = MultiJson.dump(json_schema) unless json_schema.is_a?(String) json_schema = MultiJson.load(json_schema, :symbolize_keys => true) module_name = "JSD#{json_schema.hash}".gsub(/\-/, "_").to_sym # JSON-Schema Definition if const_defined?(module_name) const_get(module_name) else const_set(module_name, Module.new).tap do |m| m.send(:include, self::Models) def m.json_schema; @json_schema end m.module_eval do @json_schema = json_schema.freeze define_method(:json_schema){ m.json_schema } end m.module_eval(&block) if block_given? end end end |
#parse_json_schema_type(ref, field_name, type) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/json/schematized/wrapper.rb', line 76 def parse_json_schema_type(ref, field_name, type) case type when "string" then String when "number" then Numeric else Object end end |
#prepare_model(ref, field_name, model_class, json_schema) ⇒ Object
122 123 |
# File 'lib/json/schematized/wrapper.rb', line 122 def prepare_model(ref, field_name, model_class, json_schema) end |
#prepare_schema!(ref, json_schema, mode) ⇒ Object
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 |
# File 'lib/json/schematized/wrapper.rb', line 30 def prepare_schema!(ref, json_schema, mode) modes = { :complex_types => 1, :simple_types => 2, :all_types => 3 } mode = [modes[mode].to_i, 0].max accept_complex_types = (modes[:complex_types] & mode) > 0 accept_simple_types = (modes[:simple_types] & mode) > 0 json_schema[:properties].each_pair do |field_name, | kind = nil case [:type] when "array" next unless accept_complex_types collection = build_collection(ref, field_name, [:items]) kind = collection when "object" next unless accept_complex_types model = build_model(ref, field_name, ) kind = model else next unless accept_simple_types kind = (ref, field_name, ) end add_attribute! ref, field_name, , kind end end |