Class: TcUser
- Inherits:
-
Object
- Object
- TcUser
- Defined in:
- lib/models/rufus_tokyo_user.rb
Class Method Summary collapse
- .delete(pk) ⇒ Object
- .get(key) ⇒ Object
- .query(&block) ⇒ Object
- .set(attributes) ⇒ Object
- .set!(attributes) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
saves to database and returns self.
- #admin? ⇒ Boolean
- #id ⇒ Object
-
#initialize(attributes) ⇒ TcUser
constructor
attribute method? if I’m gonna write all this, I might as well create a tinyyyy orm, that’s more just like a way to define custom attributes for cabinets something worth noting though is that even datamapper defines custom attributes by allowing the developer to override setter methods.
-
#method_missing(meth, *args) ⇒ Object
from hash extension for making hashes like javascript objects.
- #site_admin? ⇒ Boolean
- #update(attributes) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ TcUser
attribute method? if I’m gonna write all this, I might as well create a tinyyyy orm, that’s more just like a way to define custom attributes for cabinets something worth noting though is that even datamapper defines custom attributes by allowing the developer to override setter methods. and it just calls all the setter methods defined in the model. the only trouble with this route is it assumes a predefined schema. and thus it knows what setter methods to call. I would write a class method that allows you to declare attributes like attribute :salt, with an optional block (which gets passed a hash of attributes) if a block isn’t defined, it looks in the class for a salt=(attributes) function, calls it and marges the result into the hash going into the database, like ‘attributes.merge=> result’ so my ‘set’ method or whatever I choose to call it, has to somehow look through each declared attribute, call the method associated with it, and merge the result into the hash going into the database.
but what if I don’t want an attribute passed in to be stored into the database? What if I just want to create a virtual attribute, for declaring other attributes? I might create a class variable that I store all the attributes in, and the I can get to it from any setter, and then after I’ve called all the setters, I store that class variable into the database. or, I do all of this on the instance level, and have a save method.
32 33 34 |
# File 'lib/models/rufus_tokyo_user.rb', line 32 def initialize(attributes) @attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
from hash extension for making hashes like javascript objects
145 146 147 148 149 150 151 152 153 |
# File 'lib/models/rufus_tokyo_user.rb', line 145 def method_missing(meth,*args) if /=$/=~(meth=meth.id2name) then self[meth[0...-1]] = (args.length<2 ? args[0] : args) elsif @attributes[meth] @attributes[meth] else false end end |
Class Method Details
.delete(pk) ⇒ Object
106 107 108 109 110 |
# File 'lib/models/rufus_tokyo_user.rb', line 106 def self.delete(pk) connection = TcUserTable.new connection.delete(pk) connection.close end |
.get(key) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/models/rufus_tokyo_user.rb', line 44 def self.get(key) connection = TcUserTable.new result = connection[key] connection.close if result self.new(result.merge({:pk => key})) else false end end |
.query(&block) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/models/rufus_tokyo_user.rb', line 36 def self.query(&block) connection = TcUserTable.new result_set = connection.query(&block) output = result_set.collect { |result_hash| TcUser.new(result_hash) } connection.close output end |
.set(attributes) ⇒ Object
55 56 57 58 59 60 61 62 63 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 |
# File 'lib/models/rufus_tokyo_user.rb', line 55 def self.set(attributes) #this way of validating is real crap, replace it with Validator maybe #and maybe replace all this hash merging with setters for the various attributes that update @attributes, and then I can call save to store to the database #or maybe just write a little method that makes hash merger look a little cleaner pk = attributes.delete(:pk) if attributes[:pk] email_regexp = /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i if attributes['email'] =~ email_regexp if attributes['password'] == attributes.delete('password_confirmation') && attributes['password'] != nil password = attributes.delete('password') attributes.merge!({'salt' => User.random_string(10)}) if !attributes['salt'] attributes.merge!('hashed_password' => User.encrypt(password, attributes['salt'])) = attributes['permission_level'] ? attributes['permission_level'] : '1' attributes.merge!('permission_level' => ) unless attributes['created_at'] attributes.merge!('created_at' => Time.now.to_s) attributes.merge!('created_at_i' => Time.now.to_i.to_s) end end existing_user = TcUser.query do |q| q.add 'email', :streq, attributes['email'] end[0] if existing_user && existing_user['pk'] != attributes['pk'] return false else connection = TcUserTable.new pk ||= connection.genuid.to_s #site admin if their first attributes.merge!({'permission_level' => '-2'}) if pk == '1' result = connection[pk] = attributes #might not need this in newer version of rufus result.merge!({:pk => pk}) connection.close self.new(result) end else false end end |
.set!(attributes) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/models/rufus_tokyo_user.rb', line 97 def self.set!(attributes) connection = TcUserTable.new pk = connection.genuid.to_s result = connection[pk] = attributes result.merge!({:pk => pk}) connection.close self.new(result) end |
Instance Method Details
#[](key) ⇒ Object
117 118 119 |
# File 'lib/models/rufus_tokyo_user.rb', line 117 def [](key) @attributes[key] end |
#[]=(key, value) ⇒ Object
saves to database and returns self
122 123 124 125 126 127 128 129 |
# File 'lib/models/rufus_tokyo_user.rb', line 122 def []=(key, value) @attributes[key] = value #change so that it sets the attributes and then you call save to save to the database? connection = TcUserTable.new connection[@attributes[:pk]] = @attributes.merge!({key => value}) connection.close self end |
#admin? ⇒ Boolean
135 136 137 138 |
# File 'lib/models/rufus_tokyo_user.rb', line 135 def admin? #-2 is the site admin @attributes['permission_level'] == '-1' || @attributes['permission_level'] == '-2' end |
#id ⇒ Object
131 132 133 |
# File 'lib/models/rufus_tokyo_user.rb', line 131 def id @attributes[:pk] end |
#site_admin? ⇒ Boolean
140 141 142 |
# File 'lib/models/rufus_tokyo_user.rb', line 140 def site_admin? @attributes['permission_level'] == '-2' end |