Class: Widget::Data

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/widget/widget_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(clean_slate = false) ⇒ Data

Returns a new instance of Data.



9
10
11
12
13
# File 'lib/widget/widget_data.rb', line 9

def initialize(clean_slate=false)
  hm = HashModel.new
  save_widgets(HashModel.new) unless File.exist?(HASHMODEL_FILE) || clean_slate
  @widgets = load_widgets
end

Instance Method Details

#create(name) ⇒ Object

CRUD



22
23
24
25
26
27
28
# File 'lib/widget/widget_data.rb', line 22

def create(name)
  return false unless @widgets.where(name).empty?
  time_now = now
  @widgets.add(:name=>name, :created_at=>time_now, :modified_at=>time_now)
  save_widgets
  true
end

#delete(name) ⇒ Object



41
42
43
44
45
46
# File 'lib/widget/widget_data.rb', line 41

def delete(name)
  return false if @widgets.where(name).empty?
  @widgets.delete!(name)
  save_widgets
  true
end

#delete_allObject

CRUD helpers



50
51
52
53
# File 'lib/widget/widget_data.rb', line 50

def delete_all
  @widgets.clear
  save_widgets
end

#lengthObject Also known as: count



15
16
17
# File 'lib/widget/widget_data.rb', line 15

def length
  @widgets.length
end

#read(name) ⇒ Object



30
31
32
# File 'lib/widget/widget_data.rb', line 30

def read(name)
  @widgets.where(name)
end

#read_allObject



55
56
57
# File 'lib/widget/widget_data.rb', line 55

def read_all
  @widgets.clone
end

#reloadObject



59
60
61
# File 'lib/widget/widget_data.rb', line 59

def reload
  load_widgets
end

#update(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/widget/widget_data.rb', line 34

def update(name)
  # note the destructive update! method
  changed = !(@widgets.update!(name, :modified_at=>now).empty?)
  save_widgets
  changed
end