Class: Hokusai::Style

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/style.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Style

Returns a new instance of Style.



60
61
62
# File 'ui/src/hokusai/style.rb', line 60

def initialize(elements)
  @elements = elements
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



3
4
5
# File 'ui/src/hokusai/style.rb', line 3

def raw
  @raw
end

Class Method Details

.parse(template) ⇒ Object



5
6
7
8
# File 'ui/src/hokusai/style.rb', line 5

def self.parse(template)
  raw = LibHokusai.parse_style(template)
  new(parse_styles(raw))
end

.parse_attributes(raw) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'ui/src/hokusai/style.rb', line 22

def self.parse_attributes(raw)
  attributes = {}

  until raw.null?
    case raw[:type]
    when :style_int
      value = raw[:value].to_i
    when :style_bool
      value = (raw[:value] == "true")
    when :style_float
      value = raw[:value].to_f
    when :style_string
      value = raw[:value]
    when :style_func
      value = parse_function(raw[:function_name], raw[:value])
    end

    attributes[raw[:name]] = value
    
    raw = raw[:next]
  end

  attributes
end

.parse_function(name, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'ui/src/hokusai/style.rb', line 47

def self.parse_function(name, value)
  case name
  when "rgb"
    Color.convert(value)
  when "outline"
    Outline.convert(value)
  when "padding"
    Padding.convert(value)
  else
    raise Hokusai::Error.new("Unknown style function #{name}")
  end
end

.parse_styles(raw) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'ui/src/hokusai/style.rb', line 10

def self.parse_styles(raw)
  styles = {}

  until raw.null?
    styles[raw[:name]] = parse_attributes(raw[:attributes])

    raw = raw[:next]
  end

  styles
end

Instance Method Details

#[](element_name) ⇒ Object



64
65
66
# File 'ui/src/hokusai/style.rb', line 64

def [](element_name)
  @elements[element_name]
end

#keysObject



68
69
70
# File 'ui/src/hokusai/style.rb', line 68

def keys
  @elements.keys
end