Class: Brentano::Presenter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, options = {}) {|_self| ... } ⇒ Presenter

Returns a new instance of Presenter.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
# File 'lib/brentano/presenter.rb', line 12

def initialize(subject, options = {}, &blk)
  @subject = subject
  @options = Hashie::Mash.new(options)
  yield self if block_given?
  finish
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brentano/presenter.rb', line 36

def method_missing(name, *args, &blk)
  if @collection.respond_to? name
    result = @collection.send(name, *args, &blk)
    if result.is_a? ActiveRecord::Relation
      self.dup.tap do |copy|
        copy.instance_eval { @collection = result }
      end
    else
      result
    end
  else
    super
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'lib/brentano/presenter.rb', line 8

def collection
  @collection
end

#maximum_sizeObject

Returns the value of attribute maximum_size.



8
9
10
# File 'lib/brentano/presenter.rb', line 8

def maximum_size
  @maximum_size
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/brentano/presenter.rb', line 8

def options
  @options
end

#subjectObject (readonly)

Returns the value of attribute subject.



8
9
10
# File 'lib/brentano/presenter.rb', line 8

def subject
  @subject
end

Instance Method Details

#as_json(*args) ⇒ Object



51
52
53
# File 'lib/brentano/presenter.rb', line 51

def as_json(*args)
  @collection.as_json(*args)
end

#finish(&blk) ⇒ Object



19
20
21
# File 'lib/brentano/presenter.rb', line 19

def finish(&blk)
  @collection = Brentano::Query.finish relation, options.dup, &blk
end

#proxy_respond_to?Object



10
# File 'lib/brentano/presenter.rb', line 10

alias_method :proxy_respond_to?, :respond_to?

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/brentano/presenter.rb', line 32

def respond_to?(*args)
  proxy_respond_to?(args[0]) || @collection.respond_to?(args[0])
end

#to_sqlObject



55
56
57
# File 'lib/brentano/presenter.rb', line 55

def to_sql
  @collection.to_sql
end