Class: CouchDB::JSONObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/couchdb/json_object.rb

Overview

Public: The Ruby class that represents the JSON Object.

All properties (keys) in a JSONObject are strings.
(even strings are not the best hash key in Ruby)

Direct Known Subclasses

Document

Defined Under Namespace

Classes: Property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ JSONObject

Returns a new instance of JSONObject.



100
101
102
103
104
# File 'lib/couchdb/json_object.rb', line 100

def initialize(data = nil)
  replace data if data
  set_defaults
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



98
99
100
# File 'lib/couchdb/json_object.rb', line 98

def errors
  @errors
end

Class Method Details

.dynamic_structure!Object

Public: Make this object a dynamic struture object, which means

its properties are dynamic (just like a Hash).


71
72
73
# File 'lib/couchdb/json_object.rb', line 71

def self.dynamic_structure!
  @fixed_structure = false
end

.dynamic_structure?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/couchdb/json_object.rb', line 79

def self.dynamic_structure?
  not fixed_structure?
end

.fixed_structure!Object

Public: Make this object become a fixed struture object, which means

all properties of this object have to be declared (using the
`property` method) before being used.


65
66
67
# File 'lib/couchdb/json_object.rb', line 65

def self.fixed_structure!
  @fixed_structure = true
end

.fixed_structure?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/couchdb/json_object.rb', line 75

def self.fixed_structure?
  @fixed_structure
end

.lookup(property_name) ⇒ Object

Public: lookup a property definition by its name.



94
95
96
# File 'lib/couchdb/json_object.rb', line 94

def self.lookup(property_name)
  properties[property_name.to_s]
end

.propertiesObject

Public: Properties will inherit from the parent class.



84
85
86
# File 'lib/couchdb/json_object.rb', line 84

def self.properties
  @properties ||= {}.tap { |h| h.merge! superclass.properties if superclass < JSONObject }
end

.property(name, type = :string, options = {}, &blk) ⇒ Object



88
89
90
91
# File 'lib/couchdb/json_object.rb', line 88

def self.property(name, type = :string, options = {}, &blk)
  name = name.to_s
  properties[name] = Property.new(name, type, options, &blk)
end

Instance Method Details

#[]=(name, value) ⇒ Object



106
107
108
# File 'lib/couchdb/json_object.rb', line 106

def []=(name, value)
  super name.to_s, convert_value(name, value)
end

#inspectObject



156
157
158
# File 'lib/couchdb/json_object.rb', line 156

def inspect
  "#{self.class.name}#{super}"
end

#merge(data) ⇒ Object



122
123
124
# File 'lib/couchdb/json_object.rb', line 122

def merge(data)
  super convert_hash(data)
end

#merge!(data) ⇒ Object



118
119
120
# File 'lib/couchdb/json_object.rb', line 118

def merge!(data)
  super convert_hash(data)
end

#replace(data) ⇒ Object



126
127
128
# File 'lib/couchdb/json_object.rb', line 126

def replace(data)
  super convert_hash(data)
end

#store(name, value) ⇒ Object



110
111
112
# File 'lib/couchdb/json_object.rb', line 110

def store(name, value)
  super name.to_s, convert_value(name, value)
end

#update(data) ⇒ Object



114
115
116
# File 'lib/couchdb/json_object.rb', line 114

def update(data)
  super convert_hash(data)
end

#valid?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/couchdb/json_object.rb', line 130

def valid?
  validate!
  errors.empty?
end

#validate!Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/couchdb/json_object.rb', line 135

def validate!
  errors.clear

  properties.each { |k, property|
    v = self[k]

    if property.required? and v.nil?
      errors[k] = MissingProperty.new(k)
      next
    end

    if not property.valid_value?(v)
      errors[k] = InvalidValue.new(k, v)
    end

    if v.is_a?(JSONObject) and not v.valid?
      errors[k] = InvalidObject.new(v)
    end
  }
end