Class: Piggyback::Attribute

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reflection, name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



140
141
142
143
144
145
146
147
148
149
# File 'lib/piggyback.rb', line 140

def initialize(reflection, name, options = {})
  @reflection  = reflection
  @name   = name.to_s
  if options[:sql]
    @sql = Arel.sql(options[:sql])
    @source = options[:source].to_s if options[:source]
  else
    @source = (options[:source] || name).to_s
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



138
139
140
# File 'lib/piggyback.rb', line 138

def name
  @name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



138
139
140
# File 'lib/piggyback.rb', line 138

def reflection
  @reflection
end

#sourceObject (readonly)

Returns the value of attribute source.



138
139
140
# File 'lib/piggyback.rb', line 138

def source
  @source
end

#sqlObject (readonly)

Returns the value of attribute sql.



138
139
140
# File 'lib/piggyback.rb', line 138

def sql
  @sql
end

Instance Method Details

#columnObject



164
165
166
# File 'lib/piggyback.rb', line 164

def column
  reflection.klass.columns_hash[source]
end

#selectObject



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/piggyback.rb', line 151

def select
  unless defined? @select 
    @select = if sql
      sql.as(Arel.sql(name))
    elsif source == name
      reflection.klass.arel_table[name]
    else
      reflection.klass.arel_table[source].as(Arel.sql(name))
    end
  end
  @select
end

#serialized_classObject



168
169
170
# File 'lib/piggyback.rb', line 168

def serialized_class
  reflection.klass.serialized_attributes[source]
end