Module: Ecoportal::API::Common::Content::ClassHelpers

Includes:
Common::BaseClass
Included in:
DoubleModel
Defined in:
lib/ecoportal/api/common/content/class_helpers.rb

Constant Summary collapse

NOT_USED =
"no_used!"

Instance Method Summary collapse

Instance Method Details

#instance_variable_name(name) ⇒ Object

Helper to create an instance variable name

Parameters:

  • the (String, Symbol)

    name of the variable



46
47
48
49
50
# 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

#new_class(name, inherits:) {|child_class| ... } ⇒ Class

If the class for name exists, it returns it. Otherwise it generates it.

Parameters:

  • name (String, Symbol)

    the name of the new class

  • inherits (Class)

    the parent class to inherit from

Yields:

  • (child_class)

    configure the new class

Yield Parameters:

  • child_class (Class)

    the new class

Returns:

  • (Class)

    the new generated class



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

#resolve_class(klass, exception: true) ⇒ Class

Note:

it caches the resolved klasses

Class resolver

Parameters:

  • klass (Class, String, Symbol)

    the class to resolve

  • exception (Boolean) (defaults to: true)

    if it should raise exception when could not resolve

Returns:

  • (Class)

    the Class constant

Raises:

  • (Exception)

    when could not resolve if exception is true



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

#to_constant(key) ⇒ String

Helper to normalize key into a correct ruby constant name

Parameters:

  • key (String, Symbol)

    to be normalized

Returns:

  • (String)

    a correct constant name



37
38
39
40
41
# 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

#to_time(value, exception: true) ⇒ Object

Helper to parse a value into a Time object.

Parameters:

  • value (String, Date)

    the value to convert to Time

  • exception (Boolean) (defaults to: true)

    if should raise Exception when could not convert

Returns:

Raises:

  • (Exception)

    if exception is true and could not convert



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#used_param?(val) ⇒ Boolean

Note:

to effectivelly use this helper, you should initialize your target paramters with the constant NOT_USED

Helper to determine if a paramter has been used

Parameters:

  • val

    [] the value of the paramter

Returns:

  • (Boolean)

    true if value other than NOT_USED, false otherwise



103
104
105
# File 'lib/ecoportal/api/common/content/class_helpers.rb', line 103

def used_param?(val)
  val != NOT_USED
end