Module: GlueGun::DSL::ClassMethods
- Extended by:
- Shared
- Defined in:
- lib/glue_gun/dsl.rb
Constant Summary collapse
- DEFAULT_TYPE =
if ActiveModel.version >= Gem::Version.new("7") nil elsif ActiveModel.version >= Gem::Version.new("5") ActiveModel::Type::Value.new end
Instance Method Summary collapse
-
#attribute(name, type = DEFAULT_TYPE, **options) ⇒ Object
Override the attribute method to define custom setters.
- #dependency(component_type, options = {}, factory_class = nil, &block) ⇒ Object
- #detect_root_dir ⇒ Object
- #inherited(subclass) ⇒ Object
Instance Method Details
#attribute(name, type = DEFAULT_TYPE, **options) ⇒ Object
Override the attribute method to define custom setters
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/glue_gun/dsl.rb', line 92 def attribute(name, type = DEFAULT_TYPE, **) super(name, type, **) attribute_definitions[name.to_sym] = { type: type, options: } # Define dirty tracking for the attribute define_attribute_methods name unless ancestors.include?(ActiveRecord::Base) attribute_methods_module = const_get(:AttributeMethods) attribute_methods_module.class_eval do define_method "#{name}=" do |value| value = super(value) propagate_attribute_change(name, value) if initialized? end end end |
#dependency(component_type, options = {}, factory_class = nil, &block) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/glue_gun/dsl.rb', line 109 def dependency(component_type, = {}, factory_class = nil, &block) if .is_a?(Class) factory_class = = {} end dependency_builder = DependencyBuilder.new(component_type, ) if factory_class.present? dependency_builder.set_factory_class(factory_class) else dependency_builder.instance_eval(&block) end dependency_definitions[component_type] = dependency_builder # Define singleton method to allow hardcoding dependencies in subclasses define_singleton_method component_type do |option = nil, = {}| if option.is_a?(Hash) && .empty? = option option = nil end option ||= dependency_builder.default_option_name hardcoded_dependencies[component_type] = { option_name: option, value: } end define_method component_type do instance_variable_get("@#{component_type}") || instance_variable_set("@#{component_type}", initialize_dependency(component_type)) end attribute_methods_module = const_get(:AttributeMethods) attribute_methods_module.class_eval do define_method "#{component_type}=" do |init_args| instance_variable_set("@#{component_type}", initialize_dependency(component_type, init_args)) end end end |
#detect_root_dir ⇒ Object
163 164 165 166 |
# File 'lib/glue_gun/dsl.rb', line 163 def detect_root_dir base_path = Module.const_source_location(self.class.name)&.first || "" File.dirname(base_path) end |
#inherited(subclass) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/glue_gun/dsl.rb', line 147 def inherited(subclass) super subclass.attribute_definitions = attribute_definitions.deep_dup subclass.dependency_definitions = dependency_definitions.deep_dup subclass.hardcoded_dependencies = hardcoded_dependencies.deep_dup # Prepend the AttributeMethods module to the subclass attribute_methods_module = const_get(:AttributeMethods) subclass.prepend(attribute_methods_module) # Prepend ClassMethods into the singleton class of the subclass class << subclass prepend ClassMethods end end |