Class: Frontman::DataStore

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/frontman/data_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, base_file_name = nil, parent = nil) ⇒ DataStore

Returns a new instance of DataStore.



21
22
23
24
25
26
27
28
# File 'lib/frontman/data_store.rb', line 21

def initialize(path, base_file_name = nil, parent = nil)
  @path = path
  @cache = {}
  @parent = parent
  @base_file_name = base_file_name

  load_files
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/frontman/data_store.rb', line 46

def method_missing(method_id, *arguments, &block)
  method_name = method_id

  # Make sure we are able to access data files with an array syntax
  method_name = arguments[0].to_sym if method_id == :[]

  if @cache.key?(method_name.to_s)
    cached = @cache[method_name.to_s]

    if Frontman::App.instance.refresh_data_files
      cached.refresh if cached.is_a?(Frontman::DataStoreFile)
    end

    return cached
  end

  # Make sure we forward the access to the data
  return @cache.public_send(method_name, &block) if @cache.respond_to?(method_name)

  nil
end

Instance Attribute Details

#base_file_nameObject (readonly)

Returns the value of attribute base_file_name.



12
13
14
# File 'lib/frontman/data_store.rb', line 12

def base_file_name
  @base_file_name
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/frontman/data_store.rb', line 12

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/frontman/data_store.rb', line 12

def path
  @path
end

Instance Method Details

#flattenObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/frontman/data_store.rb', line 31

def flatten
  elements = []

  @cache.sort_by { |_k, v| v.path }.each do |_k, v|
    if v.instance_of?(Frontman::DataStoreFile)
      v.refresh
      elements.push(v)
    else
      elements.push(*v.flatten)
    end
  end

  elements
end

#respond_to_missing?(method_name, _) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/frontman/data_store.rb', line 68

def respond_to_missing?(method_name, _)
  @cache.key?(method_name.to_s) || @cache.respond_to?(method_name)
end

#to_sObject



73
74
75
# File 'lib/frontman/data_store.rb', line 73

def to_s
  "<DataStore #{@cache.keys.join(', ')} >"
end