Class: TurbineRb::Client::App::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/turbine_rb/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res, app) ⇒ Resource



60
61
62
63
# File 'lib/turbine_rb/client.rb', line 60

def initialize(res, app)
  @pb_resource = res
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



58
59
60
# File 'lib/turbine_rb/client.rb', line 58

def app
  @app
end

#pb_resourceObject (readonly)

Returns the value of attribute pb_resource.



58
59
60
# File 'lib/turbine_rb/client.rb', line 58

def pb_resource
  @pb_resource
end

Instance Method Details

#records(collection:, configs: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/turbine_rb/client.rb', line 65

def records(collection:, configs: nil)
  req = TurbineCore::ReadCollectionRequest.new(resource: @pb_resource, collection: collection)
  if configs
    pb_configs = configs.keys.map { |key| TurbineCore::Config.new(field: key, value: configs[key]) }
    req.configs = TurbineCore::Configs.new(config: pb_configs)
  end

  app.core_server
     .read_collection(req)
     .wrap(app) # wrap in Collection to enable chaining
end

#write(records:, collection:, configs: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/turbine_rb/client.rb', line 77

def write(records:, collection:, configs: nil)
  new_records = records.dup
  if records.instance_of?(Collection) # it has been processed by a function, so unwrap back to gRPC collection
    new_records = records.unwrap
  end

  req = TurbineCore::WriteCollectionRequest.new(
    resource: @pb_resource,
    sourceCollection: new_records,
    targetCollection: collection
  )

  if configs
    pb_configs = configs.keys.map { |key| TurbineCore::Config.new(field: key, value: configs[key]) }
    req.configs = TurbineCore::Configs.new(config: pb_configs)
  end

  app.core_server.write_collection_to_resource(req)
end