Module: Foobara::Model::Concerns::Types::ClassMethods
- Defined in:
- foobara-0.5.8/projects/entities/projects/model/src/concerns/types.rb
Instance Attribute Summary collapse
-
#attributes_type ⇒ Object
writeonly
Sets the attribute attributes_type.
-
#model_type ⇒ Object
Returns the value of attribute model_type.
Instance Method Summary collapse
- #attributes ⇒ Object
- #delegate_attribute(attribute_name, data_path, writer: false) ⇒ Object
- #delegate_attributes(delegates) ⇒ Object
- #foobara_attributes_type ⇒ Object (also: #attributes_type)
- #foobara_delegates ⇒ Object
- #foobara_private_attribute_names ⇒ Object
- #foobara_type ⇒ Object
- #has_delegated_attributes? ⇒ Boolean
- #mutable(*args) ⇒ Object
- #private_attribute(attribute_name) ⇒ Object
- #private_attributes(attribute_names) ⇒ Object
- #set_model_type ⇒ Object
- #type_declaration(attributes_declaration) ⇒ Object
Instance Attribute Details
#attributes_type=(value) ⇒ Object (writeonly)
Sets the attribute attributes_type
13 14 15 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 13 def attributes_type=(value) @attributes_type = value end |
#model_type ⇒ Object
Returns the value of attribute model_type.
12 13 14 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 12 def model_type @model_type end |
Instance Method Details
#attributes ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 40 def attributes(...) private, attributes_type_declaration = extract_private_from_attributes_declaration(...) new_type = domain.(attributes_type_declaration) existing_type = attributes_type if existing_type declaration = TypeDeclarations::Attributes.merge( existing_type.declaration_data, new_type.declaration_data ) new_type = domain.(declaration) end self.attributes_type = new_type private_attributes(private) set_model_type end |
#delegate_attribute(attribute_name, data_path, writer: false) ⇒ Object
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 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 197 def delegate_attribute(attribute_name, data_path, writer: false) if data_path.is_a?(::Symbol) || data_path.is_a?(::String) data_path = [data_path, attribute_name] end data_path = DataPath.for(data_path) delegate_manifest = { data_path: data_path.to_s } if writer delegate_manifest[:writer] = true end delegates[attribute_name] = delegate_manifest delegated_type_declaration = model_type.type_at_path(data_path).reference_or_declaration_data # TODO: add defaults and required for the delegated declaration! attributes(type: :attributes, element_type_declarations: { attribute_name => delegated_type_declaration }) define_method attribute_name do data_path.value_at(self) end if writer define_method "#{attribute_name}=" do |value| data_path.set_value_at(self, value) end else method = :"#{attribute_name}=" if method_defined?(method) # TODO: test this code path # :nocov: remove_method method # :nocov: end end set_model_type end |
#delegate_attributes(delegates) ⇒ Object
190 191 192 193 194 195 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 190 def delegate_attributes(delegates) delegates.each_pair do |attribute_name, delegate_info| data_path = DataPath.for(delegate_info[:data_path]) delegate_attribute(attribute_name, data_path, writer: delegate_info[:writer]) end end |
#foobara_attributes_type ⇒ Object Also known as: attributes_type
126 127 128 129 130 131 132 133 134 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 126 def return @attributes_type if @attributes_type @attributes_type = if model_type model_type.element_types elsif ancestors.find { |ancestor| ancestor < Model } superclass.attributes_type end end |
#foobara_delegates ⇒ Object
166 167 168 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 166 def @foobara_delegates ||= {} end |
#foobara_private_attribute_names ⇒ Object
174 175 176 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 174 def @foobara_private_attribute_names ||= [] end |
#foobara_type ⇒ Object
82 83 84 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 82 def model_type end |
#has_delegated_attributes? ⇒ Boolean
170 171 172 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 170 def has_delegated_attributes? !delegates.empty? end |
#mutable(*args) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 15 def mutable(*args) args_size = args.size case args.size when 0 if defined?(@mutable_override) @mutable_override else type = model_type if type if type.declaration_data.key?(:mutable) type.declaration_data[:mutable] end end end when 1 @mutable_override = args.first set_model_type else # :nocov: raise ArgumentError, "Expected 0 or 1 arguments but got #{args_size}" # :nocov: end end |
#private_attribute(attribute_name) ⇒ Object
184 185 186 187 188 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 184 def private_attribute(attribute_name) @foobara_private_attribute_names = private_attribute_names | [attribute_name] set_model_type end |
#private_attributes(attribute_names) ⇒ Object
178 179 180 181 182 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 178 def private_attributes(attribute_names) attribute_names.each do |attribute_name| private_attribute attribute_name end end |
#set_model_type ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 61 def set_model_type return if abstract? if attributes_type declaration = type_declaration(attributes_type.declaration_data) if model_type unless Foobara::TypeDeclarations.declarations_equal?(declaration.declaration_data, model_type.declaration_data) type_domain = domain self.model_type = nil type_domain.(declaration) end else domain.(declaration) end end end |
#type_declaration(attributes_declaration) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'projects/entities/projects/model/src/concerns/types.rb', line 86 def type_declaration(attributes_declaration) if model_type model_module_name = model_type.declaration_data[:model_module] model_class = model_type.declaration_data[:model_class] model_name = model_type.scoped_name model_base_class = superclass.name || superclass.model_type.scoped_full_name else model_base_class = superclass.name || superclass.full_model_name model_class = name || full_model_name if model_class.start_with?(closest_namespace_module.name) model_module_name = closest_namespace_module.name model_name = model_class.gsub(/^#{closest_namespace_module.name}::/, "") else model_module_name = nil model_name = model_class end end type_declaration = TypeDeclaration.new( Util.remove_blank( type: :model, name: model_name, model_module: model_module_name, model_class:, model_base_class:, attributes_declaration:, description:, mutable:, delegates:, private: private_attribute_names ) ) type_declaration.is_absolutified = true type_declaration.is_duped = true type_declaration end |