Class: ActiveUrl::Base
- Inherits:
-
Object
- Object
- ActiveUrl::Base
- Defined in:
- lib/active_url/base.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .attr_accessible(*attribute_names) ⇒ Object
- .attribute(*attribute_names) ⇒ Object
- .find(id) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object
- #create ⇒ Object
-
#initialize(attributes = nil) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
- #to_param ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Base
Returns a new instance of Base.
20 21 22 23 |
# File 'lib/active_url/base.rb', line 20 def initialize(attributes = nil) attributes ||= {} self.attributes = attributes end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
18 19 20 |
# File 'lib/active_url/base.rb', line 18 def id @id end |
Class Method Details
.attr_accessible(*attribute_names) ⇒ Object
14 15 16 |
# File 'lib/active_url/base.rb', line 14 def self.attr_accessible(*attribute_names) self.accessible_attributes += attribute_names.map(&:to_sym) end |
.attribute(*attribute_names) ⇒ Object
9 10 11 12 |
# File 'lib/active_url/base.rb', line 9 def self.attribute(*attribute_names) = attribute_names. attribute_names.map(&:to_sym).each { |attribute_name| add_attribute(attribute_name, ) } end |
.find(id) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_url/base.rb', line 54 def self.find(id) raise RecordNotFound unless id.is_a?(String) && !id.blank? serialized = begin Crypto.decrypt(id) rescue Crypto::CipherError raise RecordNotFound end type, attributes = YAML.load(serialized) raise RecordNotFound unless type == self.to_s && attributes.is_a?(Hash) active_url = new attributes.each { |key, value| active_url.send "#{key}=", value } active_url.create active_url rescue RecordNotFound raise RecordNotFound.new("Couldn't find #{self.name} with id=#{id}") end |
Instance Method Details
#==(other) ⇒ Object
79 80 81 |
# File 'lib/active_url/base.rb', line 79 def ==(other) attributes == other.attributes && self.class == other.class end |
#attributes ⇒ Object
35 36 37 38 39 |
# File 'lib/active_url/base.rb', line 35 def attributes attribute_names.inject({}) do |hash, name| hash.merge(name => send(name)) end end |
#attributes=(attributes) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/active_url/base.rb', line 25 def attributes=(attributes) attributes.symbolize_keys.select do |key, value| self.class.accessible_attributes.include? key end.map do |key, value| [ "#{key}=", value ] end.each do |setter, value| send setter, value if respond_to? setter end end |
#create ⇒ Object
41 42 43 44 |
# File 'lib/active_url/base.rb', line 41 def create serialized = [ self.class.to_s, attributes ].to_yaml @id = Crypto.encrypt(serialized) end |
#new_record? ⇒ Boolean
75 76 77 |
# File 'lib/active_url/base.rb', line 75 def new_record? @id.nil? end |
#save ⇒ Object
46 47 48 |
# File 'lib/active_url/base.rb', line 46 def save !create.blank? end |
#save! ⇒ Object
50 51 52 |
# File 'lib/active_url/base.rb', line 50 def save! save end |
#to_param ⇒ Object
71 72 73 |
# File 'lib/active_url/base.rb', line 71 def to_param @id.to_s end |