module Ecoportal::API::Common::Content::ClassHelpers
Constants
- NOT_USED
Public Instance Methods
Builds the attr_reader and attr_writer of `attrs` and registers the associated instance variable as inheritable.
# File lib/ecoportal/api/common/content/class_helpers.rb, line 119 def inheritable_attrs(*attrs) attrs.each do |attr| class_eval %( class << self; attr_accessor :#{attr} end ) end inheritable_class_vars(*attrs) end
Keeps track on class instance variables that should be inherited by child classes. @note
- subclasses will inherit the value as is at that moment - any change afterwards will be only on the specific class (in line with class instance variables) - adapted from https://stackoverflow.com/a/10729812/4352306
TODO: this separates the logic of the method to the instance var. Think if would be possible to join them somehow.
# File lib/ecoportal/api/common/content/class_helpers.rb, line 113 def inheritable_class_vars(*vars) @inheritable_class_vars ||= [:inheritable_class_vars] @inheritable_class_vars += vars end
This callback method is called whenever a subclass of the current class is created. @note
- values of the instance variables are copied as they are (no dups or clones) - the above means: avoid methods that change the state of the mutable object on it - mutating methods would reflect the changes on other classes as well - therefore, `freeze` will be called on the values that are inherited.
# File lib/ecoportal/api/common/content/class_helpers.rb, line 134 def inherited(subclass) inheritable_class_vars.each do |var| instance_var = instance_variable_name(var) value = instance_variable_get(instance_var) subclass.instance_variable_set(instance_var, value.freeze) end end
Helper to create an instance variable `name` @param [String, Symbol] the name of the variable @reutrn [String] the name of the created instance variable
# File lib/ecoportal/api/common/content/class_helpers.rb, line 46 def instance_variable_name(name) str = name.to_s str = "@#{str}" unless str.start_with?("@") str end
If the class for `name` exists, it returns it. Otherwise it generates it. @param name [String, Symbol] the name of the new class @param inherits [Class] the parent class to inherit from @yield [child_class] configure the new class @yieldparam child_class [Class] the new class @return [Class] the new generated class
# File lib/ecoportal/api/common/content/class_helpers.rb, line 58 def new_class(name, inherits:) name = name.to_sym.freeze class_name = to_constant(name) full_class_name = "#{inherits}::#{class_name}" unless target_class = resolve_class(full_class_name, exception: false) target_class = Class.new(inherits) self.const_set class_name, target_class end target_class.tap do |klass| yield(klass) if block_given? end end
Class resolver @note it caches the resolved `klass`es @raise [Exception] when could not resolve if `exception` is `true` @param klass [Class, String, Symbol] the class to resolve @param exception [Boolean] if it should raise exception when could not resolve @return [Class] the `Class` constant
# File lib/ecoportal/api/common/content/class_helpers.rb, line 15 def resolve_class(klass, exception: true) @resolved ||= {} @resolved[klass] ||= case klass when Class klass when String begin Kernel.const_get(klass) rescue NameError => e raise if exception end when Symbol resolve_class(self.send(klass)) else raise "Unknown class: #{klass}" if exception end end
Helper to normalize `key` into a correct `ruby` **constant name** @param key [String, Symbol] to be normalized @return [String] a correct constant name
# File lib/ecoportal/api/common/content/class_helpers.rb, line 37 def to_constant(key) str_name = key.to_s.strip.split(/[\-\_ ]/i).compact.map do |str| str.slice(0).upcase + str.slice(1..-1).downcase end.join("") end
Helper to parse a value into a `Time` object. @raise [Exception] if `exception` is `true` and could not convert @param value [String, Date] the value to convert to `Time` @param exception [Boolean] if should raise `Exception` when could not convert @return
# File lib/ecoportal/api/common/content/class_helpers.rb, line 78 def to_time(value, exception: true) case value when NilClass value when String begin Time.parse(value) rescue ArgumentArgument => e raise if exception nil end when Date Time.parse(value.to_s) when Time value else to_time(value.to_s) if value.respond_to?(:to_s) end end
Helper to determine if a paramter has been used @note to effectivelly use this helper, you should initialize your target
paramters with the constant `NOT_USED`
@param val [] the value of the paramter @return [Boolean] `true` if value other than `NOT_USED`, `false` otherwise
# File lib/ecoportal/api/common/content/class_helpers.rb, line 103 def used_param?(val) val != NOT_USED end