lib/style_palette.rb in style_palette-0.0.1 vs lib/style_palette.rb in style_palette-0.0.3

- old
+ new

@@ -3,10 +3,12 @@ require_relative "style_palette/version" require_relative "style_palette/helper" module StylePalette + attr_reader :palettes_config + def self.brush(word, palette_name) word = word.to_s palette_name = palette_name.to_sym raise ArgumentError, "Palette not found '#{palette_name}'" unless palettes[palette_name] @@ -35,16 +37,32 @@ index = word.each_byte.inject( &:+ ) palettes[palette_name][ index % palettes[palette_name].length ] end def self.palettes_config=(style_palettes_file_path) + style_palettes_json = File.read(style_palettes_file_path) + palettes_config_json style_palettes_json + end + + def self.palettes_config + @palettes_config + end + + def self.palettes_config_json(style_palettes_json) + @palettes_config = style_palettes_json + palettes = {} - JSON.parse(File.read(style_palettes_file_path)).each do |palette_name, palette| + JSON.parse(style_palettes_json).each do |palette_name, palette| palettes[palette_name.to_sym] = palette.map { |e| OpenStruct.new(e) } end @palettes = palettes + end + + def self.reset + @palettes = nil + @palettes_config = nil end def self.palettes @palettes || raise(Exception, "Palettes not initialized, try `StylePalette.palettes_config = <file_path>`") end \ No newline at end of file