Class: IssueBeaver::Models::GithubIssue
- Inherits:
-
Object
- Object
- IssueBeaver::Models::GithubIssue
- Includes:
- Shared::AttributesModel
- Defined in:
- lib/issue_beaver/models/github_issue.rb
Constant Summary collapse
- ATTRIBUTES =
[:number, :state, :title, :body, :file, :begin_line, :created_at, :updated_at, :labels, :assignee, :commit]
Class Method Summary collapse
- .all ⇒ Object
- .default_attributes ⇒ Object
- .new_from_github(attrs) ⇒ Object
- .new_from_todo(attrs) ⇒ Object
- .repo_name ⇒ Object
- .use_repository(repository) ⇒ Object
Instance Method Summary collapse
- #changed_attributes_for_update ⇒ Object
- #clean_attributes_from_github ⇒ Object
- #clean_attributes_from_todo ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(attrs = {}) ⇒ GithubIssue
constructor
A new instance of GithubIssue.
- #magic_mark ⇒ Object
- #mark_for_closing(flag = true) ⇒ Object
- #must_close? ⇒ Boolean
- #must_update? ⇒ Boolean
- #new? ⇒ Boolean
- #open? ⇒ Boolean
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #update_attributes_with_limit(attrs) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ GithubIssue
Returns a new instance of GithubIssue.
75 76 77 78 |
# File 'lib/issue_beaver/models/github_issue.rb', line 75 def initialize(attrs = {}) @attributes = Hashie::Mash.new(attrs) ATTRIBUTES.each do |attr| @attributes[attr] ||= nil end end |
Class Method Details
.all ⇒ Object
24 25 26 |
# File 'lib/issue_beaver/models/github_issue.rb', line 24 def self.all Shared::ModelCollection.new(self, @repository.all.map{|attrs| new_from_github(attrs.dup)}) end |
.default_attributes ⇒ Object
16 17 18 |
# File 'lib/issue_beaver/models/github_issue.rb', line 16 def self.default_attributes @repository ? @repository.default_attributes : {} end |
.new_from_github(attrs) ⇒ Object
61 62 63 64 65 |
# File 'lib/issue_beaver/models/github_issue.rb', line 61 def self.new_from_github(attrs) new(attrs).tap do |obj| obj.clean_attributes_from_github end end |
.new_from_todo(attrs) ⇒ Object
68 69 70 71 72 |
# File 'lib/issue_beaver/models/github_issue.rb', line 68 def self.new_from_todo(attrs) new(attrs).tap do |obj| obj.clean_attributes_from_todo end end |
.repo_name ⇒ Object
21 |
# File 'lib/issue_beaver/models/github_issue.rb', line 21 def self.repo_name() @repository.repo if @repository end |
.use_repository(repository) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/issue_beaver/models/github_issue.rb', line 8 def self.use_repository(repository) @repository = repository class << self delegate :update, :create, :first, to: :@repository end end |
Instance Method Details
#changed_attributes_for_update ⇒ Object
56 57 58 |
# File 'lib/issue_beaver/models/github_issue.rb', line 56 def changed_attributes_for_update Hashie::Mash.new(changed_attributes).only(:title, :body, :assignee) end |
#clean_attributes_from_github ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/issue_beaver/models/github_issue.rb', line 81 def clean_attributes_from_github self.attributes.merge! self.class.default_attributes self.created_at = Time.parse(created_at) if created_at.kind_of?(String) self.updated_at = Time.parse(updated_at) if updated_at.kind_of?(String) self.assignee = assignee.login if assignee.respond_to?(:login) self.body ||= "" if details = Grammars::GithubIssueBodyParser.new.parse(self.body).details self.commit = details[:commit] self.begin_line = details[:begin] self.file = details[:file] end @changed_attributes = nil end |
#clean_attributes_from_todo ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/issue_beaver/models/github_issue.rb', line 98 def clean_attributes_from_todo basename = File.basename(self.file) self.title ||= "" self.title = "#{self.title.capitalize} (#{basename}:#{self.begin_line})" github_line_url = "https://github.com/#{self.class.repo_name}/blob/master/#{self.file}\#L#{self.begin_line}" self.body = %Q{#{self.body}\n\n#{github_line_url}\n\n#{magic_mark}} @changed_attributes = nil end |
#closed? ⇒ Boolean
32 |
# File 'lib/issue_beaver/models/github_issue.rb', line 32 def closed?() state == "closed" end |
#magic_mark ⇒ Object
107 108 109 |
# File 'lib/issue_beaver/models/github_issue.rb', line 107 def magic_mark Grammars::GithubIssueBody.build_mark(file, begin_line, commit) end |
#mark_for_closing(flag = true) ⇒ Object
50 51 52 53 |
# File 'lib/issue_beaver/models/github_issue.rb', line 50 def mark_for_closing(flag = true) @must_close = flag self.state = "closed" end |
#must_close? ⇒ Boolean
47 |
# File 'lib/issue_beaver/models/github_issue.rb', line 47 def must_close?() !!@must_close end |
#must_update? ⇒ Boolean
44 |
# File 'lib/issue_beaver/models/github_issue.rb', line 44 def must_update?() changed_attributes_for_update.any? end |
#new? ⇒ Boolean
38 |
# File 'lib/issue_beaver/models/github_issue.rb', line 38 def new?() !number end |
#open? ⇒ Boolean
35 |
# File 'lib/issue_beaver/models/github_issue.rb', line 35 def open?() state == "open" end |
#persisted? ⇒ Boolean
41 |
# File 'lib/issue_beaver/models/github_issue.rb', line 41 def persisted?() !new? && !changed? end |
#save ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/issue_beaver/models/github_issue.rb', line 117 def save if new? @attributes = self.class.create(attributes) elsif changed? @attributes = self.class.update(number, attributes) end @changed_attributes.clear if @changed_attributes true end |
#update_attributes_with_limit(attrs) ⇒ Object
111 112 113 |
# File 'lib/issue_beaver/models/github_issue.rb', line 111 def update_attributes_with_limit(attrs) update_attributes_without_limit(attrs.only(:title, :body, :updated_at, :assignee)) end |