Class: ZohoHub::ModuleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/zoho_hub/reflection/module_builder.rb

Class Method Summary collapse

Class Method Details

.build_from_cacheObject



8
9
10
11
12
13
14
# File 'lib/zoho_hub/reflection/module_builder.rb', line 8

def build_from_cache
  cached_module_definitions.map do |file|
    json = MultiJson.load(File.read(file), symbolize_keys: true)

    eval_module_class(json)
  end
end

.cached_module_definitionsObject



16
17
18
# File 'lib/zoho_hub/reflection/module_builder.rb', line 16

def cached_module_definitions
  Dir[File.join(ZohoHub.root, 'cache', 'modules', '**')]
end

.cached_module_fields(module_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/zoho_hub/reflection/module_builder.rb', line 46

def cached_module_fields(module_name)
  file = File.join(ZohoHub.root, 'cache', 'fields', "#{module_name}.json")

  return [] unless File.exist?(file)

  json_content = File.read(file)

  MultiJson.load(json_content, symbolize_keys: true)
end

.eval_module_class(json) ⇒ Object



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
# File 'lib/zoho_hub/reflection/module_builder.rb', line 20

def eval_module_class(json)
  fields = cached_module_fields(json[:api_name])

  klass = Class.new(ZohoHub::BaseRecord) do
    request_path json[:api_name]

    translations = { id: :id }
    fields.each do |field|
      key = StringUtils.underscore(field[:api_name]).to_sym

      translations[key] = field[:api_name].to_sym

      add_validation(key, validate: :length, length: field[:length]) if field[:length]

      if field[:data_type] == 'picklist'
        add_validation(key, validate: :picklist, list: field[:pick_list_values])
      end
    end

    attributes(*translations.keys)
    attribute_translation(translations)
  end

  ZohoHub.const_set(StringUtils.camelize(json[:singular_label]), klass)
end