Module: RubyLess::SafeClass::ClassMethods
- Defined in:
- lib/ruby_less/safe_class.rb
Instance Method Summary collapse
-
#safe_attribute(*attributes) ⇒ Object
Declare a safe method to access a list of attributes.
-
#safe_class? ⇒ Boolean
Return true if the class is safe (we can call safe_read on its instances).
-
#safe_context(methods_hash) ⇒ Object
A safe context is simply a safe method that can return nil in some situations.
- #safe_literal_class(hash) ⇒ Object
-
#safe_method(methods_hash) ⇒ Object
Declare safe methods.
-
#safe_method_for(klass, signature) ⇒ Object
Declare a safe method for a given class.
-
#safe_method_type(signature, receiver = nil) ⇒ Object
Return the type if the given signature corresponds to a safe method for the class.
-
#safe_methods ⇒ Object
Hash of all safe methods defined for the class.
-
#safe_property(*properties) ⇒ Object
Declare a safe method to access a list of properties.
Instance Method Details
#safe_attribute(*attributes) ⇒ Object
Declare a safe method to access a list of attributes. This method should only be used when the class is linked with a database table and provides proper introspection to detect types and the possibility of NULL values.
154 155 156 157 158 159 160 161 162 |
# File 'lib/ruby_less/safe_class.rb', line 154 def safe_attribute(*attributes) attributes.each do |att| if col = columns_hash[att.to_s] safe_method att.to_sym => SafeClass.safe_method_type_for_column(col) else puts "Warning: could not declare safe_attribute '#{att}' (No column with this name found in class #{self})" end end end |
#safe_class? ⇒ Boolean
Return true if the class is safe (we can call safe_read on its instances)
193 194 195 |
# File 'lib/ruby_less/safe_class.rb', line 193 def safe_class? true end |
#safe_context(methods_hash) ⇒ Object
A safe context is simply a safe method that can return nil in some situations. The rest of the syntax is the same as #safe_method. We call it a safe context because it enables syntaxes such as: if var = my_context(…) —> enter context.
141 142 143 144 145 |
# File 'lib/ruby_less/safe_class.rb', line 141 def safe_context(methods_hash) methods_hash[:defaults] ||= {} methods_hash[:defaults][:nil] = true safe_method(methods_hash) end |
#safe_literal_class(hash) ⇒ Object
147 148 149 |
# File 'lib/ruby_less/safe_class.rb', line 147 def safe_literal_class(hash) RubyLess::SafeClass.safe_literal_class(hash) end |
#safe_method(methods_hash) ⇒ Object
Declare safe methods. By providing
The methods hash has the following format:
signature => return type
or
signature =>
or
signature => lambda {|h| ... }
The lambda expression will be called with @helper as argument during compilation.
The signature can be either a single symbol or an array containing the method name and type arguments like:
[:strftime, Time, String]
If your method accepts variable arguments through a Hash, you should declare it with:
[:img, String, {:mode => String, :max_size => Number}]
Make sure your literal values are of the right type: :mode
and ‘mode’ are not the same here.
If the signature is :defaults, the options defined are used as defaults for the other elements defined in the same call.
The return type can be a string with the class name or a class.
Options are: :class the return type (class name) :nil set this to true if the method could return nil
134 135 136 |
# File 'lib/ruby_less/safe_class.rb', line 134 def safe_method(methods_hash) RubyLess::SafeClass.safe_method_for(self, methods_hash) end |
#safe_method_for(klass, signature) ⇒ Object
Declare a safe method for a given class
178 179 180 |
# File 'lib/ruby_less/safe_class.rb', line 178 def safe_method_for(klass, signature) SafeClass.safe_method_for(klass, signature) end |
#safe_method_type(signature, receiver = nil) ⇒ Object
Return the type if the given signature corresponds to a safe method for the class.
188 189 190 |
# File 'lib/ruby_less/safe_class.rb', line 188 def safe_method_type(signature, receiver = nil) SafeClass.safe_method_type_for(self, signature) end |
#safe_methods ⇒ Object
Hash of all safe methods defined for the class.
183 184 185 |
# File 'lib/ruby_less/safe_class.rb', line 183 def safe_methods SafeClass.safe_methods_for(self) end |
#safe_property(*properties) ⇒ Object
Declare a safe method to access a list of properties. This method should only be used in conjunction with the Property gem.
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ruby_less/safe_class.rb', line 166 def safe_property(*properties) columns = schema.columns properties.each do |att| if col = columns[att.to_s] safe_method att.to_sym => SafeClass.safe_method_type_for_column(col, true) else puts "Warning: could not declare safe_property '#{att}' (No property column with this name found in class #{self})" end end end |