Class: Cyby::Kintone::Relation
- Inherits:
-
Object
- Object
- Cyby::Kintone::Relation
- Includes:
- Enumerable
- Defined in:
- lib/cyby/kintone/relation.rb
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#order_by ⇒ Object
readonly
Returns the value of attribute order_by.
Instance Method Summary collapse
- #and(cond, *params) ⇒ Object
- #asc(field) ⇒ Object
- #desc(field) ⇒ Object
- #each ⇒ Object
-
#initialize(app) ⇒ Relation
constructor
A new instance of Relation.
- #inspect ⇒ Object
- #or(cond, *params) ⇒ Object
- #select(*fields) ⇒ Object
- #to_query ⇒ Object
- #where(cond, *params) ⇒ Object
Constructor Details
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
4 5 6 |
# File 'lib/cyby/kintone/relation.rb', line 4 def condition @condition end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
4 5 6 |
# File 'lib/cyby/kintone/relation.rb', line 4 def fields @fields end |
#order_by ⇒ Object (readonly)
Returns the value of attribute order_by.
4 5 6 |
# File 'lib/cyby/kintone/relation.rb', line 4 def order_by @order_by end |
Instance Method Details
#and(cond, *params) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/cyby/kintone/relation.rb', line 36 def and(cond, *params) case cond when String @condition = Query::WhereAnd.new(@condition, Query::Where.new(cond, *params)) else @condition = Query::WhereAnd.new(@condition, cond.condition) end self end |
#asc(field) ⇒ Object
56 57 58 59 |
# File 'lib/cyby/kintone/relation.rb', line 56 def asc(field) @order_by << "#{field} asc" self end |
#desc(field) ⇒ Object
61 62 63 64 |
# File 'lib/cyby/kintone/relation.rb', line 61 def desc(field) @order_by << "#{field} desc" self end |
#each ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/cyby/kintone/relation.rb', line 16 def each args = {} args[:query] = to_query args[:fields] = @fields if @fields records = @app.find(args) records.map do |record| yield record end end |
#inspect ⇒ Object
79 80 81 |
# File 'lib/cyby/kintone/relation.rb', line 79 def inspect { app: @app.id, query: to_query, select: @fields }.inspect end |
#or(cond, *params) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/cyby/kintone/relation.rb', line 46 def or(cond, *params) case cond when String @condition = Query::WhereOr.new(@condition, Query::Where.new(cond, *params)) else @condition = Query::WhereOr.new(@condition, cond.condition) end self end |
#select(*fields) ⇒ Object
66 67 68 69 |
# File 'lib/cyby/kintone/relation.rb', line 66 def select(*fields) @fields = fields self end |
#to_query ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/cyby/kintone/relation.rb', line 71 def to_query query = @condition.to_query if @order_by.any? query += " order by #{@order_by.join(',')}" end query end |