Class: AzaharaSchema::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/azahara_schema/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, type) ⇒ Attribute



7
8
9
10
# File 'lib/azahara_schema/attribute.rb', line 7

def initialize(model, name, type)
  @name, @model = name, model
  @format = AzaharaSchema::FieldFormat.find(type)
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/azahara_schema/attribute.rb', line 5

def format
  @format
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/azahara_schema/attribute.rb', line 5

def model
  @model
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/azahara_schema/attribute.rb', line 5

def name
  @name
end

#table_aliasObject

Returns the value of attribute table_alias.



5
6
7
# File 'lib/azahara_schema/attribute.rb', line 5

def table_alias
  @table_alias
end

Instance Method Details

#add_join(scope) ⇒ Object



103
104
105
# File 'lib/azahara_schema/attribute.rb', line 103

def add_join(scope)
  scope
end

#add_preload(scope) ⇒ Object



107
108
109
# File 'lib/azahara_schema/attribute.rb', line 107

def add_preload(scope)
  scope
end

#add_sort(scope, order) ⇒ Object



116
117
118
# File 'lib/azahara_schema/attribute.rb', line 116

def add_sort(scope, order)
  scope.order( arel_sort_field.public_send(order) )
end

#add_statement(scope, operator, values) ⇒ Object



111
112
113
114
# File 'lib/azahara_schema/attribute.rb', line 111

def add_statement(scope, operator, values)
  values = [values] unless values.is_a?(Array)
  scope.where(arel_statement(operator, values))
end

#aggregable?Boolean



63
64
65
# File 'lib/azahara_schema/attribute.rb', line 63

def aggregable?
  format.aggregable?
end

#arel_field(t_alias = self.table_alias) ⇒ Object



33
34
35
# File 'lib/azahara_schema/attribute.rb', line 33

def arel_field(t_alias=self.table_alias)
  arel_table(t_alias)[filter_name]
end

#arel_join(parent = nil, join_type = ::Arel::Nodes::OuterJoin, a_tbl = self.arel_table(self.table_alias)) ⇒ Object



132
133
134
# File 'lib/azahara_schema/attribute.rb', line 132

def arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias))
  parent
end

#arel_sort_fieldObject



37
38
39
# File 'lib/azahara_schema/attribute.rb', line 37

def arel_sort_field
  arel_field
end

#arel_statement(operator, values) ⇒ Object

values has to be array!



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/azahara_schema/attribute.rb', line 76

def arel_statement(operator, values)
  values = values.collect{|v| format.sanitize_value(v) }
  case operator
  when '='
    condition = arel_field.in(values.compact) unless values.compact.empty?
    if values.include?(nil)
      c_nil = arel_field.eq(nil)
      condition = condition ? condition.or(c_nil) : c_nil
    end
    condition
  when '~'
    vals = values.collect{|v| v.split }.flatten
    arl = arel_field.matches("%#{vals[0]}%")
    vals[1..-1].each{|v| arl = arl.or( arel_field.matches("%#{v}%") ) }
    arl
  when '>='
    arel_field.gteq(values.map(&:to_f).min)
  when '<='
    arel_field.lteq(values.map(&:to_f).max)
  when '@>'
    require 'arel/azahara_postgres_exts'
    arel_field.contains(values)
  else
    throw 'Unknown operator ' + operator.to_s
  end
end

#arel_table(t_alias = self.table_alias) ⇒ Object



29
30
31
# File 'lib/azahara_schema/attribute.rb', line 29

def arel_table(t_alias=self.table_alias)
  t_alias ? model.arel_table.alias(t_alias) : model.arel_table
end

#association_hashObject



128
129
130
# File 'lib/azahara_schema/attribute.rb', line 128

def association_hash
  {}
end

#attribute_nameObject



124
125
126
# File 'lib/azahara_schema/attribute.rb', line 124

def attribute_name
  AzaharaSchema::AttributeName.new(self)
end

#available_operatorsObject



12
13
14
# File 'lib/azahara_schema/attribute.rb', line 12

def available_operators
  format.available_operators
end

#available_valuesObject



16
17
18
19
20
21
22
23
# File 'lib/azahara_schema/attribute.rb', line 16

def available_values
  case type
  when 'list'
    @model.try(name.to_s.pluralize)
  else
    nil
  end
end

#build_json_options!(options) ⇒ Object



120
121
122
# File 'lib/azahara_schema/attribute.rb', line 120

def build_json_options!(options)
  options
end

#column?Boolean



55
56
57
# File 'lib/azahara_schema/attribute.rb', line 55

def column?
  true
end

#filter?Boolean



59
60
61
# File 'lib/azahara_schema/attribute.rb', line 59

def filter?
  true
end

#filter_nameObject



41
42
43
# File 'lib/azahara_schema/attribute.rb', line 41

def filter_name
  name
end

#pathObject

Path in json structure



46
47
48
# File 'lib/azahara_schema/attribute.rb', line 46

def path
  name
end

#primary_key_nameObject

Name of the primary key attribute (same as column obviously)



51
52
53
# File 'lib/azahara_schema/attribute.rb', line 51

def primary_key_name
  model.primary_key
end

#searchable?Boolean



67
68
69
# File 'lib/azahara_schema/attribute.rb', line 67

def searchable?
  format.searchable?
end

#typeObject



25
26
27
# File 'lib/azahara_schema/attribute.rb', line 25

def type
  format.format_name
end

#value(record) ⇒ Object



71
72
73
# File 'lib/azahara_schema/attribute.rb', line 71

def value(record)
  record.public_send(name)
end