Class: InactiveRecord::Base
- Inherits:
-
Object
- Object
- InactiveRecord::Base
- Includes:
- ActiveRecord::Validations
- Defined in:
- lib/inactive_record/base.rb
Class Method Summary collapse
-
.human_attribute_name(attribute_key_name) ⇒ Object
Transforms attribute key names into a more humane format, such as “First name” instead of “first_name”.
- .human_name(options = {}) ⇒ 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.
7 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 |
# File 'lib/inactive_record/base.rb', line 7 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
45 46 47 48 49 50 51 |
# File 'lib/inactive_record/base.rb', line 45 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_attribute_name(attribute_key_name) ⇒ Object
Transforms attribute key names into a more humane format, such as “First name” instead of “first_name”. Example:
Person.human_attribute_name("first_name") # => "First name"
146 147 148 |
# File 'lib/inactive_record/base.rb', line 146 def self.human_attribute_name(attribute_key_name) #:nodoc: I18n.translate( "inactive_record.attributes.#{self.name.underscore.gsub( /\//, '.' )}.#{attribute_key_name.to_s}") end |
.human_name(options = {}) ⇒ Object
140 141 142 |
# File 'lib/inactive_record/base.rb', line 140 def self.human_name( ={} ) Base.human_attribute_name(@name) end |
.raise_not_implemented_error(*params) ⇒ Object Also known as: validates_uniqueness_of, create!, validate_on_create, validate_on_update, save_with_validation
164 165 166 |
# File 'lib/inactive_record/base.rb', line 164 def raise_not_implemented_error(*params) raise NotImplementedError end |
.self_and_descendants_from_active_record ⇒ Object
136 137 138 |
# File 'lib/inactive_record/base.rb', line 136 def self.self_and_descendants_from_active_record [self] end |
.self_and_descendents_from_active_record ⇒ Object
132 133 134 |
# File 'lib/inactive_record/base.rb', line 132 def self.self_and_descendents_from_active_record [self] end |
Instance Method Details
#[](key) ⇒ Object
41 42 43 |
# File 'lib/inactive_record/base.rb', line 41 def []( key ) instance_variable_get("@#{key}") end |
#new_record? ⇒ Boolean
128 129 130 |
# File 'lib/inactive_record/base.rb', line 128 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. :methods - A symbol or array of symbols of methods to evaluate and include 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/inactive_record/base.rb', line 64 def to_xml( ={} ) [:indent] ||= 2 except = [:except] methods = [:methods] only = [:only] throw 'Both the :except and :only options cannot be used simultaneously.' if !except.nil? && !only.nil? only = only.map { |attribute| attribute.to_sym } if only.is_a?( Array ) only = Array.new << only.to_sym if only.is_a?( String ) only = Array.new << only if only.is_a?( Symbol ) except = except.map { |attribute| attribute.to_sym } if except.is_a?( Array ) except = Array.new << except.to_sym if except.is_a?( String ) except = Array.new << except if except.is_a?( Symbol ) methods = methods.map { |attribute| attribute.to_sym } if methods.is_a?( Array ) methods = Array.new << methods.to_sym if methods.is_a?( String ) methods = Array.new << methods if methods.is_a?( Symbol ) dasherize = [:dasherize] dasherize = true unless !dasherize.nil? attrs = self.instance_variables.map { |var| var.to_s.gsub( /@/, '' ).to_sym } to_serialize = [] to_serialize += methods unless methods.nil? if only to_serialize += (attrs.select { |var| only.include?( var ) }) elsif except to_serialize += (attrs - except) else to_serialize += attrs unless attrs.nil? end xml = [:builder] ||= Builder::XmlMarkup.new( :indent => [:indent] ) xml.instruct! unless [:skip_instruct] el_tag_name = dasherize ? self.class.to_s.underscore.dasherize : self.class.to_s.underscore xml.tag!( el_tag_name ) do to_serialize.each do |attribute| var_name = dasherize ? attribute.to_s.dasherize : attribute attr_val = self.send( attribute ) if self.respond_to?( attribute ) if attr_val.is_a?( Array ) xml << attr_val.to_xml( :skip_instruct => true ) else if attr_val.nil? is_nil = 'true' elsif attr_val.is_a?( TrueClass ) || attr_val.is_a?( FalseClass ) type = 'boolean' elsif attr_val.is_a?( Integer ) || attr_val.is_a?( Fixnum ) type = 'integer' elsif attr_val.is_a?( Float ) type = 'float' elsif attr_val.is_a?( DateTime ) || attr_val.is_a?( Date ) || attr_val.is_a?( Time ) type = 'datetime' end if is_nil xml.tag!( var_name, attr_val, :nil => is_nil ) #unless attr_val.nil? elsif type xml.tag!( var_name, attr_val, :type => type ) #unless attr_val.nil? else xml.tag!( var_name, attr_val ) #unless attr_val.nil? end end end end end |