Class: NormalizrRuby::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/normalizr_ruby/schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context, props) ⇒ Schema

Returns a new instance of Schema.



25
26
27
28
29
# File 'lib/normalizr_ruby/schema.rb', line 25

def initialize(object, context, props)
  @object = object
  @context = context
  @props = props
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/normalizr_ruby/schema.rb', line 3

def context
  @context
end

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/normalizr_ruby/schema.rb', line 3

def object
  @object
end

#propsObject (readonly)

Returns the value of attribute props.



3
4
5
# File 'lib/normalizr_ruby/schema.rb', line 3

def props
  @props
end

Class Method Details

.association(key, options = {}) ⇒ Object



21
22
23
# File 'lib/normalizr_ruby/schema.rb', line 21

def self.association(key, options = {})
  _associations[key] = options;
end

.attribute(key, options = {}) ⇒ Object



17
18
19
# File 'lib/normalizr_ruby/schema.rb', line 17

def self.attribute(key, options = {})
  _attributes[key] = options;
end

.inherited(child) ⇒ Object



11
12
13
14
15
# File 'lib/normalizr_ruby/schema.rb', line 11

def self.inherited(child)
  super
  child._attributes = _attributes.dup
  child._associations = _associations.dup
end

Instance Method Details

#association_resource(association_key) ⇒ Object



59
60
61
# File 'lib/normalizr_ruby/schema.rb', line 59

def association_resource(association_key)
  object.send(association_key)
end

#associationsObject



40
41
42
43
44
45
46
47
# File 'lib/normalizr_ruby/schema.rb', line 40

def associations
  hash = {}
  self.class._associations.each do |key, options|
    next if options[:if] && !send(options[:if])
    hash[key] = options.except(:if)
  end
  hash
end

#attributesObject



31
32
33
34
35
36
37
38
# File 'lib/normalizr_ruby/schema.rb', line 31

def attributes
  hash = {}
  self.class._attributes.each do |key, options|
    next if options[:if] && !send(options[:if])
    hash[key] = respond_to?(key) ? send(key) : object.send(key)
  end
  hash
end

#entity_keyObject



49
50
51
52
53
54
55
56
57
# File 'lib/normalizr_ruby/schema.rb', line 49

def entity_key
  klass = object.class
  if klass.respond_to?(:base_class)
    klass = klass.base_class
  end
  klass.name
       .pluralize
       .to_sym
end