Module: Skyline::Settings
- Defined in:
- lib/skyline/settings.rb
Defined Under Namespace
Modules: KlassMethods
Instance Method Summary collapse
- #data ⇒ Object
-
#data=(data) ⇒ Object
Set the data for this page, this works the same as .attributes= on regular ActiveRecord objects.
-
#data_before_type_cast ⇒ Object
Returns a hash before it was typecasted, is empty when we didn’t set any data through any virtual accessors yet.
-
#fields ⇒ Object
The fields hash of the current settings-page.
-
#method_missing(meth, *args) ⇒ Object
Catch various virtual accessors and make sure xxx_before_typecast is also set.
-
#page ⇒ Object
Returns the current settingspage.
-
#respond_to?(meth) ⇒ Boolean
Analogue to method_missing.
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.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/skyline/settings.rb', line 132 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
#data ⇒ Object
120 121 122 |
# File 'lib/skyline/settings.rb', line 120 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.
112 113 114 115 116 117 118 119 |
# File 'lib/skyline/settings.rb', line 112 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_cast ⇒ Object
Returns a hash before it was typecasted, is empty when we didn’t set any data through any virtual accessors yet.
126 127 128 |
# File 'lib/skyline/settings.rb', line 126 def data_before_type_cast @data_before_type_cast ||= HashWithIndifferentAccess.new end |
#fields ⇒ Object
The fields hash of the current settings-page
156 157 158 |
# File 'lib/skyline/settings.rb', line 156 def fields self.page.fields end |
#page ⇒ Object
Returns the current settingspage. Every object instance can only represent the data of one page.
162 163 164 |
# File 'lib/skyline/settings.rb', line 162 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
150 151 152 153 |
# File 'lib/skyline/settings.rb', line 150 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 |