Class: WellActually::Mounter
- Inherits:
-
Object
- Object
- WellActually::Mounter
- Defined in:
- lib/well_actually/mounter.rb
Constant Summary collapse
- BLANK_RE =
defined?(String::BLANK_RE) ? String::BLANK_RE : /\A[[:space:]]*\z/
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#overwrites ⇒ Object
readonly
Returns the value of attribute overwrites.
-
#strick ⇒ Object
readonly
Returns the value of attribute strick.
Instance Method Summary collapse
-
#initialize(klass:, overwrites: nil, overwrite: nil, attributes:, strict: false) ⇒ Mounter
constructor
A new instance of Mounter.
- #mount ⇒ Object
Constructor Details
#initialize(klass:, overwrites: nil, overwrite: nil, attributes:, strict: false) ⇒ Mounter
Returns a new instance of Mounter.
7 8 9 10 11 12 13 14 15 |
# File 'lib/well_actually/mounter.rb', line 7 def initialize(klass:, overwrites: nil, overwrite: nil, attributes:, strict:false) overwrites = overwrite || overwrites raise ArgumentError.new("overwrite must be a symbol or an array of symbols") unless overwrites.is_a?(Symbol) || (overwrites.is_a?(Array) && overwrites.all?{|a| a.is_a?(Symbol)}) raise ArgumentError.new("attributes must be an array of symbols") unless attributes.is_a?(Array) && attributes.all?{|a| a.is_a?(Symbol)} @klass = klass @overwrites = overwrites @attributes = attributes.uniq @strict = strict end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/well_actually/mounter.rb', line 3 def attributes @attributes end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/well_actually/mounter.rb', line 3 def klass @klass end |
#overwrites ⇒ Object (readonly)
Returns the value of attribute overwrites.
3 4 5 |
# File 'lib/well_actually/mounter.rb', line 3 def overwrites @overwrites end |
#strick ⇒ Object (readonly)
Returns the value of attribute strick.
3 4 5 |
# File 'lib/well_actually/mounter.rb', line 3 def strick @strick end |
Instance Method Details
#mount ⇒ Object
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 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/well_actually/mounter.rb', line 17 def mount klass.class_variable_set(:@@well_actually_attributes, attributes.map{|_attr| _attr.to_s}) klass.class_variable_set(:@@well_actually_overwrites, [*overwrites]) klass.class_variable_set(:@@well_actually_strict, strick) klass.send(:define_singleton_method, :well_actually_attributes) do self.class_variable_get(:@@well_actually_attributes) end klass.send(:define_method, :well_actually_attributes) do self.class.well_actually_attributes end klass.send(:define_singleton_method, :well_actually_overwrites) do self.class_variable_get(:@@well_actually_overwrites) end klass.send(:define_singleton_method, :well_actually_strict) do self.class_variable_get(:@@well_actually_strict) end klass.send(:define_method, :well_actually_overwrites) do self.class.well_actually_overwrites.map do |overwrite| self.public_send(overwrite) if (self.respond_to?(overwrite) || self.class.well_actually_strict) end.compact.reduce({}) do |result, overwrite| result.merge(overwrite || {}) do |key, v1, v2| v1 = nil unless self.well_actually_overwrite_value_check(key, v1) v2 = nil unless self.well_actually_overwrite_value_check(key, v2) v1.nil? ? v2 : v1 end end end klass.send(:define_method, :well_actually?) do !!self.instance_variable_get(:@well_actually) end klass.send(:define_method, :well_actually) do self.clone.tap do |actually| actually.readonly! if actually.respond_to?(:readonly!) actually.instance_variable_set(:@well_actually, true) self.send(:well_actually_overwrite_safe).each do |_attr, _value| if actually.respond_to?(:"#{_attr}=") actually.public_send(:"#{_attr}=", _value) else actually.instance_variable_set(:"@#{_attr}", _value) end end end end klass.send(:define_method, :well_actually_overwrite_safe) do (self.well_actually_overwrites || {}).select do |key, value| self.well_actually_overwrite_value_check(key, value) end end klass.send(:define_method, :well_actually_overwrite_value_check) do |key, value| self.well_actually_attributes.include?(key) && !value.nil? && (!value.is_a?(String) || !value.match(BLANK_RE)) end end |