Class: MarkdownRecord::ModelInflator

Inherits:
Object
  • Object
show all
Includes:
MarkdownRecord::Models::Filtering, PathUtilities
Defined in:
lib/markdown_record/models/model_inflator.rb

Instance Method Summary collapse

Methods included from MarkdownRecord::Models::Filtering

#filter_for

Methods included from PathUtilities

#base_content_root_name, #base_rendered_path, #base_rendered_root, #clean_path, #erb_locals_from_path, #fragment_attributes_from_path, #full_path_to_parts, #path_to_fragment_id, #remove_prefix, #rendered_path, #scoped_id_to_parts, #to_scoped_id

Constructor Details

#initialize(force_render = false) ⇒ ModelInflator

Returns a new instance of ModelInflator.



8
9
10
# File 'lib/markdown_record/models/model_inflator.rb', line 8

def initialize(force_render = false)
  @force_render = force_render
end

Instance Method Details

#constantized_models(filters, subdirectory) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/markdown_record/models/model_inflator.rb', line 53

def constantized_models(filters, subdirectory)
  json_hash = json(filters, subdirectory)
  
  json_hash.map do |model|
    model["type"].camelize.safe_constantize&.new(model)
  end
end

#json(filters, subdirectory) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/markdown_record/models/model_inflator.rb', line 31

def json(filters, subdirectory)
  path = json_path(filters, subdirectory)
  json_hash = json_source(path)
  
  json_hash.delete(::MarkdownRecord::ContentFragment.json_klass) if filters.delete(:exclude_fragments)
  json_hash = filters[:klass].present? ? json_hash[filters.delete(:klass).json_klass] : json_hash.values.flatten
  json_hash ||= []
  json_hash
end

#json_path(filters, subdirectory) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/markdown_record/models/model_inflator.rb', line 41

def json_path(filters, subdirectory)
  if subdirectory.nil?
    file_path = base_rendered_path
    file_path = "#{file_path}_fragments" if filters[:klass] == ::MarkdownRecord::ContentFragment
    "#{file_path}.json"
  else
    file_path = subdirectory
    file_path = "#{file_path}_fragments" if filters[:klass] == ::MarkdownRecord::ContentFragment
    "#{base_rendered_path}/#{file_path}.json"
  end
end

#json_source(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/markdown_record/models/model_inflator.rb', line 20

def json_source(path)
  if Pathname.new(path).exist? && !@force_render
    json_hash = JSON.parse(File.read(path))
    return json_hash
  end

  json_hash = ::MarkdownRecord::JsonRenderer.new.render_models_for_subdirectory(subdirectory: "",:concat => true, :deep => true, :save => false, :render_content_fragment_json => true)

  json_hash
end

#passes_filters?(filters, attributes) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/markdown_record/models/model_inflator.rb', line 61

def passes_filters?(filters, attributes)
  filter_for(filters, attributes).passes_filter?
end

#where(filters, subdirectory = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/markdown_record/models/model_inflator.rb', line 12

def where(filters, subdirectory = nil)
  models = constantized_models(filters, subdirectory)
  
  models.select do |model|
    passes_filters?(filters.dup, model.attributes.with_indifferent_access)
  end
end