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.



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

def config
  @config
end

Instance Method Details

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



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

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

#attributesObject



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

def attributes
  @attributes ||= {}
end

#callObject



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

def call
  self
end

#inherited(subclass) ⇒ Object



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

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



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

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)


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

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



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

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

#to_h(object, context = nil) ⇒ Object



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

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