Module: Skyline::Settings

Defined in:
lib/skyline/settings.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Catch various virtual accessors and make sure xxx_before_typecast is also set. Typecasts data on setting.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/skyline/settings.rb', line 138

def method_missing(meth,*args)
  base_method = meth.to_s.gsub(/(_before_type_cast)|(=)$/,"").to_sym
  if self[:page] && self.page && self.page.field_names.include?(base_method)
    field = self.page.fields[base_method.to_sym]
    case meth.to_s
      when /=$/ 
        self.data[base_method] = field.type_cast(args.first.blank? ? nil : args.first)
        self.data_before_type_cast[base_method] = args.first
      when /_before_type_cast$/ 
        self.data_before_type_cast[base_method]
      else self.data[base_method]
    end
  else 
    super
  end
end

Instance Method Details

#dataObject



126
127
128
# File 'lib/skyline/settings.rb', line 126

def data
  self[:data] ||= HashWithIndifferentAccess.new    
end

#data=(data) ⇒ Object

Set the data for this page, this works the same as .attributes= on regular ActiveRecord objects.



118
119
120
121
122
123
124
125
# File 'lib/skyline/settings.rb', line 118

def data=(data)
  data = HashWithIndifferentAccess.new(data)
  multi_parameter_attributes = [] 
  data.each do |k,v|
    k.include?("(") ? multi_parameter_attributes << [k,v] : send(k+"=",v)
  end
  assign_multiparameter_attributes(multi_parameter_attributes)
end

#data_before_type_castObject

Returns a hash before it was typecasted, is empty when we didn’t set any data through any virtual accessors yet.



132
133
134
# File 'lib/skyline/settings.rb', line 132

def data_before_type_cast
  @data_before_type_cast ||= HashWithIndifferentAccess.new
end

#fieldsObject

The fields hash of the current settings-page



168
169
170
# File 'lib/skyline/settings.rb', line 168

def fields
  self.page.fields
end

#pageObject

Returns the current settingspage. Every object instance can only represent the data of one page.



174
175
176
# File 'lib/skyline/settings.rb', line 174

def page
  self.class.pages[(self.read_attribute_before_type_cast("page") || @attributes["page"]).to_sym]
end

#respond_to?(meth, include_private = false) ⇒ Boolean

Analogue to method_missing

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
# File 'lib/skyline/settings.rb', line 156

def respond_to?(meth, include_private=false)
  s = super
  return s if s
  
  # Failsafe for stack level errors of calling _page from within AR read_attribute
  return false if meth == "_page"
  
  base_method = meth.to_s.gsub(/(_before_type_cast)|(=)$/,"").to_sym
  self[:page] && self.page && self.page.field_names.include?(base_method)
end