Class: Colorly::Script

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/colorly/script.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#add, #last, #random, #title

Constructor Details

#initialize(script, filename: nil) ⇒ Script

Returns a new instance of Script.



14
15
16
17
# File 'lib/colorly/script.rb', line 14

def initialize(script, filename: nil)
  @script = script
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/colorly/script.rb', line 5

def filename
  @filename
end

#paramsObject



31
32
33
# File 'lib/colorly/script.rb', line 31

def params
  @params ||= {}
end

#scriptObject (readonly)

Returns the value of attribute script.



5
6
7
# File 'lib/colorly/script.rb', line 5

def script
  @script
end

Class Method Details

.load(script_file) ⇒ Object

Raises:



8
9
10
11
12
# File 'lib/colorly/script.rb', line 8

def self.load(script_file)
  raise ScriptNotFound, "Unable to load #{script_file}" unless File.exist? script_file

  new File.read(script_file), filename: script_file
end

Instance Method Details

#outputObject



27
28
29
# File 'lib/colorly/script.rb', line 27

def output
  @output ||= {}
end

#runObject



19
20
21
22
23
24
25
# File 'lib/colorly/script.rb', line 19

def run
  run!
rescue SyntaxError => e
  raise ScriptSyntaxError, e
rescue => e
  raise ScriptError, e
end

#simple_output(names: false) ⇒ Object Also known as: to_h



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/colorly/script.rb', line 35

def simple_output(names: false)
  output.transform_values do |chroma_array|
    chroma_array.map do |chroma|
      if names
        { hex: chroma.to_hex, name: [chroma.name, chroma.shade] }
      else
        chroma.to_hex
      end
    end
  end
end