Class: Dataclips::Clip
- Inherits:
-
Object
- Object
- Dataclips::Clip
- Includes:
- ActiveModel::Model
- Defined in:
- app/models/dataclips/clip.rb
Class Attribute Summary collapse
-
.query ⇒ Object
Returns the value of attribute query.
-
.template ⇒ Object
Returns the value of attribute template.
Class Method Summary collapse
Instance Method Summary collapse
Class Attribute Details
.query ⇒ Object
Returns the value of attribute query.
8 9 10 |
# File 'app/models/dataclips/clip.rb', line 8 def query @query end |
.template ⇒ Object
Returns the value of attribute template.
8 9 10 |
# File 'app/models/dataclips/clip.rb', line 8 def template @template end |
Class Method Details
.per_page ⇒ Object
18 19 20 |
# File 'app/models/dataclips/clip.rb', line 18 def per_page @per_page || 1000 end |
.schema ⇒ Object
14 15 16 |
# File 'app/models/dataclips/clip.rb', line 14 def schema (@schema || {}).deep_symbolize_keys end |
.variables ⇒ Object
10 11 12 |
# File 'app/models/dataclips/clip.rb', line 10 def variables (@variables || {}).deep_symbolize_keys end |
Instance Method Details
#context ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/dataclips/clip.rb', line 42 def context return {} if invalid? self.class.variables.reduce({}) do |memo, (attr, )| value = send(attr) memo[attr] = case [:type] when "date" Date.parse(value).to_s else value.to_s end memo end.symbolize_keys end |
#paginate(page = 1) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/dataclips/clip.rb', line 61 def paginate(page = 1) WillPaginate::Collection.create(page, self.class.per_page) do |pager| sql_with_total_entries = %{WITH _q AS (#{query}) SELECT COUNT(*) OVER () AS _total_entries, * FROM _q LIMIT #{pager.per_page} OFFSET #{pager.offset};} results = ActiveRecord::Base.connection.execute(sql_with_total_entries) if pager.total_entries.nil? pager.total_entries = results.none? ? 0 : results.first["_total_entries"].to_i end records = results.map do |record| type_cast record.except("_total_entries").symbolize_keys end pager.replace records end end |
#query ⇒ Object
57 58 59 |
# File 'app/models/dataclips/clip.rb', line 57 def query self.class.template % context end |
#type_cast(attributes) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/dataclips/clip.rb', line 23 def type_cast(attributes) attributes.reduce({}) do |memo, (key, value)| if schema_key = self.class.schema[key] memo[key] = case schema_key[:type].to_sym when :text then ActiveRecord::Type::String.new.type_cast_from_database(value) when :integer then ActiveRecord::Type::Integer.new.type_cast_from_database(value) when :float then ActiveRecord::Type::Float.new.type_cast_from_database(value) when :datetime then ActiveRecord::Type::DateTime.new.type_cast_from_database(value) when :time then ActiveRecord::Type::Time.new.type_cast_from_database(value) when :date then ActiveRecord::Type::Date.new.type_cast_from_database(value) when :boolean then ActiveRecord::Type::Boolean.new.type_cast_from_database(value) else value end end memo end end |