Module: Ecoportal::API::GraphQL::Concerns::SnakeCamelAccess

Included in:
Logic::BaseModel
Defined in:
lib/ecoportal/api/graphql/concerns/snake_camel_access.rb

Overview

Provides snake_case access to camelCase methods on GraphQL model objects. eco-helpers scripts (and v2 models) use snake_case, GraphQL uses camelCase.

Converts any snake_case method call to its camelCase equivalent and delegates. Works for both readers (external_id → externalId) and writers (time_zone= → timeZone=).

Examples: page.external_id → page.externalId page.patch_ver → page.patchVer page.source_template_id → page.sourceTemplateId page.other_tags = ['x'] → page.otherTags = ['x']

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ecoportal/api/graphql/concerns/snake_camel_access.rb', line 17

def method_missing(method_name, *args, &block)
  str    = method_name.to_s
  setter = str.end_with?('=')
  base   = setter ? str[0..-2] : str
  camel  = snake_to_camel(base)

  # Only intercept if conversion actually changed something and target exists
  if camel != base
    target = setter ? :"#{camel}=" : camel.to_sym
    return send(target, *args, &block) if respond_to?(target)
  end

  super
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ecoportal/api/graphql/concerns/snake_camel_access.rb', line 32

def respond_to_missing?(method_name, include_private = false)
  str    = method_name.to_s
  setter = str.end_with?('=')
  base   = setter ? str[0..-2] : str
  camel  = snake_to_camel(base)

  if camel != base
    target = setter ? :"#{camel}=" : camel.to_sym
    return true if respond_to?(target, include_private)
  end

  super
end