Module: Mist::GitModel::ClassMethods

Included in:
Mist::GitModel
Defined in:
lib/mist/git_model/class_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



2
3
4
5
# File 'lib/mist/git_model/class_methods.rb', line 2

def self.extended(base) #:nodoc:
  base.class_attribute :default_attributes
  base.default_attributes ||= HashWithIndifferentAccess.new
end

Instance Method Details

#[](type) ⇒ Object

Returns meta data for this model.



65
66
67
68
69
70
71
72
# File 'lib/mist/git_model/class_methods.rb', line 65

def [](type)
  @meta ||= {}
  @meta[type] ||= if File.file?(meta_file_path(type))
    YAML.load(File.read(meta_file_path(type))) || {}
  else
    {}
  end.with_indifferent_access
end

#add_attribute_default(name, options) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/mist/git_model/class_methods.rb', line 47

def add_attribute_default(name, options)
  define_method(:"default_#{name}") do                  # def default_id
    if options[:default].kind_of?(Proc)                 #   if options[:default].kind_of?(Proc)
      options[:default].call                            #     options[:default].call
    else                                                #   else
      options[:default]                                 #     options[:default]
    end                                                 #   end
  end                                                   # end
end

#add_attribute_getter(name) ⇒ Object



33
34
35
36
37
# File 'lib/mist/git_model/class_methods.rb', line 33

def add_attribute_getter(name)
  define_method(name) do                                # def id
    attributes[name]                                    #   attributes[:id]
  end                                                   # end
end

#add_attribute_setter(name) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/mist/git_model/class_methods.rb', line 39

def add_attribute_setter(name)
  define_method(:"#{name}=") do |value|                 # def id=(value)
    unless value == attributes[name]                    #   unless value == attributes[:id]
      attributes[name] = value                          #     attributes[:id] = value
    end                                                 # end
  end
end

#allObject



91
92
93
94
95
96
# File 'lib/mist/git_model/class_methods.rb', line 91

def all
  # it's dangerous to rely on Dir[] because we have no guarantee of the
  # returned order. Git will be more reliable.
  files = Mist::GitFileSystemHistory.new(Mist.repository).find(nil, /^#{Regexp::escape table_name}\/?/)
  files.collect! { |path| load Mist.repository_location.join(path) }
end

#attribute(name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/mist/git_model/class_methods.rb', line 24

def attribute(name, options = {})
  name = name.to_sym
  default_attributes[name] = options[:default]
  define_attribute_methods [name.to_s]
  add_attribute_default(name, options)
  add_attribute_getter(name)
  add_attribute_setter(name)
end

#countObject



137
138
139
# File 'lib/mist/git_model/class_methods.rb', line 137

def count
  Dir[Mist.repository_location.join(table_name, '*')].length
end

#create!(attributes = {}) ⇒ Object



131
132
133
134
135
# File 'lib/mist/git_model/class_methods.rb', line 131

def create!(attributes = {})
  new(attributes).tap do |record|
    record.save!
  end
end

#exist?(id) ⇒ Boolean

If the id exists, its file path is returned. Otherwise, nil.

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/mist/git_model/class_methods.rb', line 120

def exist?(id)
  path = record_path(id)
  File.exist?(path) ? path : nil
end

#find(id, new_attributes = {}) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/mist/git_model/class_methods.rb', line 111

def find(id, new_attributes = {})
  if path = exist?(id)
    load path, new_attributes
  else
    nil
  end
end

#inherited(subclass) ⇒ Object

this is necessary because without it default_attributes will be inherited and shared across all subclasses of base, which we don’t exactly want.



10
11
12
13
14
15
16
# File 'lib/mist/git_model/class_methods.rb', line 10

def inherited(subclass) #:nodoc:
  if subclass.default_attributes
    subclass.default_attributes = subclass.default_attributes.dup
  else
    subclass.default_attributes = HashWithIndifferentAccess.new
  end
end

#last(count = nil) ⇒ Object



125
126
127
128
129
# File 'lib/mist/git_model/class_methods.rb', line 125

def last(count = nil)
  files = Mist::GitFileSystemHistory.new(Mist.repository).find(count || 1, /^#{Regexp::escape table_name}\/?/)
  files.collect! { |path| load Mist.repository_location.join(path) }
  count.nil? ? files.first : files
end

#load(path, attributes = {}) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/mist/git_model/class_methods.rb', line 98

def load(path, attributes = {})
  # id is always the filename, ensure id isn't changed by yaml
  attributes = attributes.with_indifferent_access.reverse_merge(YAML.load(File.read(path)))
  attributes['id'] = File.basename(path)
  new(attributes).tap do |instance|
    instance.changed_attributes.clear
  end
end

#meta_file_path(type) ⇒ Object



87
88
89
# File 'lib/mist/git_model/class_methods.rb', line 87

def meta_file_path(type)
  Mist.repository_location.join('.meta', table_name, type.to_s)
end

#record_path(id) ⇒ Object



107
108
109
# File 'lib/mist/git_model/class_methods.rb', line 107

def record_path(id)
  Mist.repository_location.join(table_name, id.to_s)
end

#save_meta_data(type) ⇒ Object

Assigns, saves and commits meta data for this model.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mist/git_model/class_methods.rb', line 75

def (type)
  FileUtils.mkdir_p File.dirname(meta_file_path(type))
  File.open(meta_file_path(type), 'w') { |f| f.print self[type].to_yaml }
  if Mist.
    Mist.repository.add meta_file_path(type)
    Mist.repository.commit '%s meta changes to %s' % [type, table_name]
  end

  # we must force meta to be reloaded because otherwise it could get out of sync with filesystem
  @meta = nil
end

#table_nameObject



57
58
59
60
61
62
# File 'lib/mist/git_model/class_methods.rb', line 57

def table_name
  if name =~ /GitModel/
    raise
  end
  name.underscore.pluralize
end

#timestampsObject



18
19
20
21
22
# File 'lib/mist/git_model/class_methods.rb', line 18

def timestamps
  attribute :created_at, :default => proc { Time.now }
  attribute :updated_at, :default => proc { Time.now }
  before_save { |record| record.updated_at = Time.now }
end