Class: Syphon::Schema

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

Defined Under Namespace

Classes: DSL, Field, NestedField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Schema

Returns a new instance of Schema.



3
4
5
6
7
8
9
10
11
# File 'lib/syphon/schema.rb', line 3

def initialize(&block)
  @fields = {}
  @relation = nil
  @joins = []
  @conditions = nil
  @group_clause = nil
  @having_clause = nil
  configure(&block) if block
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



14
15
16
# File 'lib/syphon/schema.rb', line 14

def conditions
  @conditions
end

#fieldsObject (readonly)

Returns the value of attribute fields.



13
14
15
# File 'lib/syphon/schema.rb', line 13

def fields
  @fields
end

#group_clauseObject

Returns the value of attribute group_clause.



14
15
16
# File 'lib/syphon/schema.rb', line 14

def group_clause
  @group_clause
end

#having_clauseObject

Returns the value of attribute having_clause.



14
15
16
# File 'lib/syphon/schema.rb', line 14

def having_clause
  @having_clause
end

#joinsObject (readonly)

Returns the value of attribute joins.



13
14
15
# File 'lib/syphon/schema.rb', line 13

def joins
  @joins
end

#relationObject

Returns the value of attribute relation.



14
15
16
# File 'lib/syphon/schema.rb', line 14

def relation
  @relation
end

Instance Method Details

#configure(&block) ⇒ Object



16
17
18
# File 'lib/syphon/schema.rb', line 16

def configure(&block)
  DSL.new(self)._eval(&block)
end

#propertiesObject



41
42
43
44
45
46
47
# File 'lib/syphon/schema.rb', line 41

def properties
  mapping = {}
  fields.each do |name, field|
    mapping[name] = field.properties
  end
  mapping
end

#query(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/syphon/schema.rb', line 20

def query(options = {})
  order = options[:order] and
    order_by_fragment = "ORDER BY #{query_fragment(order)}"
  limit = options[:limit] and
    limit_fragment = "LIMIT #{query_fragment(limit)}"

  select_fragment = options[:select] || select_fragments
  where_fragment = where_fragment(options.slice(:scope, :invert))

  <<-EOS.strip.gsub(/\s+/, ' ')
    SELECT #{select_fragment}
    FROM #{query_fragment(relation)}
    #{joins_fragment}
    #{where_fragment}
    #{group_by_fragment}
    #{having_fragment}
    #{order_by_fragment}
    #{limit_fragment}
  EOS
end