Class: Twine::TwineDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/twine/twine_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ TwineDefinition

Returns a new instance of TwineDefinition.



10
11
12
13
14
15
# File 'lib/twine/twine_file.rb', line 10

def initialize(key)
  @key = key
  @comment = nil
  @tags = nil
  @translations = {}
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



4
5
6
# File 'lib/twine/twine_file.rb', line 4

def comment
  @comment
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/twine/twine_file.rb', line 3

def key
  @key
end

#referenceObject

Returns the value of attribute reference.



7
8
9
# File 'lib/twine/twine_file.rb', line 7

def reference
  @reference
end

#reference_keyObject

Returns the value of attribute reference_key.



8
9
10
# File 'lib/twine/twine_file.rb', line 8

def reference_key
  @reference_key
end

#tagsObject

Returns the value of attribute tags.



5
6
7
# File 'lib/twine/twine_file.rb', line 5

def tags
  @tags
end

#translationsObject (readonly)

Returns the value of attribute translations.



6
7
8
# File 'lib/twine/twine_file.rb', line 6

def translations
  @translations
end

Instance Method Details

#matches_tags?(tags, include_untagged) ⇒ Boolean

[‘tag1’, ‘tag2’], [‘~tag3’]

(tag1 OR tag2) AND (!tag3)

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/twine/twine_file.rb', line 26

def matches_tags?(tags, include_untagged)
  if tags == nil || tags.empty?  # The user did not specify any tags. Everything passes.
    return true
  elsif @tags == nil  # This definition has no tags -> check reference (if any)
    return reference ? reference.matches_tags?(tags, include_untagged) : include_untagged
  elsif @tags.empty?
    return include_untagged
  else
    return tags.all? do |set|
      regular_tags, negated_tags = set.partition { |tag| tag[0] != '~' }
      negated_tags.map! { |tag| tag[1..-1] }
      matches_regular_tags = (!regular_tags.empty? && !(regular_tags & @tags).empty?)
      matches_negated_tags = (!negated_tags.empty? && (negated_tags & @tags).empty?)
      matches_regular_tags or matches_negated_tags
    end
  end

  return false
end

#raw_commentObject



21
22
23
# File 'lib/twine/twine_file.rb', line 21

def raw_comment
  @comment
end

#translation_for_lang(lang) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/twine/twine_file.rb', line 46

def translation_for_lang(lang)
  translation = [lang].flatten.map { |l| @translations[l] }.compact.first

  translation = reference.translation_for_lang(lang) if translation.nil? && reference

  return translation
end