Module: CouchResource::Struct::ClassMethods

Defined in:
lib/couch_resource/struct.rb

Instance Method Summary collapse

Instance Method Details

#array(name, option = {}) ⇒ Object

define a array attribute, each of which elements is a primitive (one of string, number, array, boolean or hash) object options are :



58
59
60
61
62
63
# File 'lib/couch_resource/struct.rb', line 58

def array(name, option={})
  option[:is_a] = :array
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end

#boolean(name, option = {}) ⇒ Object

define a boolean attribute options are :

  • :validates - see CouchResource::Validations



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/couch_resource/struct.rb', line 41

def boolean(name, option={})
  option[:is_a] = :boolean
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  method = "    def \#{name}?\n      get_attribute(:\#{name})\n    end\n"
  class_eval(method, __FILE__, __LINE__)
  define_validations(name, option)
end

#collection(name, option = {}) ⇒ Object

define a collection attribute, each of which elements is an object specified by the :is_a option. options are :

  • :each - set the class to encode/decode each of Hash object (default is :hash, which means no encoding/decoding will be processed)

  • :validates - see CouchResource::Validations



71
72
73
74
75
76
77
78
79
# File 'lib/couch_resource/struct.rb', line 71

def collection(name, option={})
  option = {
    :each => :hash
  }.update(option)
  option[:is_a] = :collection
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end

#datetime(name, option = {}) ⇒ Object

define a datetime object (extension of string) options are :

  • :validates - see CouchResource::Validations



101
102
103
104
105
106
# File 'lib/couch_resource/struct.rb', line 101

def datetime(name, option={})
  option[:is_a] = :datetime
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end

#from_hash(hash) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/couch_resource/struct.rb', line 109

def from_hash(hash)
  hash ||= {}
  hash.symbolize_keys!
  instance = self.new
  (read_inheritable_attribute(:attribute_members) || {}).each do |name, option|
    instance.set_attribute(name, hash[name.to_sym])
  end
  instance
end

#number(name, option = {}) ⇒ Object

define a number attribute options are :

  • :validates - see CouchResource::Validations



29
30
31
32
33
34
# File 'lib/couch_resource/struct.rb', line 29

def number(name, option={})
  option[:is_a] = :number
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end

#object(name, option = {}) ⇒ Object

define a object attribute options are :

  • :is_a - set the class to encode/decode Hash object (default is :hash, which means no encoding/decoding will be processed)

  • :validates - see CouchResource::Validations



87
88
89
90
91
92
93
94
# File 'lib/couch_resource/struct.rb', line 87

def object(name, option={})
  unless option.has_key?(:is_a)
    option[:is_a] = :hash
  end
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end

#string(name, option = {}) ⇒ Object

define a string attribtue options are :

  • :validates - see CouchResource::Validations



17
18
19
20
21
22
# File 'lib/couch_resource/struct.rb', line 17

def string(name, option={})
  option[:is_a] = :string
  register_attribute_member(name, option)
  define_attribute_accessor(name, option)
  define_validations(name, option)
end