Module: Jat::ClassMethods

Included in:
Jat
Defined in:
lib/jat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/jat.rb', line 21

def config
  @config
end

Instance Method Details

#attribute(name, **opts, &block) ⇒ Object



86
87
88
89
# File 'lib/jat.rb', line 86

def attribute(name, **opts, &block)
  new_attr = self::Attribute.new(name: name, opts: opts, block: block)
  attributes[new_attr.name] = new_attr
end

#attributesObject



82
83
84
# File 'lib/jat.rb', line 82

def attributes
  @attributes ||= {}
end

#callObject



74
75
76
# File 'lib/jat.rb', line 74

def call
  self
end

#inherited(subclass) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jat.rb', line 23

def inherited(subclass)
  # Initialize config
  config_class = Class.new(self::Config)
  config_class.jat_class = subclass
  subclass.const_set(:Config, config_class)
  subclass.instance_variable_set(:@config, subclass::Config.new(config.opts))

  # Initialize attribute class
  attribute_class = Class.new(self::Attribute)
  attribute_class.jat_class = subclass
  subclass.const_set(:Attribute, attribute_class)

  # Assign same attributes
  attributes.each_value do |attribute|
    params = attribute.params
    subclass.attribute(params[:name], **params[:opts], &params[:block])
  end

  super
end

#plugin(name, **opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jat.rb', line 44

def plugin(name, **opts)
  return if plugin_used?(name)

  plugin = Plugins.find_plugin(name)

  # We split loading of plugin to three methods - before_load, load, after_load:
  #
  # - **before_load** usually used to check requirements and to load additional plugins
  # - **load** usually used to include plugin modules
  # - **after_load** usually used to add config options
  plugin.before_load(self, **opts) if plugin.respond_to?(:before_load)
  plugin.load(self, **opts) if plugin.respond_to?(:load)
  plugin.after_load(self, **opts) if plugin.respond_to?(:after_load)

  # Store attached plugins, so we can check it is loaded later
  config[:plugins] << (plugin.respond_to?(:plugin_name) ? plugin.plugin_name : plugin)

  plugin
end

#plugin_used?(plugin) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
# File 'lib/jat.rb', line 64

def plugin_used?(plugin)
  plugin_name =
    case plugin
    when Module then plugin.respond_to?(:plugin_name) ? plugin.plugin_name : plugin
    else plugin
    end

  config[:plugins].include?(plugin_name)
end

#relationship(name, serializer:, **opts, &block) ⇒ Object



91
92
93
# File 'lib/jat.rb', line 91

def relationship(name, serializer:, **opts, &block)
  attribute(name, serializer: serializer, **opts, &block)
end

#to_h(object, context = nil) ⇒ Object



78
79
80
# File 'lib/jat.rb', line 78

def to_h(object, context = nil)
  new(context || {}).to_h(object)
end