Class: Dugway::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/dugway/theme.rb

Constant Summary collapse

REQUIRED_FILES =
%w( cart.html checkout.html contact.html home.html layout.html maintenance.html product.html products.html screenshot.jpg settings.json success.html theme.css theme.js )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overridden_customization = {}) ⇒ Theme

Returns a new instance of Theme.



14
15
16
# File 'lib/dugway/theme.rb', line 14

def initialize(overridden_customization={})
  @overridden_customization = overridden_customization.stringify_keys
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/dugway/theme.rb', line 12

def errors
  @errors
end

Instance Method Details

#build_file(name) ⇒ Object



71
72
73
74
# File 'lib/dugway/theme.rb', line 71

def build_file(name)
  @building = true
  file_content(name)
end

#customizationObject



38
39
40
41
42
43
44
45
46
# File 'lib/dugway/theme.rb', line 38

def customization
  Hash.new.tap { |customization|
    %w( fonts colors options images image_sets ).each { |type|
      customization.update(customization_for_type(type))
    }

    customization.update(@overridden_customization)
  }
end

#file_content(name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dugway/theme.rb', line 56

def file_content(name)
  case name
  when 'theme.js'
    if @building
      Uglifier.new.compile(sprockets[name].to_s)
    else
      sprockets[name].to_s
    end
  when 'theme.css'
    sprockets[name].to_s
  else
    read_source_file(name)
  end
end

#filesObject



76
77
78
# File 'lib/dugway/theme.rb', line 76

def files
  REQUIRED_FILES + image_files + font_files
end

#font_filesObject



86
87
88
89
90
# File 'lib/dugway/theme.rb', line 86

def font_files
  Dir.glob(File.join(source_dir, 'fonts', '**', '*.{eot,ttf,otf,woff,svg}')).map { |i|
    i.gsub(source_dir, '')[1..-1]
  }
end

#fontsObject



26
27
28
# File 'lib/dugway/theme.rb', line 26

def fonts
  customization_for_type('fonts')
end

#image_filesObject



80
81
82
83
84
# File 'lib/dugway/theme.rb', line 80

def image_files
  Dir.glob(File.join(source_dir, 'images', '**', '*.{png,jpg,jpeg,gif,ico,svg}')).map { |i|
    i.gsub(source_dir, '')[1..-1]
  }
end

#image_setsObject



34
35
36
# File 'lib/dugway/theme.rb', line 34

def image_sets
  customization_for_type('image_sets')
end

#imagesObject



30
31
32
# File 'lib/dugway/theme.rb', line 30

def images
  customization_for_type('images')
end

#layoutObject



18
19
20
# File 'lib/dugway/theme.rb', line 18

def layout
  read_source_file('layout.html')
end

#nameObject



48
49
50
# File 'lib/dugway/theme.rb', line 48

def name
  settings['name']
end

#settingsObject



22
23
24
# File 'lib/dugway/theme.rb', line 22

def settings
  JSON.parse(read_source_file('settings.json'))
end

#valid?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dugway/theme.rb', line 92

def valid?
  @errors = []

  REQUIRED_FILES.each { |file|
    @errors << "Missing source/#{ file }" if read_source_file(file).nil?
  }

  @errors << 'Missing theme name in source/settings.json' if name.blank?
  @errors << 'Invalid theme version in source/settings.json (ex: 1.0.3)' unless !!(version =~ /\d+\.\d+\.\d+/)
  @errors << 'Missing images in source/images' if image_files.empty?

  @errors.empty?
end

#versionObject



52
53
54
# File 'lib/dugway/theme.rb', line 52

def version
  settings['version']
end