Class: Datasource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/datasource/base.rb,
lib/datasource/attributes/query_attribute.rb,
lib/datasource/attributes/computed_attribute.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Base

Returns a new instance of Base.



26
27
28
29
30
# File 'lib/datasource/base.rb', line 26

def initialize(scope)
  @scope = scope
  @expose_attributes = []
  @datasource_data = {}
end

Class Attribute Details

._associationsObject

Returns the value of attribute _associations.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _associations
  @_associations
end

._attributesObject

Returns the value of attribute _attributes.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _attributes
  @_attributes
end

._virtual_attributesObject

Returns the value of attribute _virtual_attributes.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _virtual_attributes
  @_virtual_attributes
end

.adapterObject

Returns the value of attribute adapter.



5
6
7
# File 'lib/datasource/base.rb', line 5

def adapter
  @adapter
end

Class Method Details

.attribute(name, klass = nil) ⇒ Object



17
18
19
# File 'lib/datasource/base.rb', line 17

def attribute(name, klass = nil)
  @_attributes.push name: name.to_s, klass: klass
end

.attributes(*attrs) ⇒ Object



13
14
15
# File 'lib/datasource/base.rb', line 13

def attributes(*attrs)
  attrs.each { |name| attribute(name) }
end

.computed_attribute(name, deps, &block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/datasource/attributes/computed_attribute.rb', line 32

def self.computed_attribute(name, deps, &block)
  klass = Class.new(Attributes::ComputedAttribute) do
    depends deps

    define_method(:value, &block)
  end
  attribute name, klass
end

.includes_many(name, klass, foreign_key) ⇒ Object



21
22
23
# File 'lib/datasource/base.rb', line 21

def includes_many(name, klass, foreign_key)
  @_attributes.push name: name.to_s, klass: klass, foreign_key: foreign_key.to_s, id_key: self::ID_KEY
end

.inherited(base) ⇒ Object



7
8
9
10
11
# File 'lib/datasource/base.rb', line 7

def inherited(base)
  base._attributes = (_attributes || []).dup
  @adapter ||= Datasource::Adapters::ActiveRecord
  self.send :include, @adapter
end

.query_attribute(name, deps, &block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/datasource/attributes/query_attribute.rb', line 19

def self.query_attribute(name, deps, &block)
  klass = Class.new(Attributes::QueryAttribute) do
    depends deps

    define_method(:select_value, &block)
  end
  attribute name, klass
end

Instance Method Details

#attribute_exposed?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/datasource/base.rb', line 48

def attribute_exposed?(name)
  @expose_attributes.include?(name)
end

#resultsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/datasource/base.rb', line 56

def results
  rows = get_rows(@scope)

  attribute_map = self.class._attributes.inject({}) do |hash, att|
    hash[att[:name]] = att
    hash
  end

  computed_expose_attributes = []
  datasources = {}

  @expose_attributes.each do |name|
    att = attribute_map[name]
    klass = att[:klass]
    next unless klass

    if klass.ancestors.include?(Attributes::ComputedAttribute)
      computed_expose_attributes.push(att)
    elsif klass.ancestors.include?(Base)
      datasources[att] =
        included_datasource_rows(att, @datasource_data[att[:name]], rows)
    end
  end

  # TODO: field names...
  rows.each do |row|
    computed_expose_attributes.each do |att|
      klass = att[:klass]
      if klass
        row[att[:name]] = klass.new(row).value
      end
    end
    datasources.each_pair do |att, rows|
      row[att[:name]] = Array(rows[row[att[:id_key]]])
    end
    row.delete_if do |key, value|
      !attribute_exposed?(key)
    end
  end

  rows
end

#select(*names) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datasource/base.rb', line 32

def select(*names)
  names = names.flat_map do |name|
    if name.kind_of?(Hash)
      # datasource data
      name.each_pair do |k, v|
        @datasource_data[k.to_s] = v
      end
      name.keys
    else
      name
    end
  end
  @expose_attributes = (@expose_attributes + names.map(&:to_s)).uniq
  self
end

#to_queryObject



52
53
54
# File 'lib/datasource/base.rb', line 52

def to_query
  to_query(@scope)
end