Module: Musa::Extension::AttributeBuilder
- Included in:
- MusicXML::Builder::Internal::Attributes, MusicXML::Builder::Internal::Direction, MusicXML::Builder::Internal::Measure, MusicXML::Builder::Internal::Part, MusicXML::Builder::ScorePartwise
- Defined in:
- lib/musa-dsl/core-ext/attribute-builder.rb
Instance Method Summary collapse
-
#attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) things -> (collection).
-
#attr_complex_adder_to_custom(name, plural: nil, variable: nil, &constructor_and_adder) ⇒ Object
add_thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) things -> (collection).
-
#attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object
thing value1, value2, key1: value3, key2: value4 -> crea Thing(value1, value2, key1: value3, key2: value3) thing -> Thing(value1, value2, key1: value3, key2: value4) y también…
-
#attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object
thing value -> crea Thing(value) thing -> Thing(value).
-
#attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing id, parameter things id1: parameter1, id2: parameter2 -> [ Thing(id1, parameter1), Thing(id2, parameter2) ] things -> [ Thing(id1, parameter1), Thing(id2, parameter2) ].
-
#attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing id, parameter things id1: parameter1, id2: parameter2 -> { id1: Thing(id1, parameter1), id2: Thing(id2, parameter2) } things -> { id1: Thing(id1, parameter1), id2: Thing(id2, parameter2) }.
-
#attr_tuple_builder(name, klass, variable: nil) ⇒ Object
thing id: value -> crea Thing(id, value) thing -> Thing(id, value).
Instance Method Details
#attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) things -> (collection)
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/musa-dsl/core-ext/attribute-builder.rb', line 58 def attr_complex_adder_to_array(name, klass, plural: nil, variable: nil) plural ||= name.to_s + 's' variable ||= ('@' + plural.to_s).to_sym adder_method = "add_#{name}".to_sym define_method adder_method do |*parameters, **key_parameters, &block| klass.new(*parameters, **key_parameters, &block).tap do |object| instance_variable_get(variable) << object end end if plural == name define_method plural do |*parameters, **key_parameters, &block| if parameters.empty? && key_parameters.empty? && block.nil? instance_variable_get variable else send adder_method, *parameters, **key_parameters, &block end end else alias_method name, adder_method define_method plural do instance_variable_get variable end end end |
#attr_complex_adder_to_custom(name, plural: nil, variable: nil, &constructor_and_adder) ⇒ Object
add_thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) thing param1, param2, key1: parameter1, key2: parameter2 -> Thing(…) things -> (collection)
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 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 93 def attr_complex_adder_to_custom(name, plural: nil, variable: nil, &constructor_and_adder) plural ||= name.to_s + 's' adder_method = "add_#{name}".to_sym define_method adder_method do |*parameters, **key_parameters, &block| instance_exec(*parameters, **key_parameters, &constructor_and_adder).tap do |object| object.with &block if block end end if plural == name && variable define_method plural do |*parameters, **key_parameters, &block| if parameters.empty? && key_parameters.empty? && block.nil? instance_variable_get variable else send adder_method, *parameters, **key_parameters, &block end end else alias_method name, adder_method if variable define_method plural do instance_variable_get variable end end end end |
#attr_complex_builder(name, klass, variable: nil, first_parameter: nil) ⇒ Object
thing value1, value2, key1: value3, key2: value4 -> crea Thing(value1, value2, key1: value3, key2: value3) thing -> Thing(value1, value2, key1: value3, key2: value4) y también… thing value1, value2, key1: value3, key2: value4 -> crea Thing(first_parameter, value1, value2, key1: value3, key2: value3) thing -> Thing(first_parameter, value1, value2, key1: value3, key2: value4)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 171 def attr_complex_builder(name, klass, variable: nil, first_parameter: nil) variable ||= ('@' + name.to_s).to_sym define_method name do |*parameters, **key_parameters, &block| if parameters.empty? && key_parameters.empty? && block.nil? instance_variable_get variable else if first_parameter klass.new(first_parameter, *parameters, **key_parameters, &block).tap do |object| instance_variable_set variable, object end else klass.new(*parameters, **key_parameters, &block).tap do |object| instance_variable_set variable, object end end end end attr_writer name end |
#attr_simple_builder(name, klass = nil, variable: nil) ⇒ Object
thing value -> crea Thing(value) thing -> Thing(value)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 127 def attr_simple_builder(name, klass = nil, variable: nil) variable ||= ('@' + name.to_s).to_sym define_method name do |parameter = nil, &block| if parameter.nil? instance_variable_get variable else (klass&.new(parameter, &block) || parameter).tap do |object| instance_variable_set variable, object end end end attr_writer name end |
#attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing id, parameter things id1: parameter1, id2: parameter2 -> [ Thing(id1, parameter1), Thing(id2, parameter2) ] things -> [ Thing(id1, parameter1), Thing(id2, parameter2) ]
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 33 def attr_tuple_adder_to_array(name, klass, plural: nil, variable: nil) plural ||= name.to_s + 's' variable ||= ('@' + plural.to_s).to_sym adder_method = "add_#{name}".to_sym define_method adder_method do |id, parameter, &block| klass.new(id, parameter, &block).tap do |object| instance_variable_get(variable) << object end end define_method plural do |**parameters, &block| parameters.each_pair do |id, value| send adder_method, id, value, &block end instance_variable_get variable end end |
#attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) ⇒ Object
add_thing id, parameter things id1: parameter1, id2: parameter2 -> { id1: Thing(id1, parameter1), id2: Thing(id2, parameter2) } things -> { id1: Thing(id1, parameter1), id2: Thing(id2, parameter2) }
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 8 def attr_tuple_adder_to_hash(name, klass, plural: nil, variable: nil) plural ||= name.to_s + 's' variable ||= ('@' + plural.to_s).to_sym adder_method = "add_#{name}".to_sym define_method adder_method do |id, parameter| klass.new(id, parameter).tap do |object| instance_variable_get(variable)[id] = object end end define_method plural do |**parameters| parameters&.each_pair do |id, value| send adder_method, id, value end instance_variable_get variable end end |
#attr_tuple_builder(name, klass, variable: nil) ⇒ Object
thing id: value -> crea Thing(id, value) thing -> Thing(id, value)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/musa-dsl/core-ext/attribute-builder.rb', line 146 def attr_tuple_builder(name, klass, variable: nil) variable ||= ('@' + name.to_s).to_sym define_method name do |**parameters, &block| raise ArgumentError, "Method #{name} can only create instances with one id: value arguments pattern" unless parameters.size == 1 if parameters.empty? instance_variable_get variable else parameter = parameters.first klass.new(*parameter, &block).tap do |object| instance_variable_set variable, object end end end attr_writer name end |