Class: Ree::ObjectDsl
- Includes:
- Args
- Defined in:
- lib/ree/dsl/object_dsl.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Instance Method Summary collapse
- #after_init(method_name) ⇒ Object
- #factory(method_name) ⇒ Object
- #freeze(flag) ⇒ Object
- #import(*args, **kwargs) ⇒ Object
-
#initialize(packages_facade, klass, name, mount_as) ⇒ ObjectDsl
constructor
A new instance of ObjectDsl.
-
#link(*args, **kwargs) ⇒ Object
Proxy method for link_object & link_file.
- #link_file(path, import_proc = nil) ⇒ Object
- #link_multiple_objects(object_names, **kwargs) ⇒ Object
- #link_object(object_name, as: nil, from: nil, import: nil, target: nil) ⇒ Object
- #singleton ⇒ Object
- #tags(list) ⇒ Object
- #target(val) ⇒ Object
- #with_caller ⇒ Object
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.
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
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/ree/dsl/object_dsl.rb', line 8 def object @object end |
#package ⇒ Object (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
164 165 166 167 168 169 170 171 |
# File 'lib/ree/dsl/object_dsl.rb', line 164 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ree/dsl/object_dsl.rb', line 138 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
174 175 176 177 178 179 180 181 |
# File 'lib/ree/dsl/object_dsl.rb', line 174 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 |
#import(*args, **kwargs) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ree/dsl/object_dsl.rb', line 49 def import(*args, **kwargs) if args.first.is_a?(Symbol) # import from ree object _import_from_object(*args, **kwargs) else _import_object_consts(*args, **kwargs) end end |
#link(*args, **kwargs) ⇒ Object
Proxy method for link_object & link_file
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ree/dsl/object_dsl.rb', line 31 def link(*args, **kwargs) if args.first.is_a?(Symbol) if args.size > 1 link_multiple_objects(args, **kwargs) else link_object(*args, **kwargs) end 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 |
#link_file(path, import_proc = nil) ⇒ Object
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ree/dsl/object_dsl.rb', line 185 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 |
#link_multiple_objects(object_names, **kwargs) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ree/dsl/object_dsl.rb', line 103 def link_multiple_objects(object_names, **kwargs) check_arg(kwargs[:from], :from, Symbol) if kwargs[:from] if kwargs.reject{ |k, _v| k == :from }.size > 0 raise Ree::Error.new("options #{kwargs.reject{ |k, _v| k == :from }.keys} are not allowed for multi-object links", :invalid_link_option) end object_names.each do |object_name| link_object(object_name, from: kwargs[:from]) end end |
#link_object(object_name, as: nil, from: nil, import: nil, target: nil) ⇒ Object
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 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ree/dsl/object_dsl.rb', line 62 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 |
#singleton ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/ree/dsl/object_dsl.rb', line 155 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
45 46 47 |
# File 'lib/ree/dsl/object_dsl.rb', line 45 def (list) @object.(list) end |
#target(val) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/ree/dsl/object_dsl.rb', line 116 def target(val) check_arg(val, :target, Symbol) check_target(val) @object.set_target(val) end |
#with_caller ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ree/dsl/object_dsl.rb', line 123 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 |