Class: Plucker::Base

Inherits:
Object
  • Object
show all
Includes:
Caching
Defined in:
lib/plucker/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Caching

#cache_key, #cache_version, #fetch, #serializer_class

Constructor Details

#initialize(object, options = {}) ⇒ Base

Returns a new instance of Base.



26
27
28
# File 'lib/plucker/base.rb', line 26

def initialize(object, options = {})
    self.object = object
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



14
15
16
# File 'lib/plucker/base.rb', line 14

def object
  @object
end

Class Method Details

.attribute(attr, options = {}, &block) ⇒ Object



88
89
90
# File 'lib/plucker/base.rb', line 88

def self.attribute(attr, options = {}, &block)
    self._descriptor.add_attribute(options.fetch(:key, attr), Plucker::Attribute.new(attr, options, block))
end

.attributes(*attrs) ⇒ Object



82
83
84
85
86
# File 'lib/plucker/base.rb', line 82

def self.attributes(*attrs)
    attrs.each do |attr|
        attribute(attr)
    end
end

.belongs_to(attr, options = {}, &block) ⇒ Object



92
93
94
# File 'lib/plucker/base.rb', line 92

def self.belongs_to(attr, options = {}, &block)
    self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::BelongsTo.new(attr, options, block))
end

.has_many(attr, options = {}, &block) ⇒ Object



100
101
102
# File 'lib/plucker/base.rb', line 100

def self.has_many(attr, options = {}, &block)
    self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasMany.new(attr, options, block))
end

.has_one(attr, options = {}, &block) ⇒ Object



96
97
98
# File 'lib/plucker/base.rb', line 96

def self.has_one(attr, options = {}, &block)
    self._descriptor.add_relationship(options.fetch(:key, attr), Plucker::HasOne.new(attr, options, block))
end

.inherited(base) ⇒ Object



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

def self.inherited(base)
    super
    base._descriptor = Plucker::Descriptor.new(base)
end

.is_pluckable?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/plucker/base.rb', line 74

def self.is_pluckable?
    self._descriptor.is_pluckable?
end

.model(attr) ⇒ Object



104
105
106
# File 'lib/plucker/base.rb', line 104

def self.model(attr)
    self._descriptor.set_model(attr)
end

.pluckable_columnsObject



78
79
80
# File 'lib/plucker/base.rb', line 78

def self.pluckable_columns
    self._descriptor._pluckable_columns
end

Instance Method Details

#as_json(options = nil) ⇒ Object



42
43
44
# File 'lib/plucker/base.rb', line 42

def as_json(options = nil)
    serializable_hash
end

#associations_hashObject



60
61
62
63
64
65
# File 'lib/plucker/base.rb', line 60

def associations_hash
    self.class._descriptor._relationships.each_with_object({}) do |(key, relationship), hash|
        next if !relationship.should_include?(self)
        hash[key.to_s] = relationship.value(self)
    end
end

#attributes_hashObject



67
68
69
70
71
72
# File 'lib/plucker/base.rb', line 67

def attributes_hash
    self.class._descriptor._attributes.each_with_object({}) do |(key, attr), hash|
        next if !attr.should_include?(self)
        hash[key.to_s] = attr.value(self)
    end
end

#get_hashObject



56
57
58
# File 'lib/plucker/base.rb', line 56

def get_hash
    attributes_hash.merge! associations_hash
end

#serializable_hash(use_cache: true) ⇒ Object Also known as: to_hash, to_h



30
31
32
33
34
35
36
37
38
# File 'lib/plucker/base.rb', line 30

def serializable_hash(use_cache: true)
    if use_cache && self.class.cache_enabled?
        fetch(adapter: :hash) do
            get_hash
        end
    else
        get_hash
    end
end

#to_json(options = {}, use_cache: true) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/plucker/base.rb', line 46

def to_json(options = {}, use_cache: true)
    if use_cache && self.class.cache_enabled?
        fetch(adapter: :json) do
            Oj.dump(get_hash, mode: :rails)
        end
    else
        Oj.dump(get_hash, mode: :rails)
    end
end