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.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/skyline/settings.rb', line 142

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



130
131
132
# File 'lib/skyline/settings.rb', line 130

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.



122
123
124
125
126
127
128
129
# File 'lib/skyline/settings.rb', line 122

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.



136
137
138
# File 'lib/skyline/settings.rb', line 136

def data_before_type_cast
  @data_before_type_cast ||= HashWithIndifferentAccess.new
end

#fieldsObject

The fields hash of the current settings-page



166
167
168
# File 'lib/skyline/settings.rb', line 166

def fields
  self.page.fields
end

#pageObject

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



172
173
174
# File 'lib/skyline/settings.rb', line 172

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

#respond_to?(meth) ⇒ Boolean

Analogue to method_missing

Returns:

  • (Boolean)


160
161
162
163
# File 'lib/skyline/settings.rb', line 160

def respond_to?(meth)
  base_method = meth.to_s.gsub(/(_before_type_cast)|(=)$/,"").to_sym
  self[:page] && self.page && self.page.field_names.include?(base_method) || super
end