ember-schema 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bbd2105d4a845ce5bc89f0a32104a85a623ebdb5
4
+ data.tar.gz: 0eaf1ef3bd0eb38ca5499468bc9ea83c6fbb3442
5
+ SHA512:
6
+ metadata.gz: 87905e6144ee9f53e0f0a58238ff2f1dea69a75fe2dd98e651df3692e53ef0e6ae968f2c51ce6daaf7f07f0ccbae879c5efcd3ce5ec45a39fb4104e62104f205
7
+ data.tar.gz: 4f5a879903b6442f9187c163d85598a191ab26543dd6049cfc2199edb0cf1e176da88e0c7bf8599ddc6889d2410e97b982b4faaa9afafb76d5fd17627cc61bfa
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ember-schema.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Ember::Schema
2
+
3
+ Generates a json schema for restpack_serializer models
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ember-schema'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ember-schema
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/ember-schema/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ember/schema/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ember-schema"
8
+ spec.version = Ember::Schema::VERSION
9
+ spec.authors = ["Nathan Palmer", "Aaron Hansen"]
10
+ spec.email = ["nathan@nathanpalmer.com"]
11
+ spec.summary = %q{Generates a json schema for restpack_serializer models}
12
+ # spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "restpack_serializer"
25
+ end
@@ -0,0 +1,139 @@
1
+ require "rake"
2
+ require "ember/schema/version"
3
+
4
+ module Ember
5
+ module Schema
6
+ def self.generate
7
+ schema_hash = {}
8
+ # Rails.application.eager_load! # populate descendants
9
+
10
+ RestPack::Serializer.class_map.sort_by { |s| s[0] }.map { |s| s[1] }.each do |serializer_class|
11
+ begin
12
+ next if serializer_class == ApplicationSerializer
13
+ next if serializer_class.respond_to?(:ignore) && serializer_class.ignore
14
+ klass = get_klass(serializer_class)
15
+ if klass.present?
16
+ # if we are dealing with a polymorphic class, then process accordingly
17
+ if klass.respond_to?(:base_class) && klass != klass.base_class
18
+ p "--Skipping inherited class: '#{klass.name}', it will be processed with parent"
19
+ next
20
+ end
21
+ schema = schema(serializer_class, klass)
22
+ name = klass.name.camelize
23
+ schema_hash[name] = schema
24
+ p "#{klass.name}: Complete"
25
+ # Check for inherited serializer classes now
26
+ serializer_class.descendants.sort_by { |s| s.name }.each do |child_serializer_class|
27
+ begin
28
+ # to be inherited, it has to have a subclass thats not the base and a klass that is inherited
29
+ if child_serializer_class == ApplicationSerializer || # skip, default class
30
+ child_serializer_class.superclass == ApplicationSerializer || # skip, base is default class, not inherited
31
+ child_serializer_class.superclass == RestPack::Serializer || # skip, default class
32
+ child_serializer_class == serializer_class # skip serializer is itself
33
+ next
34
+ end
35
+ child_klass = get_klass(child_serializer_class)
36
+ if child_klass.present?
37
+ # if we are dealing with a polymorphic class, then process accordingly
38
+ next unless child_klass.respond_to?(:base_class) # this is not active record
39
+ next if child_klass == child_klass.base_class # this is the base class
40
+ next unless child_klass.base_class == klass # this is not a subclass of klass
41
+ diff_schema = inherited_schema(child_serializer_class, child_klass, schema)
42
+ child_name = child_klass.name.camelize
43
+ # Modify parents schema
44
+ schema[:descendants] ||= {}
45
+ schema[:descendants][child_name] = diff_schema
46
+ p " > #{child_klass.name}: Child Complete"
47
+ end
48
+ rescue => e
49
+ p e
50
+ print e.backtrace.join("\r\n")
51
+ end
52
+ end
53
+ end
54
+ rescue => e
55
+ p e
56
+ print e.backtrace.join("\r\n")
57
+ end
58
+ end
59
+
60
+ schema_hash
61
+ end
62
+
63
+ private
64
+
65
+ def self.get_klass(serializer)
66
+ return serializer.model_class
67
+ # module_name = serializer.name.deconstantize
68
+ # class_name = serializer.root_name.camelize
69
+ # full_name = [module_name, class_name].reject(&:empty?).join("::")
70
+ # begin
71
+ # return full_name.constantize
72
+ # rescue => e
73
+ # p " - Unable to find model: '#{full_name}'. Skipping schema processing."
74
+ # return nil
75
+ # end
76
+ end
77
+
78
+ def self.schema(serializer, klass)
79
+ columns = if klass.respond_to? :columns_hash then klass.columns_hash else {} end
80
+
81
+ attrs = {}
82
+ (serializer.serializable_attributes || {}).each do |id, name|
83
+ options = serializer.serializable_attributes_options[id] || {}
84
+ if options[:type].present?
85
+ attrs[name] = options[:type].to_s
86
+ else
87
+ # If no type is given, attempt to get it from the Active Model class
88
+ if column = columns[name.to_s]
89
+ attrs[name] = column.type
90
+ else
91
+ # Other wise default to string
92
+ attrs[name] = "string"
93
+ end
94
+ end
95
+ end
96
+
97
+ associations = {}
98
+
99
+ serializer.can_include.each do |association_name|
100
+ #association = association_class.new(attr, self)
101
+
102
+ if model_association = klass.reflect_on_association(association_name)
103
+ # Real association.
104
+ associations[association_name] = { model_association.macro => model_association.class_name.pluralize.underscore.downcase }
105
+ # elsif model_association = get_type(association)
106
+ # # Computed association. We could infer has_many vs. has_one from
107
+ # # the association class, but that would make it different from
108
+ # # real associations, which read has_one vs. belongs_to from the
109
+ # # model.
110
+ # associations[association.key] = { model_association => association.key }
111
+ end
112
+ #associations[association_name][:async] = (association.options[:async] || false) if association.options.has_key? :async
113
+ end
114
+
115
+ return { :attributes => attrs, :associations => associations }
116
+ end
117
+
118
+ def self.inherited_schema(serializer, klass, base_schema)
119
+ schema = schema(serializer, klass)
120
+
121
+ schema_diff = {}
122
+ schema_diff[:attributes] = diff(base_schema[:attributes], schema[:attributes])
123
+ schema_diff[:associations] = diff(base_schema[:associations], schema[:associations])
124
+ return schema_diff
125
+ end
126
+
127
+ def self.diff(base, child)
128
+ diff = {}
129
+ child.each do |key, value|
130
+ if !base.has_key?(key) || (base.has_key?(key) && base[key] != value)
131
+ diff[key] = value
132
+ end
133
+ end
134
+ return diff
135
+ end
136
+ end
137
+ end
138
+
139
+ load "tasks/ember.rake"
@@ -0,0 +1,5 @@
1
+ module Ember
2
+ module Schema
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ namespace :db do
2
+ namespace :schema do
3
+ desc 'Regenerate the Ember schema.js based on the serializers'
4
+ task :ember => :environment do
5
+ schema_hash = Ember::Schema.generate
6
+ schema_json = JSON.pretty_generate(schema_hash)
7
+ File.open 'db/schema.js', 'w' do |f|
8
+ f << schema_json
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ ##
15
+ # Automatically generate schema when migration changes occur
16
+ ##
17
+ if ActiveRecord::Base.dump_schema_after_migration
18
+ namespace :db do
19
+ task :_dump => [ 'db:schema:ember' ]
20
+ end
21
+ else
22
+ namespace :db do
23
+ task :migrate do
24
+ Rake::Task['db:schema:ember'].invoke
25
+ end
26
+
27
+ namespace :migrate do
28
+ [:change, :up, :down, :reset, :redo].each do |t|
29
+ task t do
30
+ Rake::Task['db:schema:ember'].invoke
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ember-schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Palmer
8
+ - Aaron Hansen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: restpack_serializer
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description:
57
+ email:
58
+ - nathan@nathanpalmer.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - ember-schema.gemspec
69
+ - lib/ember/schema.rb
70
+ - lib/ember/schema/version.rb
71
+ - lib/tasks/ember.rake
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Generates a json schema for restpack_serializer models
96
+ test_files: []