Class: Tml::Tokens::Decoration

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/tokens/decoration.rb

Direct Known Subclasses

XMessage::Decoration

Constant Summary collapse

RESERVED_TOKEN =
'tml'
TOKEN_PLACEHOLDER =
'{$0}'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, token) ⇒ Decoration

Returns a new instance of Decoration.



42
43
44
45
46
47
48
49
# File 'lib/tml/tokens/decoration.rb', line 42

def initialize(label, token)
  @label = label
  @full_name = token.to_s
  @short_name = @full_name

  # removing the numbers at the end - default tokens don't need them
  @default_name = @short_name.gsub(/(\d)*$/, '')
end

Instance Attribute Details

#default_nameObject (readonly)

Returns the value of attribute default_name.



40
41
42
# File 'lib/tml/tokens/decoration.rb', line 40

def default_name
  @default_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



40
41
42
# File 'lib/tml/tokens/decoration.rb', line 40

def full_name
  @full_name
end

#labelObject (readonly)

Returns the value of attribute label.



40
41
42
# File 'lib/tml/tokens/decoration.rb', line 40

def label
  @label
end

#short_nameObject (readonly)

Returns the value of attribute short_name.



40
41
42
# File 'lib/tml/tokens/decoration.rb', line 40

def short_name
  @short_name
end

Instance Method Details

#allowed?(allowed_tokens) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/tml/tokens/decoration.rb', line 80

def allowed?(allowed_tokens)
  return true if allowed_tokens.nil?
  allowed_tokens.include?(short_name)
end

#apply(token_values, token_content, allowed_tokens = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tml/tokens/decoration.rb', line 85

def apply(token_values, token_content, allowed_tokens = nil)
  return token_content if short_name == RESERVED_TOKEN
  return token_content unless allowed?(allowed_tokens)

  method = Tml::Utils.hash_value(token_values, short_name)

  if method
    if method.is_a?(String)
      return method.to_s.gsub(TOKEN_PLACEHOLDER, token_content)
    end

    if method.is_a?(Proc)
      return method.call(token_content)
    end

    if method.is_a?(Array) || method.is_a?(Hash)
      return default_decoration(token_content, method)
    end

    return token_content
  end

  default_decoration(token_content)
end

#default_decoration(token_content, decoration_token_values = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tml/tokens/decoration.rb', line 55

def default_decoration(token_content, decoration_token_values = nil)
  default_decoration = Tml.config.default_token_value(default_name, :decoration)

  unless default_decoration
    return "<#{short_name}>#{token_content}</#{short_name}>"
  end

  # {$0} always represents the actual content
  default_decoration = default_decoration.gsub(TOKEN_PLACEHOLDER, token_content.to_s)

  # substitute the token values with hash elements
  if decoration_token_values.is_a?(Hash)
    decoration_token_values.each do |key, value|
      default_decoration = default_decoration.gsub("{$#{key}}", value.to_s)
    end
  elsif decoration_token_values.is_a?(Array)
    decoration_token_values.each_with_index do |value, index|
      default_decoration = default_decoration.gsub("{$#{index + 1}}", value.to_s)
    end
  end

  # remove unused attributes
  default_decoration.gsub(/\{\$[^}]*\}/, '')
end

#to_sObject



51
52
53
# File 'lib/tml/tokens/decoration.rb', line 51

def to_s
  full_name
end