Class: Regulate::Git::Model::Base
- Inherits:
-
Object
- Object
- Regulate::Git::Model::Base
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Serialization, ActiveModel::Validations
- Defined in:
- lib/regulate/git/model/base.rb
Overview
Our standard base class for all git resources
Direct Known Subclasses
Class Method Summary collapse
-
.attributes(*names) ⇒ Object
This is setting smart attribute methods for our custom model attributes This is useful for elements that you have the foresight to know you’ll need ahead of time.
- .create(attributes = {}) ⇒ Object
- .create!(attributes = {}) ⇒ Object
-
.exists?(id) ⇒ TrueClass, FalseClass
Check whether or not an item in our repo already exists with the given ID.
-
.find(id) ⇒ Object
Search for by id, and return, a valid resource from the repo.
-
.find!(id) ⇒ Object
Search for by id, and return, a valid resource from the repo.
-
.find_all ⇒ Object
Return new objects for each non-nil git item found.
-
.find_by_version(id, commit_sha) ⇒ Object
Search for by version, and return, a valid resource from the repo.
-
.find_by_version!(id, commit_sha) ⇒ Object
Search for by version, and return, a valid resource from the repo.
Instance Method Summary collapse
-
#attributes ⇒ Hash
This lets us call Instance.attributes and get accurate data in hash form.
-
#build_rendered_html ⇒ Object
Replaces mustache style syntax with appropriate instance attribute values.
-
#destroy ⇒ Object
Delete our object from the git repo.
-
#initialize(attributes = {}, persisted = false) ⇒ Base
constructor
Standard class init.
- #persisted? ⇒ Boolean
-
#published? ⇒ Boolean
Override the published? method.
-
#rendered ⇒ Object
Return the rendered.html file from the repo or build the rendered html.
-
#save ⇒ TrueClass, FalseClass
Attempt to save our record.
-
#save! ⇒ Object
Attempt to save our record Make sure we are throwing an exception on failure.
-
#update_attributes(args = {}) ⇒ Object
Allow attributes to be mass assigned then saved.
-
#update_attributes!(args = {}) ⇒ Object
Allow attributes to be mass assigned then saved Will throw exceptions on error.
-
#versions ⇒ Object
Return a list of Grit::Commit objects for the given id.
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ Base
Standard class init
29 30 31 32 33 |
# File 'lib/regulate/git/model/base.rb', line 29 def initialize( attributes = {} , persisted = false ) @persisted = persisted @edit_regions = {} if @persisted assign_attributes(attributes) end |
Class Method Details
.attributes(*names) ⇒ Object
This is setting smart attribute methods for our custom model attributes This is useful for elements that you have the foresight to know you’ll need ahead of time
188 189 190 191 192 |
# File 'lib/regulate/git/model/base.rb', line 188 def attributes(*names) attr_accessor *names define_attribute_methods names self._attributes += names end |
.create(attributes = {}) ⇒ Object
246 247 248 249 250 |
# File 'lib/regulate/git/model/base.rb', line 246 def create( attributes = {} ) temp = self.new(attributes) temp.save return self.find(temp.id) end |
.create!(attributes = {}) ⇒ Object
252 253 254 255 256 |
# File 'lib/regulate/git/model/base.rb', line 252 def create!( attributes = {} ) temp = self.new(attributes) temp.save! return self.find!(temp.id) end |
.exists?(id) ⇒ TrueClass, FalseClass
Check whether or not an item in our repo already exists with the given ID
198 199 200 |
# File 'lib/regulate/git/model/base.rb', line 198 def exists?(id) return Regulate::Git::Interface.exists?(id) end |
.find(id) ⇒ Object
Search for by id, and return, a valid resource from the repo
206 207 208 209 |
# File 'lib/regulate/git/model/base.rb', line 206 def find(id) resource_data = Regulate::Git::Interface.find(id) self.new_from_git( resource_data ) end |
.find!(id) ⇒ Object
Search for by id, and return, a valid resource from the repo
215 216 217 218 |
# File 'lib/regulate/git/model/base.rb', line 215 def find!(id) resource_data = Regulate::Git::Interface.find(id) || raise(Regulate::Git::Errors::PageDoesNotExist) self.new_from_git( resource_data ) end |
.find_all ⇒ Object
Return new objects for each non-nil git item found
240 241 242 243 244 |
# File 'lib/regulate/git/model/base.rb', line 240 def find_all Regulate::Git::Interface.find_all.collect do |resource_data| self.new_from_git( resource_data ) end end |
.find_by_version(id, commit_sha) ⇒ Object
Search for by version, and return, a valid resource from the repo
224 225 226 227 |
# File 'lib/regulate/git/model/base.rb', line 224 def find_by_version(id,commit_sha) resource_data = Regulate::Git::Interface.find_by_version(id,commit_sha) self.new_from_git( resource_data ) end |
.find_by_version!(id, commit_sha) ⇒ Object
Search for by version, and return, a valid resource from the repo
233 234 235 236 |
# File 'lib/regulate/git/model/base.rb', line 233 def find_by_version!(id,commit_sha) resource_data = Regulate::Git::Interface.find_by_version(id,commit_sha) || raise(Regulate::Git::Errors::PageDoesNotExist) self.new_from_git( resource_data ) end |
Instance Method Details
#attributes ⇒ Hash
This lets us call Instance.attributes and get accurate data in hash form
171 172 173 174 175 176 |
# File 'lib/regulate/git/model/base.rb', line 171 def attributes self._attributes.inject({}) do |hash, attr| hash[attr.to_s] = send(attr) hash end end |
#build_rendered_html ⇒ Object
Replaces mustache style syntax with appropriate instance attribute values
138 139 140 141 142 143 144 |
# File 'lib/regulate/git/model/base.rb', line 138 def build_rendered_html rendered = self.view.dup rendered.gsub!( /\{\{(.+?)\}\}/ ) do |match| attributes[$1] || attributes["edit_regions"][$1] || "" end rendered end |
#destroy ⇒ Object
Delete our object from the git repo
36 37 38 39 40 41 42 43 44 |
# File 'lib/regulate/git/model/base.rb', line 36 def destroy clear_cached_vars Regulate::Git::Interface.delete({ :id => id, :commit_message => || "Deleting resource #{title}", :author_name => , :author_email => }) end |
#persisted? ⇒ Boolean
146 147 148 |
# File 'lib/regulate/git/model/base.rb', line 146 def persisted? @persisted || false end |
#published? ⇒ Boolean
Override the published? method
264 265 266 |
# File 'lib/regulate/git/model/base.rb', line 264 def published? published == true || published == "1" || published == "t" || published == "true" end |
#rendered ⇒ Object
Return the rendered.html file from the repo or build the rendered html
52 53 54 55 |
# File 'lib/regulate/git/model/base.rb', line 52 def rendered @_rendered ||= ( persisted? ) ? Regulate::Git::Interface.find_rendered(id) : build_rendered_html @_rendered end |
#save ⇒ TrueClass, FalseClass
Attempt to save our record
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/regulate/git/model/base.rb', line 76 def save if valid? if !persisted? # Object create # A git object with that id already exists return false if self.class.exists?(id) else # Object update # No git ID with the current id exists # You are not allowed to change the ID of an object return false if !self.class.exists?(id) end clear_cached_vars Regulate::Git::Interface.save({ :id => id, :commit_message => || "#{ ( persisted? ) ? "Updating" : "Creating" } resource #{title}", :author_name => , :author_email => , :attributes => attributes.to_json(:except => ['author_email', 'author_name', 'commit_message']), :rendered => build_rendered_html }) @persisted = true else false end end |
#save! ⇒ Object
Attempt to save our record Make sure we are throwing an exception on failure
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/regulate/git/model/base.rb', line 109 def save! if valid? if !persisted? # Object create # A git object with that id already exists raise Regulate::Git::Errors::DuplicatePageError if self.class.exists?(id) else # Object update # No git ID with the current id exists # You are not allowed to change the ID of an object raise Regulate::Git::Errors::PageDoesNotExist if !self.class.exists?(id) end save else raise Regulate::Git::Errors::InvalidGitResourceError end end |
#update_attributes(args = {}) ⇒ Object
Allow attributes to be mass assigned then saved
60 61 62 63 |
# File 'lib/regulate/git/model/base.rb', line 60 def update_attributes(args = {}) assign_attributes(args) save end |
#update_attributes!(args = {}) ⇒ Object
Allow attributes to be mass assigned then saved Will throw exceptions on error
69 70 71 72 |
# File 'lib/regulate/git/model/base.rb', line 69 def update_attributes!(args = {}) assign_attributes(args) save! end |
#versions ⇒ Object
Return a list of Grit::Commit objects for the given id
47 48 49 |
# File 'lib/regulate/git/model/base.rb', line 47 def versions return @_versions ||= Regulate::Git::Interface.commits(id) end |