Class: Botz::Binder
- Inherits:
-
Object
- Object
- Botz::Binder
- Defined in:
- lib/botz/binder.rb
Overview
Bind resource received from the connection to the result object
Defined Under Namespace
Classes: Multiple
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
- .field(name, query = nil, &block) ⇒ Object
- .fields(query, &block) ⇒ Object
- .fields_call(binder) ⇒ Object
- .query(name, query = nil, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(resource) ⇒ Binder
constructor
A new instance of Binder.
- #new_result(values) ⇒ Object
- #result ⇒ Object
Constructor Details
#initialize(resource) ⇒ Binder
Returns a new instance of Binder.
34 35 36 37 |
# File 'lib/botz/binder.rb', line 34 def initialize(resource) @resource = resource self.class.fields_call(self) end |
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
8 9 10 |
# File 'lib/botz/binder.rb', line 8 def resource @resource end |
Class Method Details
.field(name, query = nil, &block) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/botz/binder.rb', line 54 def self.field(name, query = nil, &block) field_names << name field_names.uniq! result_class.define(name) define_method(name) do connector.field(self, query, &block) end end |
.fields(query, &block) ⇒ Object
63 64 65 |
# File 'lib/botz/binder.rb', line 63 def self.fields(query, &block) @fields = { query: query, block: block } end |
.fields_call(binder) ⇒ Object
67 68 69 |
# File 'lib/botz/binder.rb', line 67 def self.fields_call(binder) Multiple.bind(connector: connector, binder: binder, query: @fields[:query], block: @fields[:block]) if @fields end |
.query(name, query = nil, &block) ⇒ Object
48 49 50 51 52 |
# File 'lib/botz/binder.rb', line 48 def self.query(name, query = nil, &block) define_method(name) do connector.field(self, query, &block) end end |
Instance Method Details
#new_result(values) ⇒ Object
43 44 45 46 |
# File 'lib/botz/binder.rb', line 43 def new_result(values) fetched_at = Time.current self.class.result_class.new(fetched_at: fetched_at, fetched_on: fetched_at.beginning_of_day, **values) end |
#result ⇒ Object
39 40 41 |
# File 'lib/botz/binder.rb', line 39 def result new_result(field_names.map { |field_name| [field_name, send(field_name)] }.to_h) end |