Module: SimplyStored::SimpleDB::Attributes::ClassMethods

Defined in:
lib/simply_stored/simpledb/attributes.rb

Instance Method Summary collapse

Instance Method Details

#add_attribute_definition_to_attribute_list(attr) ⇒ Object



100
101
102
103
# File 'lib/simply_stored/simpledb/attributes.rb', line 100

def add_attribute_definition_to_attribute_list(attr)
  @_defined_attributes ||= []
  @_defined_attributes << attr.to_s
end

#add_attribute_definition_to_partition_list(attr) ⇒ Object



105
106
107
108
# File 'lib/simply_stored/simpledb/attributes.rb', line 105

def add_attribute_definition_to_partition_list(attr)
  @_partitioned_attributes ||= []
  @_partitioned_attributes << attr.to_s
end

#attr_accessible(*args) ⇒ Object



115
116
117
118
# File 'lib/simply_stored/simpledb/attributes.rb', line 115

def attr_accessible(*args)
  @_accessible_attributes ||= []
  @_accessible_attributes += args.to_a
end

#attr_protected(*args) ⇒ Object



110
111
112
113
# File 'lib/simply_stored/simpledb/attributes.rb', line 110

def attr_protected(*args)
  @_protected_attributes ||= []
  @_protected_attributes += args.to_a
end

#simpledb_array(*attributes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simply_stored/simpledb/attributes.rb', line 27

def simpledb_array(*attributes)
  attributes.each do |attr|
    # getter
    define_method attr.to_s do
      self[attr.to_s] = [] unless self[attr.to_s]
      self[attr.to_s]
    end
      
    # setter
    define_method "#{attr}=" do |val|
      self[attr.to_s] = [val].flatten
    end
      
    add_attribute_definition_to_attribute_list(attr)
  end
end

#simpledb_integer(*attributes) ⇒ Object



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
# File 'lib/simply_stored/simpledb/attributes.rb', line 72

def simpledb_integer(*attributes)
  attributes.each do |attr|
    # getter
    define_method attr.to_s do
      if self[attr.to_s]
        cached = instance_variable_get("@#{attr}")
        if cached
          return cached
        else
          val = self[attr.to_s].first.sub(/\A0+/, '').to_i
          instance_variable_set("@#{attr}", val)
          return val
        end
      else
        nil
      end
    end
      
    # setter
    define_method "#{attr}=" do |val|
      instance_variable_set("@#{attr}", val)
      self[attr.to_s] = sprintf("%016d", val.to_i)
    end
      
    add_attribute_definition_to_attribute_list(attr)
  end
end

#simpledb_string(*attributes) ⇒ Object

generates an accessor for a simple attribute



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simply_stored/simpledb/attributes.rb', line 10

def simpledb_string(*attributes)
  attributes.each do |attr|
    # getter
    define_method attr.to_s do
      self[attr.to_s].try(:first) || @_partitioned_attributes.try(:[], attr.to_s)
    end
      
    # setter
    define_method "#{attr}=" do |val|
      self[attr.to_s] = val
    end
      
    add_attribute_definition_to_attribute_list(attr)
    add_attribute_definition_to_partition_list(attr)
  end
end

#simpledb_timestamp(*attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/simply_stored/simpledb/attributes.rb', line 44

def simpledb_timestamp(*attributes)
  attributes.each do |attr|
    # getter
    define_method attr.to_s do
      if self[attr.to_s]
        cached = instance_variable_get("@#{attr}")
        if cached
          return cached
        else
          val = Time.parse(self[attr.to_s].first + "+0000").utc # UTC
          instance_variable_set("@#{attr}", val)
          return val
        end
      else
        nil
      end
    end
      
    # setter
    define_method "#{attr}=" do |val|
      instance_variable_set("@#{attr}", val)
      self[attr.to_s] = val.strftime("%Y%m%d%H%M%S")
    end
      
    add_attribute_definition_to_attribute_list(attr)
  end
end