Class: Ceres::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/ceres/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, cache: false, &block) ⇒ Reader

Returns a new instance of Reader.



6
7
8
9
10
# File 'lib/ceres/reader.rb', line 6

def initialize(attribute, cache: false, &block)
  @attribute = attribute
  @cache = cache
  @block = block
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/ceres/reader.rb', line 3

def attribute
  @attribute
end

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/ceres/reader.rb', line 4

def block
  @block
end

Instance Method Details

#apply(klass) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ceres/reader.rb', line 32

def apply(klass)
  name = self.name

  if allow_reader_optimisation?
    klass.instance_exec { attr_reader name }
  else
    reader = self.to_proc

    klass.instance_exec { define_method(name, &reader) }
  end
end

#cache?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ceres/reader.rb', line 12

def cache?
  @cache
end

#nameObject



16
17
18
# File 'lib/ceres/reader.rb', line 16

def name
  @attribute.name
end

#targetObject



20
21
22
# File 'lib/ceres/reader.rb', line 20

def target
  @attribute.target
end

#to_procObject



28
29
30
# File 'lib/ceres/reader.rb', line 28

def to_proc
  self.cache? ? to_cached_proc : to_uncached_proc
end

#variableObject



24
25
26
# File 'lib/ceres/reader.rb', line 24

def variable
  @attribute.variable
end