Class: TcUser

Inherits:
Object
  • Object
show all
Defined in:
lib/models/rufus_tokyo_user.rb

Class Method Summary collapse

Instance Method Summary collapse

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



124
125
126
127
128
129
130
131
132
# File 'lib/models/rufus_tokyo_user.rb', line 124

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



85
86
87
88
89
# File 'lib/models/rufus_tokyo_user.rb', line 85

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)
  result_set.collect! { |result_hash| TcUser.new(result_hash) }
  connection.close
  result_set
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
# 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['password'] == attributes.delete('password_confirmation') && attributes['email'] =~ email_regexp)
    password = attributes.delete('password')
    attributes.merge!({'salt' => User.random_string(10)}) if !attributes['salt']
    attributes.merge!('hashed_password' => User.encrypt(password, attributes['salt']))
    permission_level = attributes['permission_level'] ? attributes['permission_level'] : '1'
    attributes.merge!('permission_level' => permission_level)
    attributes.merge!('created_at' => Time.now.to_s)
    attributes.merge!('created_at_i' => Time.now.to_i.to_s)

    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)
  else
    false
  end
end

Instance Method Details

#[](key) ⇒ Object



96
97
98
# File 'lib/models/rufus_tokyo_user.rb', line 96

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object

saves to database and returns self



101
102
103
104
105
106
107
108
# File 'lib/models/rufus_tokyo_user.rb', line 101

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

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/models/rufus_tokyo_user.rb', line 114

def admin?
  #-2 is the site admin
  @attributes['permission_level'] == '-1' || @attributes['permission_level'] == '-2'
end

#idObject



110
111
112
# File 'lib/models/rufus_tokyo_user.rb', line 110

def id
  @attributes[:pk]
end

#site_admin?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/models/rufus_tokyo_user.rb', line 119

def site_admin?
  @attributes['permission_level'] == '-2'
end

#update(attributes) ⇒ Object



91
92
93
94
# File 'lib/models/rufus_tokyo_user.rb', line 91

def update(attributes)
  new_attributes = @attributes.merge(attributes)
  TcUser.set(new_attributes)
end