Class: Netfira::WebConnect::Schema::Table
- Inherits:
-
Object
- Object
- Netfira::WebConnect::Schema::Table
- Defined in:
- lib/netfira/web_connect/schema/table.rb
Constant Summary collapse
- IGNORE_COLUMNS =
%w[id shop_id created_at updated_at deleted_at digest]
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #as_json(options) ⇒ Object
- #columns ⇒ Object
- #has_many ⇒ Object
-
#initialize(klass) ⇒ Table
constructor
A new instance of Table.
- #localize ⇒ Object
- #plural ⇒ Object
- #primary_key ⇒ Object
- #relations ⇒ Object
- #singular ⇒ Object
Constructor Details
#initialize(klass) ⇒ Table
Returns a new instance of Table.
7 8 9 |
# File 'lib/netfira/web_connect/schema/table.rb', line 7 def initialize(klass) @klass = klass end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/netfira/web_connect/schema/table.rb', line 5 def klass @klass end |
Instance Method Details
#as_json(options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/netfira/web_connect/schema/table.rb', line 45 def as_json() @json ||= { columns: columns.map { |name, type| [name.to_s.camelize(:lower), type] }.to_h, relations: relations.map{ |klass| klass.name.demodulize.pluralize }, singular: singular, primaryKey: [primary_key.to_s.camelize(:lower)], localize: l10n_columns.keys.map{ |k| k.camelize :lower }, hasMany: has_many.map{ |type, klass| [type, klass.plural_name.camelize] }.to_h } end |
#columns ⇒ Object
41 42 43 |
# File 'lib/netfira/web_connect/schema/table.rb', line 41 def columns @columns ||= klass.columns_hash.reject{ |k| IGNORE_COLUMNS.include? k }.merge(l10n_columns).map{ |k, v| [k.to_sym, v.type.to_s] }.to_h end |
#has_many ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/netfira/web_connect/schema/table.rb', line 31 def has_many @has_many ||= klass.reflect_on_all_associations(:has_many).select do |a| a.klass < Model::Record && !a.[:through] end.map do |association| [association.name, association.klass] end.to_h end |
#localize ⇒ Object
23 24 25 |
# File 'lib/netfira/web_connect/schema/table.rb', line 23 def localize @localize ||= l10n_columns.keys.map(&:to_sym) end |
#plural ⇒ Object
15 16 17 |
# File 'lib/netfira/web_connect/schema/table.rb', line 15 def plural @plural ||= singular.pluralize end |
#primary_key ⇒ Object
19 20 21 |
# File 'lib/netfira/web_connect/schema/table.rb', line 19 def primary_key @primary_key ||= klass.origin_key end |
#relations ⇒ Object
27 28 29 |
# File 'lib/netfira/web_connect/schema/table.rb', line 27 def relations @relations = klass. end |
#singular ⇒ Object
11 12 13 |
# File 'lib/netfira/web_connect/schema/table.rb', line 11 def singular @singular ||= klass.name.demodulize end |