Class: Ree::ObjectDsl

Inherits:
Object show all
Includes:
Args
Defined in:
lib/ree/dsl/object_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Args

#check_arg, #check_arg_any, #check_arg_array_of, #check_bool, #not_nil

Constructor Details

#initialize(packages_facade, klass, name, mount_as) ⇒ ObjectDsl

Returns a new instance of ObjectDsl.

Parameters:

  • packages_facade (Ree::PackagesFacade)
  • klass (Class)
  • name (Symbol)
  • abs_path (Nilor[String])
  • mount_as (Symbol)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ree/dsl/object_dsl.rb', line 15

def initialize(packages_facade, klass, name, mount_as)
  @packages_facade = packages_facade

  @object = register_object(
    klass, name, mount_as,
  )

  @package = @packages_facade.get_loaded_package(@object.package_name)

  if @package.default_links
    instance_exec(&@package.default_links)
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



8
9
10
# File 'lib/ree/dsl/object_dsl.rb', line 8

def object
  @object
end

#packageObject (readonly)

Returns the value of attribute package.



8
9
10
# File 'lib/ree/dsl/object_dsl.rb', line 8

def package
  @package
end

Instance Method Details

#after_init(method_name) ⇒ Object

Parameters:

  • method_name (Symbol)


137
138
139
140
141
142
143
144
# File 'lib/ree/dsl/object_dsl.rb', line 137

def after_init(method_name)
  if @object.factory?
    raise_error("Factory beans do not support after_init DSL")
  end

  check_arg(method_name, :method_name, Symbol)
  @object.set_after_init(method_name)
end

#factory(method_name) ⇒ Object

Parameters:

  • method_name (Symbol)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ree/dsl/object_dsl.rb', line 111

def factory(method_name)
  if !@object.object?
    raise_error("Factory methods only available for beans")
  end

  if @object.after_init
    raise_error("Factory beans do not support after_init DSL")
  end

  if @object.with_caller?
    raise_error("Factory beans do not support with_caller DSL")
  end

  check_arg(method_name, :method_name, Symbol)
  @object.set_factory(method_name)
end

#freeze(flag) ⇒ Object

Parameters:

  • flag (Bool)


147
148
149
150
151
152
153
154
# File 'lib/ree/dsl/object_dsl.rb', line 147

def freeze(flag)
  if @object.with_caller? && flag
    raise_error("`freeze` should not be combined with `with_caller`")
  end

  check_bool(flag, :flag)
  @object.set_freeze(flag)
end

Proxy method for link_object & link_file



30
31
32
33
34
35
36
37
38
# File 'lib/ree/dsl/object_dsl.rb', line 30

def link(*args, **kwargs)
  if args.first.is_a?(Symbol)
    link_object(*args, **kwargs)
  elsif args.first.is_a?(String)
    link_file(args[0], args[1])
  else
    raise_error("Invalid link DSL usage. Args should be Hash or String")
  end
end

Parameters:

  • path (String)

    Relative package file path (‘accounts/entities/user’)

  • proc (Proc)

    Import constants proc



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ree/dsl/object_dsl.rb', line 158

def link_file(path, import_proc = nil)
  check_arg(import_proc, :import, Proc) if import_proc

  list = path.split('/')
  package_name = File.basename(list[0], ".*").to_sym

  check_package_dependency_added(package_name)

  @packages_facade.load_package_entry(package_name)
  package = @packages_facade.get_loaded_package(package_name)

  file_path = File.join(
    Ree::PathHelper.abs_package_dir(package),
    Ree::PACKAGE, path
  )

  if !File.exist?(file_path)
    file_path = "#{file_path}.rb"

    if !File.exist?(file_path)
      raise_error("Unable to link '#{path}'. File not found #{file_path}")
    end
  end

  @packages_facade.load_file(file_path, package.name)

  const_list = path.split('/').map { |_| Ree::StringUtils.camelize(_) }
  const_short = [const_list[0], const_list.last].join("::")
  const_long = const_list.join("::")

  file_const = if Object.const_defined?(const_long)
    Object.const_get(const_long)
  elsif Object.const_defined?(const_short)
    Object.const_get(const_short)
  else
    raise_error("Unable to link '#{path}'. #{const_long} or #{const_short} was not defined in #{file_path}")
  end

  const_list = if import_proc
    Ree::LinkImportBuilder
      .new(@packages_facade)
      .build_for_const(
        @object.klass,
        file_const,
        import_proc
      )
  end

  if const_list
    @object.add_const_list(const_list.map(&:name))
  end

  file_const
end

Parameters:

  • object_name (Symbol)
  • as (Nilor[Symbol]) (defaults to: nil)
  • from (Nilor[Symbol]) (defaults to: nil)
  • import (Nilor[Proc]) (defaults to: nil)
  • import (Or[:object, :class, :both]) (defaults to: nil)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ree/dsl/object_dsl.rb', line 49

def link_object(object_name, as: nil, from: nil, import: nil, target: nil)
  check_arg(object_name, :object_name, Symbol)
  check_arg(as, :as, Symbol) if as
  check_arg(from, :from, Symbol) if from
  check_arg(import, :import, Proc) if import
  check_target(target) if target

  link_package_name = from.nil? ? @object.package_name : from
  link_object_name = object_name
  link_as = as ? as : object_name

  check_package_dependency_added(link_package_name)

  const_list = if import
    Ree::LinkImportBuilder
      .new(@packages_facade)
      .build(
        @object.klass,
        link_package_name,
        link_object_name,
        import
      )
  end

  link = Ree::ObjectLink.new(
    link_object_name, link_package_name, link_as, target
  )

  if const_list
    link.set_constants(const_list)
    @object.add_const_list(const_list)
  end

  @object.links.push(link)
  Ree.logger.debug("  #{@object.klass}.link(:#{link_object_name}, from: #{link_package_name}, as: #{link_as})")

  @packages_facade.load_package_object(link_package_name, link_object_name)
end

#singletonObject



128
129
130
131
132
133
134
# File 'lib/ree/dsl/object_dsl.rb', line 128

def singleton
  if @object.with_caller?
    raise_error("`singleton` should not be combined with `with_caller`")
  end

  @object.set_as_singleton
end

#tags(list) ⇒ Object



40
41
42
# File 'lib/ree/dsl/object_dsl.rb', line 40

def tags(list)
  @object.add_tags(list)
end

#target(val) ⇒ Object

Parameters:

  • target (Symbol)

    (:object, :class, :both, default: :object)



89
90
91
92
93
94
# File 'lib/ree/dsl/object_dsl.rb', line 89

def target(val)
  check_arg(val, :target, Symbol)
  check_target(val)

  @object.set_target(val)
end

#with_callerObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ree/dsl/object_dsl.rb', line 96

def with_caller
  @object.set_freeze(false)

  if @object.singleton?
    raise_error("`with_caller` is not available for singletons")
  end

  if @object.factory?
    raise_error("`with_caller` is not available for factory beans")
  end

  @object.set_as_with_caller
end