class ZohoHub::ModuleBuilder

Public Class Methods

build_from_cache() click to toggle source
# 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_definitions() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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