Class: Regulate::Git::Model::Base

Inherits:
Object
  • Object
show all
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

Page

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, persisted = false) ⇒ Base

Standard class init

Parameters:

  • attributes (Hash) (defaults to: {})

    Attributes to instantiate our resource with

  • new_record (TrueClass, FalseClass)

    Whether we are dealing with a new record or not



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

Examples:

Setting Custom Model Attributes

attributes :id, :commit_message, :author_name, :author_email, :title, :view

Parameters:

  • An (Array)

    array of attribute names as symbols



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

Parameters:

  • The (String)

    ID to check for

Returns:

  • (TrueClass, FalseClass)

    Whether or not the resource exists



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

Returns:

  • An instance of whatever class extends this base class

Raises:



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

Returns:

  • An instance of whatever class extends this base class

Raises:



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_allObject

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

Returns:

  • an instance of whatever class extends this base class

Raises:



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

Returns:

  • an instance of whatever class extends this base class

Raises:



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

#attributesHash

This lets us call Instance.attributes and get accurate data in hash form

Examples:

Set Model Attributes

class MyModel < Regulate::Git::Model::Base
  attributes :my_custom_attribute
end
@my_model = MyModel.new
@my_model.my_custom_attribute = "my_custom_value"
@my_model.attributes

Returns:

  • (Hash)

    Our resource attributes



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_htmlObject

Replaces mustache style syntax with appropriate instance attribute values

Examples:

Rendering a view for a git commit

class MyModel < Regulate::Git::Model::Base
  attributes :my_fancy_subtitle
end
@my_model = MyModel.new
@my_model.my_fancy_subtitle => "A Fancy Subtitle"
@my_model.view = "<h3>{{my_fancy_subtitle}}</h3>"
@my_model.build_rendered_html


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

#destroyObject

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 => commit_message || "Deleting resource #{title}",
    :author_name => author_name,
    :author_email => author_email
  })
end

#persisted?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/regulate/git/model/base.rb', line 146

def persisted?
  @persisted || false
end

#published?Boolean

Override the published? method

Returns:

  • (Boolean)


264
265
266
# File 'lib/regulate/git/model/base.rb', line 264

def published?
  published == true || published == "1" || published == "t" || published == "true"
end

#renderedObject

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

#saveTrueClass, FalseClass

Attempt to save our record

Returns:

  • (TrueClass, FalseClass)

    Whether or not the record was saved



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 => commit_message || "#{ ( persisted? ) ? "Updating" : "Creating" } resource #{title}",
      :author_name => author_name,
      :author_email => 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

Raises:



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

Parameters:

  • args (Hash) (defaults to: {})

    Our attributes



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

Parameters:

  • args (Hash) (defaults to: {})

    Our attributes



69
70
71
72
# File 'lib/regulate/git/model/base.rb', line 69

def update_attributes!(args = {})
  assign_attributes(args)
  save!
end

#versionsObject

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