Class: Bookshop::ArrayStructures

Inherits:
Object
  • Object
show all
Defined in:
lib/bookshop-array-structures.rb

Class Method Summary collapse

Class Method Details

.build_array_structures(site) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bookshop-array-structures.rb', line 93

def self.build_array_structures(site)
  base_path = "_bookshop/components/"
  if !site.theme.nil?
    base_path = site.theme.root + "/_bookshop/components/"
  end
  site.config["_select_data"] ||= {}
  site.config["_array_structures"] ||= {}
  puts "📚 Parsing Stories..."
  Dir.glob("**/*.stories.{toml,tml,tom,tm}", base: base_path).each do |f|
    begin
      component = TomlRB.load_file(base_path + f)  
    rescue => exception
      puts "❌ Error Parsing Story: " + f
      puts exception
      next
    end
    transformed_component = transform_component(f, component, site)
    array_structures = transformed_component.delete("array_structures")
    array_structures.each{|key|
      begin
        site.config["_array_structures"][key] ||= {}
        site.config["_array_structures"][key]["values"] ||= []
        site.config["_array_structures"][key]["values"].push(transformed_component)
      rescue => exception
        puts "❌ Error Adding Story to Array Structures: " + f
        puts "🤔 Maybe your current _config.yml has conflicting array structures?"
      end
    }
  end
  puts "✅ Finshed Parsing Stories"
  #puts site.config["_array_structures"].inspect
end

.get_component_type(path) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/bookshop-array-structures.rb', line 11

def self.get_component_type(path)
  result = path.split(".").first;
  pathParts = path.split(".").first.split("/")
  if pathParts.length >= 2 && pathParts[pathParts.length-1] === pathParts[pathParts.length-2]
    pathParts.pop
    result = pathParts.join("/")
  end
  return result
end

.get_story_name(path) ⇒ Object



6
7
8
9
# File 'lib/bookshop-array-structures.rb', line 6

def self.get_story_name(path)
  basename = get_component_type(path)
  return basename.split(/-|\//).map(&:capitalize).join(" ")
end

.handle_story(story, site) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bookshop-array-structures.rb', line 21

def self.handle_story(story, site)
  result = {};
  story.each_pair {|key, value|
      if result.has_key?(key) && storyname != "defaults"
        next
      end

      if key.include? "--repeat"
        new_key = key.split("--").first
        result[new_key] = []
        site.config["_array_structures"][new_key] ||= {
          "values" => []
        }

        label = new_key.split("_").map(&:capitalize).join(" ")
        if site.config["_array_structures"][new_key]["values"].select{|value| value["label"] == label}.length > 0
          next
        end
        site.config["_array_structures"][new_key]["values"].push({
          "label" => label,
          "value" => value
        })
      elsif key.include? "--select" or key.include? "--radio" or key.include? "--inline-radio"
        new_key = key.split("--").first
        result[new_key] = nil
        site.config["_select_data"][new_key+"s"] = []
        value.each_value{|option|
          if site.config["_select_data"][new_key+"s"].select{|value| value == option}.length > 0
            next
          end
          site.config["_select_data"][new_key+"s"].push(option)
        }
      elsif key.include? "--multi-select" or key.include? "--check" or key.include? "--inline-check"
        new_key = key.split("--").first
        result[new_key] = []
        site.config["_select_data"][new_key] = []
        value.each_value{|option|
          if site.config["_select_data"][new_key].select{|value| value == option}.length > 0
            next
          end
          site.config["_select_data"][new_key].push(option)
        } 
      elsif key.include? "--"
        new_key = key.split("--").first
        result[new_key] = nil
      else
        result[key] = value
      end
    }
    return result
end

.transform_component(path, component, site) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bookshop-array-structures.rb', line 73

def self.transform_component(path, component, site)
  result = { "value" => {} }
  result["label"] = get_story_name(path)
  result["value"]["_component_type"] = get_component_type(path)
  component.each_pair { |storyname, story|
    if storyname == "meta"
      result.merge!(story)
    else
      result["value"].merge!(handle_story(story, site))
    end
  }
  result["array_structures"] ||= [];
  contains_component = result["array_structures"].select{|value| value == 'components'}.length > 0
  if !contains_component && !result["_hidden"]
    result["array_structures"].push("components")
  end
  result.delete("_hidden") unless result["_hidden"].nil?
  return result
end