Module: Ree::CLI::Indexing::ClassMethods

Defined in:
lib/ree/cli/indexing.rb

Instance Method Summary collapse

Instance Method Details

#index_package_entry(package) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ree/cli/indexing.rb', line 12

def index_package_entry(package)
  package_hsh = {}
  package_hsh[:name] = package.name
  package_hsh[:schema_rpath] = package.schema_rpath
  package_hsh[:entry_rpath] = package.entry_rpath
  package_hsh[:tags] = package.tags
  package_hsh[:objects] = package.objects.map {
    {
      name: _1.name,
      schema_rpath: _1.schema_rpath,
      file_rpath: _1.rpath,
      mount_as: _1.mount_as,
      methods: map_fn_methods(_1),
      links: _1.links.sort_by(&:object_name).map { |link|
        {
          Ree::ObjectSchema::Links::TARGET => link.object_name,
          Ree::ObjectSchema::Links::PACKAGE_NAME => link.package_name,
          Ree::ObjectSchema::Links::AS => link.as,
          Ree::ObjectSchema::Links::IMPORTS => link.constants
        }
      }
    }
  }

  package_hsh
end

#index_public_methods_for_package_classes(package, index_hash) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ree/cli/indexing.rb', line 106

def index_public_methods_for_package_classes(package, index_hash)
  package.objects.each do |obj|
    klass = obj.klass
    next if klass.nil?

    klass_name = demodulize(klass.to_s)
    obj_name = obj.name.to_s
    rpath = obj.rpath

    if obj.tags.include?("enum")
      hsh = index_class(klass, rpath, package.name)
      index_hash[:classes][klass_name] ||= []
      index_hash[:classes][klass_name] << hsh
      index_hash[:objects][obj_name] ||= []
      index_hash[:objects][obj_name] << hsh
    elsif obj.tags.include?("dao")
      hsh = index_dao(klass, rpath, package.name)
      index_hash[:objects][obj_name] ||= []
      index_hash[:objects][obj_name] << hsh
    elsif obj.tags.include?("object")
      hsh = index_class(klass, rpath, package.name)
      index_hash[:objects][obj_name] ||= []
      index_hash[:objects][obj_name] << hsh
    end
  end

  recursively_index_module(package.module, index_hash, package, {})

  index_hash
end

#map_fn_methods(object) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ree/cli/indexing.rb', line 39

def map_fn_methods(object)
  if !object.fn?
    return []
  end

  klass = object.klass

  object_is_action = object.tags.include?("action")
  action_caster = object.klass.const_get(:ActionCaster) if object.klass.const_defined?(:ActionCaster)

  method_name = object_is_action ? :__original_call : :call
  method_decorator = Ree::Contracts.get_method_decorator(
    klass, method_name, scope: :instance
  )

  begin
    if method_decorator.nil?
      parameters = klass.instance_method(:call).parameters

      args = parameters.inject({}) do |res, param|
        res[param.last] = Ree::Contracts::CalledArgsValidator::Arg.new(
          param.last, param.first, nil, nil
        )

        res
      end
    else
      parameters = method_decorator.args.params
      args = method_decorator.args.get_args
    end
  rescue NameError
    raise Ree::Error.new("method call is not defined for #{klass}")
  end


  arg_list = parameters.map do |param|
    arg = args[param.last]
    validator = arg.validator
    arg_type = arg.type

    type = if object_is_action && action_caster && arg.name == :attrs
      map_mapper_fields(action_caster.fields).to_s.gsub(/\\*\"/, "").gsub(/\=\>/, ' => ')
    else
      if validator
        validator.to_s
      else
        arg_type == :block ? "Block" : "Any"
      end
    end

    {
      Ree::ObjectSchema::Methods::Args::ARG => arg.name,
      Ree::ObjectSchema::Methods::Args::ARG_TYPE => arg.type,
      Ree::ObjectSchema::Methods::Args::TYPE => type
    }
  end

  [
    {
      Ree::ObjectSchema::Methods::DOC => method_decorator&.doc || "",
      Ree::ObjectSchema::Methods::THROWS => method_decorator&.errors&.map { _1.name } || [],
      Ree::ObjectSchema::Methods::RETURN => method_decorator&.contract_definition&.return_contract || "Any",
      Ree::ObjectSchema::Methods::ARGS => arg_list
    }
  ]
end