Module: Gardener

Defined in:
lib/gardener.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#diff(obj) ⇒ Object

Instance Methods #####



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gardener.rb', line 113

def diff(obj)
  buff = {}
  self.attributes.each do |k, v|
    tmp = obj.send(k) if obj.respond_to?(k)
    if tmp.eql?(v)
      # they're the same, do nothing
    else
      buff[k] = {:original => v, :new => tmp}
    end
  end # end self.attributes.each
  buff
end

#plant_seed(options = {}) ⇒ Object

Called on an particular instance of a class to render it down into a file, from which it can be reaped later.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gardener.rb', line 129

def plant_seed(options = {})
  c = self.class.to_s.underscore.pluralize
  # Code to render down associated items exists, but nothing has been done to pull those objects back out.
  # Not automatically at least, but of course reaping the correct model will pull them up no problem.


  # TODO
  # need to load the file up and see if the thing i'm trying to add is in it
  # or just overwrite the whole file every time.
  # Do something to prevent duplicates from slipping in.
  dir = File.join [Rails.root, 'db', 'garden']
  Dir.mkdir(dir) unless File.exists?(dir)


  File.open(File.join([Rails.root, 'db', 'garden', "#{c}.yml"] ), 'a') do |file|
    file << self.to_yaml
  end

  includes = things_to_include(options)
#    puts includes
  includes.each do |k, v|
    tom = self.send(k) if self.respond_to?(k)
    [*tom].each{|y| y.plant_seed({:include => v})}
  end

end

#things_to_include(options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gardener.rb', line 157

def things_to_include(options)
  # [*THING] is so that THING can be an array or a single instance of a class.
  # .each will blow up if THING is just an instance, not an array, unless we explode and re-array it.
  return [] unless options[:include]
  case options[:include]
  when Hash
    options[:include].inject({}){|memo, v| memo[v[0]] = v[1] if self.respond_to?(v[0]); memo}
  else
    [*options[:include]]
  end
end

#to_fixtureObject



169
170
171
# File 'lib/gardener.rb', line 169

def to_fixture
  {"#{self.class.to_s.underscore}_#{self.id}" => self.attributes}.to_yaml
end