Class: Cockpit::AR::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/cockpit/stores/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, context = "default") ⇒ Store

Returns a new instance of Store.



81
82
83
84
# File 'lib/cockpit/stores/active_record.rb', line 81

def initialize(record, context = "default")
  @record = record
  @context = context
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



79
80
81
# File 'lib/cockpit/stores/active_record.rb', line 79

def cache
  @cache
end

#contextObject (readonly)

Returns the value of attribute context.



79
80
81
# File 'lib/cockpit/stores/active_record.rb', line 79

def context
  @context
end

#recordObject (readonly)

Returns the value of attribute record.



79
80
81
# File 'lib/cockpit/stores/active_record.rb', line 79

def record
  @record
end

Instance Method Details

#[](key) ⇒ Object



94
95
96
97
# File 'lib/cockpit/stores/active_record.rb', line 94

def [](key)
  setting = find_setting(key)
  setting ? setting.parsed_value : nil
end

#[]=(key, value) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cockpit/stores/active_record.rb', line 99

def []=(key, value)
  record.save if record && record.new_record?
  
  setting = find_setting(key)
  #{:value => {'root' => value}.to_json}
  attributes = {:value => value.to_json}.merge(configurable_attributes)
  if setting
    setting.update_attributes!(attributes)
  else
    setting = Setting.new(attributes)
    setting.key = key
    record.settings << setting if record
    setting[:configurable_type] = attributes[:configurable_type]
    setting.save
  end
  
  @cache = nil
  
  setting.parsed_value
end

#clearObject



129
130
131
# File 'lib/cockpit/stores/active_record.rb', line 129

def clear
  record.settings.destroy_all
end

#delete(key) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/cockpit/stores/active_record.rb', line 120

def delete(key)
  setting = find_setting(key)
  if setting
    setting.destroy
    setting.parsed_value
    record.reload if record
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/cockpit/stores/active_record.rb', line 90

def has_key?(key)
  key?(key)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/cockpit/stores/active_record.rb', line 86

def key?(key)
  !!find_setting(key)
end

#update_setting(setting) ⇒ Object



133
134
135
136
137
# File 'lib/cockpit/stores/active_record.rb', line 133

def update_setting(setting)
  cache.map! do |item|
    item.id == setting.id ? setting : item
  end if cache
end