Class: FlexiModel::ArQueryable::Criteria

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flexi_model/ar_queryable.rb

Constant Summary collapse

DEFAULT_SORTABLE_FIELDS =
[:created_at, :updated_at]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, target_model, namespace) ⇒ Criteria

Returns a new instance of Criteria.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flexi_model/ar_queryable.rb', line 78

def initialize(host, target_model, namespace)
  @conditions = {namespace: namespace}
  @joins = []
  @orders = {}
  @groups = []
  @target_model = target_model
  @select_object = nil

  # By default Max 100 items will be retrieved
  @limit = 100
  @offset = 0
  @host = host
  @members = nil
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



72
73
74
# File 'lib/flexi_model/ar_queryable.rb', line 72

def conditions
  @conditions
end

#hostObject

Returns the value of attribute host.



72
73
74
# File 'lib/flexi_model/ar_queryable.rb', line 72

def host
  @host
end

#limit(num) ⇒ Object

Returns the value of attribute limit.



72
73
74
# File 'lib/flexi_model/ar_queryable.rb', line 72

def limit
  @limit
end

#offset(num) ⇒ Object

Returns the value of attribute offset.



72
73
74
# File 'lib/flexi_model/ar_queryable.rb', line 72

def offset
  @offset
end

#select_field(field) ⇒ Object

Returns the value of attribute select_field.



72
73
74
# File 'lib/flexi_model/ar_queryable.rb', line 72

def select_field
  @select_field
end

#target_modelObject (readonly)

Returns the value of attribute target_model.



74
75
76
# File 'lib/flexi_model/ar_queryable.rb', line 74

def target_model
  @target_model
end

Instance Method Details

#destroy_allObject



121
122
123
# File 'lib/flexi_model/ar_queryable.rb', line 121

def destroy_all
  _perform_query.destroy_all
end

#each(&block) ⇒ Object



148
149
150
# File 'lib/flexi_model/ar_queryable.rb', line 148

def each(&block)
  _members.each {|m| block.call _convert_to_model(m) }
end

#get_limitObject



112
113
114
# File 'lib/flexi_model/ar_queryable.rb', line 112

def get_limit
  @limit
end

#get_offsetObject



103
104
105
# File 'lib/flexi_model/ar_queryable.rb', line 103

def get_offset
  @offset
end

#lastObject



152
153
154
# File 'lib/flexi_model/ar_queryable.rb', line 152

def last
  @target_model.initialize_with_record(_members.last)
end

#order(key, order_type) ⇒ Object

Order query by the given field and order type

key - field name in symbol or string
order_type - order type :asc or :desc
  :asc - stands for ascending order
  :desc - stands for descending order

Returns self instance



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/flexi_model/ar_queryable.rb', line 134

def order(key, order_type)
  if DEFAULT_SORTABLE_FIELDS.include?(key.to_sym)
    self.orders[key.to_sym] = order_type
  else
    field = _find_field(key)
    _join_table :values
    column = "`#{FlexiModel::ArModels::Value.table_name}`.`#{field.value_column}`"
    self.orders[column] = order_type
    self.groups << "`#{FlexiModel::ArModels::Record.table_name}`.`id`"
  end

  self
end

#to_sqlObject



156
157
158
# File 'lib/flexi_model/ar_queryable.rb', line 156

def to_sql
  self._perform_query.to_sql
end

#where(hash) ⇒ Object



93
94
95
96
# File 'lib/flexi_model/ar_queryable.rb', line 93

def where(hash)
  self._convert_query(hash)
  self
end