Class: InactiveRecord::Base
- Inherits:
-
Object
- Object
- InactiveRecord::Base
- Includes:
- ActiveRecord::Validations
- Defined in:
- lib/inactive_record/base.rb
Class Method Summary collapse
- .human_name ⇒ Object
- .raise_not_implemented_error(*params) ⇒ Object (also: validates_uniqueness_of, create!, validate_on_create, validate_on_update, save_with_validation)
- .self_and_descendants_from_active_record ⇒ Object
- .self_and_descendents_from_active_record ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(attributes = {}) {|_self| ... } ⇒ Base
constructor
A new instance of Base.
- #method_missing(method_id, *args) ⇒ Object
- #new_record? ⇒ Boolean
-
#to_xml(options = {}) ⇒ Object
Instructions on how to build xml for an object.
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/inactive_record/base.rb', line 8 def initialize( attributes={} ) unless attributes.nil? # Fix any dates/times from rails date_select keys = attributes.keys date_keys = keys.grep(/(1i)/) time_keys = keys.grep(/(4i)/) date_keys.each do |date_key| key = date_key.to_s.gsub( /\(1i\)/, '' ) num = keys.grep( /#{key.to_s}/ ).size if num == 3 # Date attributes[key.to_sym] = Date.civil( attributes.delete( "#{key}(1i)".to_sym ).to_i, attributes.delete( "#{key}(2i)".to_sym ).to_i, attributes.delete( "#{key}(3i)".to_sym ).to_i ) elsif num == 5 #DateTime attributes[key.to_sym] = DateTime.civil( attributes.delete( "#{key}(1i)".to_sym ).to_i, attributes.delete( "#{key}(2i)".to_sym ).to_i, attributes.delete( "#{key}(3i)".to_sym ).to_i, attributes.delete( "#{key}(4i)".to_sym ).to_i, attributes.delete( "#{key}(5i)".to_sym ).to_i ) elsif num == 6 #DateTime attributes[key.to_sym] = DateTime.civil( attributes.delete( "#{key}(1i)".to_sym ).to_i, attributes.delete( "#{key}(2i)".to_sym ).to_i, attributes.delete( "#{key}(3i)".to_sym ).to_i, attributes.delete( "#{key}(4i)".to_sym ).to_i, attributes.delete( "#{key}(5i)".to_sym ).to_i, attributes.delete( "#{key}(6i)".to_sym ).to_i ) end end attributes.each do |key, value| self.instance_variable_set("@#{key}", value) end end yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/inactive_record/base.rb', line 46 def method_missing( method_id, *args ) if md = /_before_type_cast$/.match(method_id.to_s) attr_name = md.pre_match return self[attr_name] if self.respond_to?(attr_name) end super end |
Class Method Details
.human_name ⇒ Object
107 108 109 |
# File 'lib/inactive_record/base.rb', line 107 def self.human_name end |
.raise_not_implemented_error(*params) ⇒ Object Also known as: validates_uniqueness_of, create!, validate_on_create, validate_on_update, save_with_validation
129 130 131 |
# File 'lib/inactive_record/base.rb', line 129 def raise_not_implemented_error(*params) raise NotImplementedError end |
.self_and_descendants_from_active_record ⇒ Object
103 104 105 |
# File 'lib/inactive_record/base.rb', line 103 def self.self_and_descendants_from_active_record [self] end |
.self_and_descendents_from_active_record ⇒ Object
99 100 101 |
# File 'lib/inactive_record/base.rb', line 99 def self.self_and_descendents_from_active_record [self] end |
Instance Method Details
#[](key) ⇒ Object
42 43 44 |
# File 'lib/inactive_record/base.rb', line 42 def [](key) instance_variable_get("@#{key}") end |
#new_record? ⇒ Boolean
95 96 97 |
# File 'lib/inactive_record/base.rb', line 95 def new_record? true end |
#to_xml(options = {}) ⇒ Object
Instructions on how to build xml for an object.
options :only - A symbol or array of symbols of instance variable names to include in the xml. :except - A symbol or array of symbols of instance variable names to exclude in the xml. :skip_instruct - If true, output the document type, otherwise do not. :skip_types - (not yet) If true, do not output types of variables that are not strings. :include - (not yet) First level associations to include. :dasherize - If true, convert underscored variable names to dasherized variable names.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/inactive_record/base.rb', line 64 def to_xml( ={} ) [:indent] ||= 2 except = [:except] only = [:only] throw 'Both the :except and :only options cannot be used simultaneously.' if !except.nil? && !only.nil? except = Array.new << except.to_sym if except.is_a?( String ) except = Array.new << except if except.is_a?( Symbol ) only = Array.new << only.to_sym if only.is_a?( String ) only = Array.new << only if only.is_a?( Symbol ) dasherize = [:dasherize] dasherize = true unless !dasherize.nil? xml = [:builder] ||= Builder::XmlMarkup.new( :indent => [:indent] ) xml.instruct! unless [:skip_instruct] xml.tag!( self.class.to_s.underscore ) do self.instance_variables.each do |var| var_name = var.to_s.gsub( /@/, '' ) var_name = var_name.dasherize if dasherize if only xml.tag!( var_name, self.instance_variable_get( var ) ) if only.include?( var_name ) || only.include?( var_name.to_sym ) elsif except xml.tag!( var_name, self.instance_variable_get( var ) ) unless except.include?( var_name ) || except.include?( var_name.to_sym ) else xml.tag!( var_name, self.instance_variable_get( var ) ) end end end end |