Class: Cyby::Kintone::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cyby/kintone/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Relation

Returns a new instance of Relation.



9
10
11
12
13
14
# File 'lib/cyby/kintone/relation.rb', line 9

def initialize(app)
  @app = app
  @condition = Query::Where.new
  @order_by = []
  @fields = nil
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



4
5
6
# File 'lib/cyby/kintone/relation.rb', line 4

def condition
  @condition
end

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/cyby/kintone/relation.rb', line 4

def fields
  @fields
end

#order_byObject (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

#eachObject



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

#inspectObject



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_queryObject



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

#where(cond, *params) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/cyby/kintone/relation.rb', line 26

def where(cond, *params)
  case cond
  when String
    @condition = Query::Where.new(cond, *params)
  else
    @condition = cond.condition
  end
  self
end