Class: Queryalize::SerializableQuery

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, scope = nil, chain_methods = { }) ⇒ SerializableQuery

Returns a new instance of SerializableQuery.



37
38
39
40
41
42
43
44
45
46
# File 'lib/queryalize/serializable_query.rb', line 37

def initialize(klass, scope = nil, chain_methods = { })
  @klass = klass
  if scope
    @scope = scope
  else
    @scope = klass
  end
  
  @chain_methods  = chain_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/queryalize/serializable_query.rb', line 76

def method_missing(name, *args, &block)
  if query_method?(name)
    chain(name, *args)

  elsif @scope.respond_to?(name)
    @scope.send(name, *args, &block)

  else
    super(name, *args, &block)
  end
end

Instance Attribute Details

#chain_methodsObject (readonly)

Returns the value of attribute chain_methods.



5
6
7
# File 'lib/queryalize/serializable_query.rb', line 5

def chain_methods
  @chain_methods
end

Class Method Details

.deserialize(data, mode = :json) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/queryalize/serializable_query.rb', line 7

def self.deserialize(data, mode = :json)
 
  data          = parse(data, mode)
  klass         = data[:class].constantize
  chain_methods = data[:chain_methods]
  
  scope = klass
  chain_methods.each do |method, args|
    scope = scope.send(method, *args)
  end
  
  new(klass, scope, chain_methods)
end

.from_hash(hash) ⇒ Object



21
22
23
# File 'lib/queryalize/serializable_query.rb', line 21

def self.from_hash(hash)
  deserialize(hash, :hash)
end

.from_json(json) ⇒ Object Also known as: _load



25
26
27
# File 'lib/queryalize/serializable_query.rb', line 25

def self.from_json(json)
  deserialize(json, :json)
end

.from_yaml(yaml) ⇒ Object



29
30
31
# File 'lib/queryalize/serializable_query.rb', line 29

def self.from_yaml(yaml)
  deserialize(yaml, :yaml)
end

Instance Method Details

#_dump(depth) ⇒ Object



64
65
66
# File 'lib/queryalize/serializable_query.rb', line 64

def _dump(depth)
  to_json
end

#chain(name, *args) ⇒ Object



72
73
74
# File 'lib/queryalize/serializable_query.rb', line 72

def chain(name, *args)
  self.class.new(@klass, @scope.send(name, *args), @chain_methods.merge(name => args))
end

#inspectObject



88
89
90
91
92
93
94
# File 'lib/queryalize/serializable_query.rb', line 88

def inspect
  if @chain_methods.empty?
    @klass.name
  else
    @klass.name + "." + @chain_methods.collect { |method, args| "#{method}(#{args.collect(&:inspect).join(", ")})" }.join(".")
  end
end

#query_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/queryalize/serializable_query.rb', line 68

def query_method?(name)
  ActiveRecord::QueryMethods.public_instance_methods.include?(name.to_s)
end

#serialize(mode = :json) ⇒ Object



48
49
50
# File 'lib/queryalize/serializable_query.rb', line 48

def serialize(mode = :json)
  send("to_#{mode}")
end

#to_hashObject



52
53
54
# File 'lib/queryalize/serializable_query.rb', line 52

def to_hash
  { :class => @klass.name, :chain_methods => chain_methods }
end

#to_jsonObject



56
57
58
# File 'lib/queryalize/serializable_query.rb', line 56

def to_json
  to_hash.to_json
end

#to_yaml(opts = { }) ⇒ Object



60
61
62
# File 'lib/queryalize/serializable_query.rb', line 60

def to_yaml(opts = { })
  to_hash.to_yaml(opts)
end