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.



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

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#paramsObject



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

def params
  @params ||= {}
end

#scriptObject (readonly)

Returns the value of attribute script.



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

def script
  @script
end

Class Method Details

.load(script_file) ⇒ Object

Raises:



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

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



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

def output
  @output ||= {}
end

#runObject



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

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



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

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