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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bookshop-array-structures.rb', line 63

def self.build_array_structures(site)
  base_path = ""
  if !site.theme.nil?
    base_path = site.theme.root + "/_bookshop/components/"
  end
  site.config["_select_data"] ||= {}
  site.config["_array_structures"] ||= {}
  site.config["_array_structures"]["components"] ||= {
    "values" => []
  }
  Dir.glob("**/*.stories.{toml,tml,tom,tm}", base: base_path).each do |f|
    component = TomlRB.load_file(base_path + f)
    site.config["_array_structures"]["components"]["values"].push({
      "label" => get_story_name(f),
      "value" => transform_component(f, component, site)
    })
  end
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 = path.split("/").last.split(".").first
  return basename.split("-").map(&:capitalize).join(" ")
end

.transform_component(path, component, site) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
# File 'lib/bookshop-array-structures.rb', line 11

def self.transform_component(path, component, site)
  result = {}
  result['_component_type'] = path.split(".").first;
  component.each_pair { |storyname, story|
    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)
        } 
      else
        result[key] = value
      end
    }
  }
  return result
end