Class: Barrister::Rails::Client::InterfaceProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/barrister-rails.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client, fx_metadata, transmute_to_model) ⇒ InterfaceProxy

Returns a new instance of InterfaceProxy.



33
34
35
36
37
38
# File 'lib/barrister-rails.rb', line 33

def initialize(name, client, , transmute_to_model)
  @name               = name
  @client             = client
          = 
  @transmute_to_model = transmute_to_model
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/barrister-rails.rb', line 40

def method_missing(name, *args)
  result = @client.send(@name).send(name, *args)

  if @transmute_to_model == true and DEFAULT_BARRISTER_TYPES.include?([name][:type]) == false
    cast result, [name][:type], [name][:is_array]
  else
    result
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/barrister-rails.rb', line 31

def name
  @name
end

Instance Method Details

#all_struct_fields(arr, struct, structs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/barrister-rails.rb', line 82

def all_struct_fields(arr, struct, structs)
  struct["fields"].each do |f|
    arr << f
  end

  if struct["extends"]
    parent = structs[struct["extends"]]
    if parent
      return all_struct_fields(arr, parent, structs)
    end
  end

  return arr
end

#attributes_for_type(type) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/barrister-rails.rb', line 74

def attributes_for_type(type)
  structs = @client
    .instance_variable_get('@contract')
    .instance_variable_get('@structs')

  all_struct_fields([], structs[type], structs).map { |f| f['name'] }
end

#cast(result, type, is_array) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/barrister-rails.rb', line 64

def cast(result, type, is_array)
  klass = ensure_const(type)

  if is_array
    result.map { |result| klass.new result }
  else
    klass.new result
  end
end

#ensure_const(type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/barrister-rails.rb', line 50

def ensure_const(type)
  return Object.const_get(type) if Object.const_defined?(type)

  klass = Class.new(BaseEtherealModel)

  a = attributes_for_type(type).map { |name| "attribute :#{name};" }

  klass.class_eval a.join('')

  Object.send(:const_set, type, klass)

  klass
end