Class: SocialSnippet::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Tag

Create instance

Parameters:

  • s (String)

    tag line text


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/social_snippet/tag.rb', line 13

def initialize(s)
  @path   = SocialSnippet::Tag.get_path(s)
  @repo   = SocialSnippet::Tag.get_repo(s)
  @ref    = SocialSnippet::Tag.get_ref(s)
  @prefix = SocialSnippet::Tag.get_prefix(s)
  @suffix = SocialSnippet::Tag.get_suffix(s)
  @spaces = SocialSnippet::Tag.get_spaces(s)

  # to normalize repo's path
  set_path SocialSnippet::Tag.get_path(s)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


3
4
5
# File 'lib/social_snippet/tag.rb', line 3

def path
  @path
end

#prefixObject (readonly)

Returns the value of attribute prefix


6
7
8
# File 'lib/social_snippet/tag.rb', line 6

def prefix
  @prefix
end

#refObject (readonly)

Returns the value of attribute ref


5
6
7
# File 'lib/social_snippet/tag.rb', line 5

def ref
  @ref
end

#repoObject (readonly)

Returns the value of attribute repo


4
5
6
# File 'lib/social_snippet/tag.rb', line 4

def repo
  @repo
end

#spacesObject (readonly)

Returns the value of attribute spaces


8
9
10
# File 'lib/social_snippet/tag.rb', line 8

def spaces
  @spaces
end

#suffixObject (readonly)

Returns the value of attribute suffix


7
8
9
# File 'lib/social_snippet/tag.rb', line 7

def suffix
  @suffix
end

Class Method Details

.get_path(s) ⇒ Object

Get path from given line


164
165
166
167
168
169
170
171
172
173
# File 'lib/social_snippet/tag.rb', line 164

def get_path(s)
  if is_snip_or_snippet_tag_line(s)
    # return snippet path (without repo name)
    path = ::Pathname.new(/<(.*?:)?(.*?)>/.match(s)[2])
    return path.cleanpath.to_s
  end

  # return empty string
  return ""
end

.get_prefix(s) ⇒ Object

Get prefix from given line


153
154
155
156
157
158
159
160
161
# File 'lib/social_snippet/tag.rb', line 153

def get_prefix(s)
  if is_snip_or_snippet_tag_line(s)
    # return prefix text
    return /(.*?)@/.match(s)[1]
  end

  # return empty string
  return ""
end

.get_ref(s) ⇒ Object

Get repo's ref from given line


176
177
178
179
180
181
182
183
# File 'lib/social_snippet/tag.rb', line 176

def get_ref(s)
  if is_tag_line_with_repository_have_ref(s)
    # return ref text
    return /<.+?#(.*?):/.match(s)[1]
  end

  return ""
end

.get_repo(s) ⇒ Object

Get repository name from given line


186
187
188
189
190
191
192
193
194
# File 'lib/social_snippet/tag.rb', line 186

def get_repo(s)
  if is_tag_line_with_repository(s)
    # return repository name
    return /<(.*?)[:#]/.match(s)[1]
  end

  # return empty string
  return ""
end

.get_spaces(s) ⇒ Object

Get spaces from given line


131
132
133
134
135
136
137
138
139
# File 'lib/social_snippet/tag.rb', line 131

def get_spaces(s)
  if is_snip_or_snippet_tag_line(s)
    # return spaces
    return /(@snip|@snippet)(\s*?)</.match(s)[2]
  end

  # return empty string
  return ""
end

.get_suffix(s) ⇒ Object

Get suffix from given line


142
143
144
145
146
147
148
149
150
# File 'lib/social_snippet/tag.rb', line 142

def get_suffix(s)
  if is_snip_or_snippet_tag_line(s)
    # return suffix text
    return />(.*)/.match(s)[1]
  end

  # return empty string
  return ""
end

.has_colon(s) ⇒ Object

Check given line to match `:` character


117
118
119
# File 'lib/social_snippet/tag.rb', line 117

def has_colon(s)
  return /:/ === s
end

.has_ref_text(s) ⇒ Object

Check given line to have `#` character


112
113
114
# File 'lib/social_snippet/tag.rb', line 112

def has_ref_text(s)
  return /<.*?#(.*?):/ === s
end

.is_snip_or_snippet_tag_line(s) ⇒ Object

Check given line to match @snip or @snippet tag


107
108
109
# File 'lib/social_snippet/tag.rb', line 107

def is_snip_or_snippet_tag_line(s)
  return is_snip_tag_line(s) || is_snippet_tag_line(s)
end

.is_snip_tag_line(s) ⇒ Object

Check given line to match @snip tag


97
98
99
# File 'lib/social_snippet/tag.rb', line 97

def is_snip_tag_line(s)
  return /@snip\s*<.*?>/ === s
end

.is_snippet_tag_line(s) ⇒ Object

Check given line to match @snippet tag


102
103
104
# File 'lib/social_snippet/tag.rb', line 102

def is_snippet_tag_line(s)
  return /@snippet\s*<.*?>/ === s
end

.is_tag_line_with_repository(s) ⇒ Object

Check given line to match snippet tag with repo


122
123
124
# File 'lib/social_snippet/tag.rb', line 122

def is_tag_line_with_repository(s)
  return is_snip_or_snippet_tag_line(s) && has_colon(s)
end

.is_tag_line_with_repository_have_ref(s) ⇒ Object


126
127
128
# File 'lib/social_snippet/tag.rb', line 126

def is_tag_line_with_repository_have_ref(s)
  return is_tag_line_with_repository(s) && has_ref_text(s)
end

Instance Method Details

#has_ref?Boolean

Check to have ref

Returns:

  • (Boolean)

57
58
59
# File 'lib/social_snippet/tag.rb', line 57

def has_ref?
  return ref.nil? === false && ref != ""
end

#has_repo?Boolean

Check to have repository

Returns:

  • (Boolean)

62
63
64
# File 'lib/social_snippet/tag.rb', line 62

def has_repo?
  return repo != ""
end

#normalize_path(path) ⇒ Object


39
40
41
42
43
44
# File 'lib/social_snippet/tag.rb', line 39

def normalize_path(path)
  # repo:/path/to/file -> repo:path/to/file
  path[0] = "" if has_repo? && path[0] == "/"

  path
end

#set_by_tag(base_tag) ⇒ Object

Set information by another tag


26
27
28
29
30
31
32
# File 'lib/social_snippet/tag.rb', line 26

def set_by_tag(base_tag)
  return self if base_tag.nil?
  @prefix = base_tag.prefix
  @suffix = base_tag.suffix
  @spaces = base_tag.spaces
  self
end

#set_path(new_path) ⇒ Object

Set path


35
36
37
# File 'lib/social_snippet/tag.rb', line 35

def set_path(new_path)
  @path = normalize_path(new_path)
end

#set_ref(new_ref) ⇒ Object

Set ref


52
53
54
# File 'lib/social_snippet/tag.rb', line 52

def set_ref(new_ref)
  @ref = new_ref
end

#set_repo(new_repo) ⇒ Object

Set repo


47
48
49
# File 'lib/social_snippet/tag.rb', line 47

def set_repo(new_repo)
  @repo = new_repo
end

#to_pathObject

Get path text


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/social_snippet/tag.rb', line 67

def to_path
  if has_repo?
    if has_ref?
      "#{repo}##{ref}:#{path}"
    else
      "#{repo}:#{path}"
    end
  else
    "#{path}"
  end
end

#to_snip_tagObject

Get @snip tag text


85
86
87
# File 'lib/social_snippet/tag.rb', line 85

def to_snip_tag
  return to_tag_text("@snip")
end

#to_snippet_tagObject

Get @snippet tag text


90
91
92
# File 'lib/social_snippet/tag.rb', line 90

def to_snippet_tag
  return to_tag_text("@snippet")
end

#to_tag_text(tag_text) ⇒ Object

Get tag text by given tag text


80
81
82
# File 'lib/social_snippet/tag.rb', line 80

def to_tag_text(tag_text)
  "#{prefix}#{tag_text}#{spaces}<#{to_path}>#{suffix}"
end