Module: OptionsModel::Concerns::Attributes::ClassMethods
- Defined in:
- lib/options_model/concerns/attributes.rb
Instance Method Summary collapse
- #attribute(name, cast_type, default: nil, array: false) ⇒ Object
- #attribute_defaults ⇒ Object
- #attribute_names ⇒ Object
- #attribute_names_for_inlining ⇒ Object
- #attribute_names_for_nesting ⇒ Object
- #embeds_one(name, class_name: nil, anonymous_class: nil) ⇒ Object
- #enum_attribute(name, enum, default: nil, allow_nil: false) ⇒ Object
- #finalize!(nested = true) ⇒ Object
- #finalized? ⇒ Boolean
- #nested_classes ⇒ Object
Instance Method Details
#attribute(name, cast_type, default: nil, array: false) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/options_model/concerns/attributes.rb', line 9 def attribute(name, cast_type, default: nil, array: false) check_not_finalized! name = name.to_sym check_name_validity! name ActiveModel::Type.lookup(cast_type) attribute_defaults[name] = default default_extractor = case when default.respond_to?(:call) ".call" when default.duplicable? ".deep_dup" else "" end generated_attribute_methods.synchronize do generated_attribute_methods.module_eval " def \#{name}\n value = attributes[:\#{name}]\n return value unless value.nil?\n attributes[:\#{name}] = self.class.attribute_defaults[:\#{name}]\#{default_extractor}\n attributes[:\#{name}]\n end\n STR\n\n if array\n generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1\n def \#{name}=(value)\n if value.respond_to?(:to_a)\n attributes[:\#{name}] = value.to_a.map { |i| ActiveModel::Type.lookup(:\#{cast_type}).cast(i) }\n elsif value.nil?\n attributes[:\#{name}] = self.class.attribute_defaults[:\#{name}]\#{default_extractor}\n else\n raise ArgumentError,\n \"`value` should respond to `to_a`, but got \\\#{value.class} -- \\\#{value.inspect}\"\n end\n end\n STR\n else\n generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1\n def \#{name}=(value)\n attributes[:\#{name}] = ActiveModel::Type.lookup(:\#{cast_type}).cast(value)\n end\n STR\n\n if cast_type == :boolean\n generated_attribute_methods.send :alias_method, :\"\#{name}?\", name\n end\n end\n end\n\n self.attribute_names_for_inlining << name\n\n self\nend\n", __FILE__, __LINE__ + 1 |
#attribute_defaults ⇒ Object
138 139 140 |
# File 'lib/options_model/concerns/attributes.rb', line 138 def attribute_defaults @attribute_defaults ||= ActiveSupport::HashWithIndifferentAccess.new end |
#attribute_names ⇒ Object
154 155 156 |
# File 'lib/options_model/concerns/attributes.rb', line 154 def attribute_names attribute_names_for_nesting + attribute_names_for_inlining end |
#attribute_names_for_inlining ⇒ Object
150 151 152 |
# File 'lib/options_model/concerns/attributes.rb', line 150 def attribute_names_for_inlining @attribute_names_for_inlining ||= Set.new end |
#attribute_names_for_nesting ⇒ Object
146 147 148 |
# File 'lib/options_model/concerns/attributes.rb', line 146 def attribute_names_for_nesting @attribute_names_for_nesting ||= Set.new end |
#embeds_one(name, class_name: nil, anonymous_class: nil) ⇒ Object
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 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/options_model/concerns/attributes.rb', line 93 def (name, class_name: nil, anonymous_class: nil) check_not_finalized! if class_name.blank? && anonymous_class.nil? raise ArgumentError, "must provide at least one of `class_name` or `anonymous_class`" end name = name.to_sym check_name_validity! name if class_name.present? nested_classes[name] = class_name.constantize else nested_classes[name] = anonymous_class end generated_attribute_methods.synchronize do generated_attribute_methods.module_eval " def \#{name}\n nested_attributes[:\#{name}] ||= self.class.nested_classes[:\#{name}].new(attributes[:\#{name}])\n end\n STR\n\n generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1\n def \#{name}=(value)\n klass = self.class.nested_classes[:\#{name}]\n if value.respond_to?(:to_h)\n nested_attributes[:\#{name}] = klass.new(value.to_h)\n elsif value.is_a? klass\n nested_attributes[:\#{name}] = value\n elsif value.nil?\n nested_attributes[:\#{name}] = klass.new\n else\n raise ArgumentError,\n \"`value` should respond to `to_h` or \\\#{klass}, but got \\\#{value.class}\"\n end\n end\n STR\n end\n\n self.attribute_names_for_nesting << name\n\n self\nend\n", __FILE__, __LINE__ + 1 |
#enum_attribute(name, enum, default: nil, allow_nil: false) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/options_model/concerns/attributes.rb', line 69 def enum_attribute(name, enum, default: nil, allow_nil: false) check_not_finalized! unless enum.is_a?(Array) && enum.any? raise ArgumentError, "enum should be an Array and can't empty" end enum = enum.map(&:to_s) attribute name, :string, default: default pluralized_name = name.to_s.pluralize generated_class_methods.synchronize do generated_class_methods.module_eval " def \#{pluralized_name}\n %w(\#{enum.join(\" \")}).freeze\n end\n STR\n\n validates name, inclusion: {in: enum}, allow_nil: allow_nil\n end\n\n self\nend\n", __FILE__, __LINE__ + 1 |
#finalize!(nested = true) ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/options_model/concerns/attributes.rb', line 162 def finalize!(nested = true) if nested nested_classes.values.each(&:finalize!) end @finalized = true end |
#finalized? ⇒ Boolean
158 159 160 |
# File 'lib/options_model/concerns/attributes.rb', line 158 def finalized? @finalized ||= false end |
#nested_classes ⇒ Object
142 143 144 |
# File 'lib/options_model/concerns/attributes.rb', line 142 def nested_classes @nested_classes ||= ActiveSupport::HashWithIndifferentAccess.new end |