Module: Schema::Utils

Defined in:
lib/schema/utils.rb

Overview

Schema::Utils is a collection of common utility methods used in this gem

Class Method Summary collapse

Class Method Details

.add_association_class(base_schema_class, schema_name, schema_type, options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schema/utils.rb', line 35

def add_association_class(base_schema_class, schema_name, schema_type, options)
  options = ::Schema::Utils.association_options(schema_name, schema_type, options)
  kls = ::Schema::Utils.create_schema_class(
    base_schema_class,
    schema_name,
    options
  )
  add_association_defaults(kls, base_schema_class, schema_name)
  add_association_dynamic_types(kls, options)
  options
end

.add_association_default_methods(kls, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/schema/utils.rb', line 77

def add_association_default_methods(kls, options)
  kls.class_eval(
 "  def \#{options[:default_method]}\n\#{options[:default_code]}\n  end\n\n  def \#{options[:getter]}\n\#{options[:instance_variable]} ||= \#{options[:default_method]}\n  end\n", __FILE__, __LINE__ + 1
  )
end

.add_association_defaults(kls, base_schema_class, schema_name) ⇒ Object



47
48
49
50
51
# File 'lib/schema/utils.rb', line 47

def add_association_defaults(kls, base_schema_class, schema_name)
  kls.send(:include, ::Schema::Associations::Base)
  kls.base_schema_class = base_schema_class
  kls.schema_name = schema_name
end

.add_association_dynamic_types(kls, options) ⇒ Object



53
54
55
56
57
# File 'lib/schema/utils.rb', line 53

def add_association_dynamic_types(kls, options)
  return if !options[:type_field] && !options[:external_type_field]

  kls.send(:include, ::Schema::Associations::DynamicTypes)
end

.add_attribute_default_methods(kls, options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/schema/utils.rb', line 59

def add_attribute_default_methods(kls, options)
  kls.class_eval(
 "  def \#{options[:default_method]}\n\#{options[:default].inspect}\n  end\n\n  def \#{options[:getter]}\nif \#{options[:instance_variable]}.nil?\n  \#{options[:default_method]}\nelse\n  \#{options[:instance_variable]}\nend\n  end\n", __FILE__, __LINE__ + 1
  )
end

.association_options(schema_name, schema_type, options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/schema/utils.rb', line 27

def association_options(schema_name, schema_type, options)
  options[:class_name] ||= 'Schema' + classify_name(schema_type.to_s) + classify_name(schema_name.to_s)
  options[:association] = true
  options[:aliases] = [options[:alias]] if options.key?(:alias)
  options[:hash_key_field] ||= :id if options[:from] == :hash
  ::Schema::Model.default_attribute_options(schema_name, schema_type).merge(options)
end

.classify_name(name) ⇒ Object



8
9
10
# File 'lib/schema/utils.rb', line 8

def classify_name(name)
  name.gsub(/[^\da-z_-]/, '').gsub(/(^.|[_|-].)/) { |m| m[-1].upcase }
end

.create_schema_class(base_schema_class, schema_name, options) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/schema/utils.rb', line 12

def create_schema_class(base_schema_class, schema_name, options)
  base_schema_class.add_value_to_class_method(:schema, schema_name => options)
  kls = Class.new(options[:base_class] || Object)
  kls = base_schema_class.const_set(options[:class_name], kls)
  include_schema_modules(kls, base_schema_class.schema_config) unless options[:base_class]
  kls
end

.include_schema_modules(kls, schema_config) ⇒ Object



20
21
22
23
24
25
# File 'lib/schema/utils.rb', line 20

def include_schema_modules(kls, schema_config)
  kls.send(:include, ::Schema::Model)
  schema_config[:schema_includes].each do |mod|
    kls.schema_include(mod)
  end
end