ember-schema 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bbd2105d4a845ce5bc89f0a32104a85a623ebdb5
4
- data.tar.gz: 0eaf1ef3bd0eb38ca5499468bc9ea83c6fbb3442
3
+ metadata.gz: 916f39406eef607fbc18202a2153630ae903354c
4
+ data.tar.gz: 9003198bddc8dc9a39a865ed89d229d7c8005d7c
5
5
  SHA512:
6
- metadata.gz: 87905e6144ee9f53e0f0a58238ff2f1dea69a75fe2dd98e651df3692e53ef0e6ae968f2c51ce6daaf7f07f0ccbae879c5efcd3ce5ec45a39fb4104e62104f205
7
- data.tar.gz: 4f5a879903b6442f9187c163d85598a191ab26543dd6049cfc2199edb0cf1e176da88e0c7bf8599ddc6889d2410e97b982b4faaa9afafb76d5fd17627cc61bfa
6
+ metadata.gz: 7415f1c58de8c8b7cf23818763ebd8108ae93fc5592b289ab11e4184b751f7bff6d12bc48053b23061d96228de8ed41de4e2dc9783f1cb8cb969beea11cfd027
7
+ data.tar.gz: f88f864da8831648a261a8e38cb2ffa7859f9caed9b444688309ec7d3f833818ff92faaec4e83e74e3e82550d990255088fcc2cbc05ff77b12c2ae8a1ae5b32f
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Ember::Schema
1
+ # UnchartedCode Ember::Schema
2
2
 
3
3
  Generates a json schema for restpack_serializer models
4
4
 
@@ -18,7 +18,17 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ The schema will automatically be generated when you run db:migrate
22
+
23
+ ```
24
+ rake db:migrate
25
+ ```
26
+
27
+ and it will show up in db/schema.js. You can also fire it off manually like this
28
+
29
+ ```
30
+ rake db:schema:ember
31
+ ```
22
32
 
23
33
  ## Contributing
24
34
 
@@ -1,44 +1,58 @@
1
1
  require "rake"
2
2
  require "ember/schema/version"
3
+ require "ember/schema/rest_pack"
4
+ require "ember/schema/active_model"
3
5
 
4
6
  module Ember
5
7
  module Schema
6
- def self.generate
8
+ def self.generate(type)
9
+ return unless const_defined?("::#{type.to_s}")
10
+
7
11
  schema_hash = {}
8
- # Rails.application.eager_load! # populate descendants
9
12
 
10
- RestPack::Serializer.class_map.sort_by { |s| s[0] }.map { |s| s[1] }.each do |serializer_class|
13
+ case type.to_s
14
+ when "RestPack::Serializer"
15
+ generator = RestPack.new
16
+ when "ActiveModel::Serializer"
17
+ generator = ActiveModel.new
18
+ else
19
+ return
20
+ end
21
+
22
+ generator.serializers.each do |serializer_class|
11
23
  begin
12
24
  next if serializer_class == ApplicationSerializer
13
25
  next if serializer_class.respond_to?(:ignore) && serializer_class.ignore
14
- klass = get_klass(serializer_class)
26
+
27
+ klass = generator.get_klass(serializer_class)
15
28
  if klass.present?
16
29
  # if we are dealing with a polymorphic class, then process accordingly
17
30
  if klass.respond_to?(:base_class) && klass != klass.base_class
18
31
  p "--Skipping inherited class: '#{klass.name}', it will be processed with parent"
19
32
  next
20
33
  end
21
- schema = schema(serializer_class, klass)
22
- name = klass.name.camelize
34
+
35
+ schema = generator.schema(serializer_class, klass)
36
+ name = generator.camelize(serializer_class, klass)
23
37
  schema_hash[name] = schema
24
38
  p "#{klass.name}: Complete"
25
39
  # Check for inherited serializer classes now
26
- serializer_class.descendants.sort_by { |s| s.name }.each do |child_serializer_class|
40
+ generator.descendants(serializer_class).each do |child_serializer_class|
27
41
  begin
28
42
  # to be inherited, it has to have a subclass thats not the base and a klass that is inherited
29
43
  if child_serializer_class == ApplicationSerializer || # skip, default class
30
44
  child_serializer_class.superclass == ApplicationSerializer || # skip, base is default class, not inherited
31
- child_serializer_class.superclass == RestPack::Serializer || # skip, default class
45
+ child_serializer_class.superclass == generator.superclass || # skip, default class
32
46
  child_serializer_class == serializer_class # skip serializer is itself
33
47
  next
34
48
  end
35
- child_klass = get_klass(child_serializer_class)
49
+ child_klass = generator.get_klass(child_serializer_class)
36
50
  if child_klass.present?
37
51
  # if we are dealing with a polymorphic class, then process accordingly
38
52
  next unless child_klass.respond_to?(:base_class) # this is not active record
39
53
  next if child_klass == child_klass.base_class # this is the base class
40
54
  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)
55
+ diff_schema = inherited_schema(child_serializer_class, child_klass, schema, generator)
42
56
  child_name = child_klass.name.camelize
43
57
  # Modify parents schema
44
58
  schema[:descendants] ||= {}
@@ -62,61 +76,8 @@ module Ember
62
76
 
63
77
  private
64
78
 
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)
79
+ def self.inherited_schema(serializer, klass, base_schema, generator)
80
+ schema = generator.schema(serializer, klass)
120
81
 
121
82
  schema_diff = {}
122
83
  schema_diff[:attributes] = diff(base_schema[:attributes], schema[:attributes])
@@ -0,0 +1,84 @@
1
+ module Ember
2
+ module Schema
3
+ class ActiveModel
4
+ def superclass
5
+ ::ActiveModel::Serializer
6
+ end
7
+
8
+ def serializers
9
+ ::ActiveModel::Serializer.descendants.sort_by(&:final_name)
10
+ end
11
+
12
+ def descendants(serializer)
13
+ serializers
14
+ end
15
+
16
+ def get_klass(serializer)
17
+ module_name = serializer.name.deconstantize
18
+ class_name = serializer.root_name.camelize
19
+ full_name = [module_name, class_name].reject(&:empty?).join("::")
20
+ begin
21
+ return full_name.constantize
22
+ rescue => e
23
+ p " - Unable to find model: '#{full_name}'. Skipping schema processing."
24
+ return nil
25
+ end
26
+ end
27
+
28
+ def camelize(serializer, klass)
29
+ (serializer._root || klass.name).camelize
30
+ end
31
+
32
+ def schema(serializer, klass)
33
+ columns = if klass.respond_to? :columns_hash then klass.columns_hash else {} end
34
+
35
+ attrs = {}
36
+ serializer._attributes.each do |name|
37
+ option = serializer._options[name] || {}
38
+ if option[:type].present?
39
+ attrs[name] = option[:type]
40
+ else
41
+ # If no type is given, attempt to get it from the Active Model class
42
+ if column = columns[name.to_s]
43
+ attrs[name] = column.type
44
+ else
45
+ # Other wise default to string
46
+ attrs[name] = "string"
47
+ end
48
+ end
49
+ end
50
+
51
+ associations = {}
52
+ serializer._associations.each do |attr, association|
53
+ #association = association_class.new(attr, self)
54
+
55
+ if model_association = get_type(association)
56
+ # Real association.
57
+ association_class_name = association.options[:class_name]
58
+ key = association.key.gsub(/_id/,'')
59
+ associations[key] = { model_association => association_class_name || key }
60
+ associations[key][:async] = (association.options[:async] || false) if association.options.has_key? :async
61
+ associations[key][:polymorphic] = (association.options[:polymorphic] || false) if association.options.has_key? :polymorphic
62
+ else
63
+ # Computed association. We could infer has_many vs. has_one from
64
+ # the association class, but that would make it different from
65
+ # real associations, which read has_one vs. belongs_to from the
66
+ # model.
67
+ associations[association.key] = nil
68
+ end
69
+ end
70
+
71
+ return { :attributes => attrs, :associations => associations }
72
+ end
73
+
74
+ def get_type(association)
75
+ if association.is_a? ::ActiveModel::Serializer::Association::HasMany
76
+ return "has_many"
77
+ elsif association.is_a? ::ActiveModel::Serializer::Association::HasOne
78
+ return "belongs_to"
79
+ end
80
+ return nil
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,56 @@
1
+ module Ember
2
+ module Schema
3
+ class RestPack
4
+ def superclass
5
+ ::RestPack::Serializer
6
+ end
7
+
8
+ def serializers
9
+ ::RestPack::Serializer.class_map.sort_by { |s| s[0] }.map { |s| s[1] }
10
+ end
11
+
12
+ def descendants(serializer)
13
+ serializer.descendants.sort_by { |s| s.name }
14
+ end
15
+
16
+ def get_klass(serializer)
17
+ serializer.model_class
18
+ end
19
+
20
+ def camelize(serializer, klass)
21
+ klass.name.camelize
22
+ end
23
+
24
+ def schema(serializer, klass)
25
+ columns = if klass.respond_to? :columns_hash then klass.columns_hash else {} end
26
+
27
+ attrs = {}
28
+ (serializer.serializable_attributes || {}).each do |id, name|
29
+ options = serializer.serializable_attributes_options[id] || {}
30
+ if options[:type].present?
31
+ attrs[name] = options[:type].to_s
32
+ else
33
+ # If no type is given, attempt to get it from the Active Model class
34
+ if column = columns[name.to_s]
35
+ attrs[name] = column.type
36
+ else
37
+ # Other wise default to string
38
+ attrs[name] = "string"
39
+ end
40
+ end
41
+ end
42
+
43
+ associations = {}
44
+
45
+ serializer.can_include.each do |association_name|
46
+ if model_association = klass.reflect_on_association(association_name)
47
+ # Real association
48
+ associations[association_name] = { model_association.macro => model_association.class_name.pluralize.underscore.downcase }
49
+ end
50
+ end
51
+
52
+ return { :attributes => attrs, :associations => associations }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  module Ember
2
2
  module Schema
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -2,7 +2,13 @@ namespace :db do
2
2
  namespace :schema do
3
3
  desc 'Regenerate the Ember schema.js based on the serializers'
4
4
  task :ember => :environment do
5
- schema_hash = Ember::Schema.generate
5
+ if defined? RestPack::Serializer
6
+ schema_hash = Ember::Schema.generate(RestPack::Serializer)
7
+ else
8
+ Rails.application.eager_load! # populate descendants
9
+ schema_hash = Ember::Schema.generate(ActiveModel::Serializer)
10
+ end
11
+
6
12
  schema_json = JSON.pretty_generate(schema_hash)
7
13
  File.open 'db/schema.js', 'w' do |f|
8
14
  f << schema_json
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ember-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Palmer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-11 00:00:00.000000000 Z
12
+ date: 2015-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -67,6 +67,8 @@ files:
67
67
  - Rakefile
68
68
  - ember-schema.gemspec
69
69
  - lib/ember/schema.rb
70
+ - lib/ember/schema/active_model.rb
71
+ - lib/ember/schema/rest_pack.rb
70
72
  - lib/ember/schema/version.rb
71
73
  - lib/tasks/ember.rake
72
74
  homepage: ''