Class: Pears::Subject

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Testing::Subject
Defined in:
lib/pears/subject.rb,
lib/pears/testing.rb

Overview

A subject is a collection of settings. You could different Pears::Subjects in your application for different parts of its domain. for instance: application_settings, theming or translaions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Testing::Subject

#prepend, #shift

Constructor Details

#initialize(name, *layers, loader: ->(builder){}) ⇒ Subject

Returns a new instance of Subject.



11
12
13
14
15
16
# File 'lib/pears/subject.rb', line 11

def initialize(name, *layers, loader: ->(builder){} )
  @name = name
  @layers = layers
  @loader = loader
  boot
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/pears/subject.rb', line 9

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/pears/subject.rb', line 18

def [](key)
  project[key]
end

#dig(*args) ⇒ Object



30
31
32
# File 'lib/pears/subject.rb', line 30

def dig(*args)
  project.dig(*args)
end

#each(&block) ⇒ Object



49
50
51
# File 'lib/pears/subject.rb', line 49

def each(&block)
  project.each(&block)
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/pears/subject.rb', line 26

def has_key?(key)
  project.has_key? key
end

#keysObject



22
23
24
# File 'lib/pears/subject.rb', line 22

def keys
  project.keys
end

#projectObject

Projects the layers to a hash that van be used as a single source of truth.



43
44
45
46
47
# File 'lib/pears/subject.rb', line 43

def project
  layers.reverse.reduce(Hash.new) do |hash, layer|
    hash.deep_merge(layer.data)
  end.with_indifferent_access
end

#purgeObject



34
35
36
# File 'lib/pears/subject.rb', line 34

def purge
  @layers.each.each(&:purge)
end

#push_layer(layer) ⇒ Object



38
39
40
# File 'lib/pears/subject.rb', line 38

def push_layer(layer)
  @layers << layer
end

#rebootObject



53
54
55
56
57
# File 'lib/pears/subject.rb', line 53

def reboot
  purge
  @layers = []
  boot
end