Class: Axel::Payload::Base

Inherits:
Object
  • Object
show all
Defined in:
app/models/axel/payload/base.rb

Direct Known Subclasses

Errors, Metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



46
47
48
49
50
# File 'app/models/axel/payload/base.rb', line 46

def initialize(params = {})
  apply_defaults
  params = params.is_a?(Hash) ? params : {}
  @attributes.merge! params
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'app/models/axel/payload/base.rb', line 4

def attributes
  @attributes
end

Class Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/axel/payload/base.rb', line 13

def self.attribute(name, options = {})
  attribute_defaults[name.to_s.to_sym] = options[:default]
  visibility_read = options.has_key?(:read) ? options[:read] : true
  visibility_write = options.has_key?(:write) ? options[:write] : true
  if visibility_read
    define_method name do
      @attributes[name]
    end
    if %w[public private protected].include? visibility_read.to_s
      send visibility_read, name
    end
  end

  if visibility_write
    define_method "#{name}=" do |new_value|
      @attributes[name] = new_value
    end
    if %w[public private protected].include? visibility_write.to_s
      send visibility_write, name
    end
  end
end

.attribute_defaultsObject



36
37
38
# File 'app/models/axel/payload/base.rb', line 36

def self.attribute_defaults
  @attribute_defaults ||= {}
end

.root_node(name = nil) ⇒ Object



8
9
10
11
# File 'app/models/axel/payload/base.rb', line 8

def self.root_node(name = nil)
  @root_node = name if name
  @root_node
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
# File 'app/models/axel/payload/base.rb', line 56

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

#[]=(key, value) ⇒ Object



52
53
54
# File 'app/models/axel/payload/base.rb', line 52

def []=(key, value)
  @attributes[key] = decode(value)
end

#displayObject



60
61
62
# File 'app/models/axel/payload/base.rb', line 60

def display
  drop? ? {} : @attributes
end

#display?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/axel/payload/base.rb', line 64

def display?
  true
end

#drop!Object



72
73
74
# File 'app/models/axel/payload/base.rb', line 72

def drop!
  @drop = true
end

#drop?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/axel/payload/base.rb', line 68

def drop?
  !!@drop
end

#reset!Object



40
41
42
43
44
# File 'app/models/axel/payload/base.rb', line 40

def reset!
  @attributes = {}
  apply_defaults
  self
end

#to_jsonObject



76
77
78
# File 'app/models/axel/payload/base.rb', line 76

def to_json
  display? ? display.to_json : ""
end

#to_xmlObject



80
81
82
# File 'app/models/axel/payload/base.rb', line 80

def to_xml
  display? ? display.to_xml(dasherize: false, skip_instruct: true, root: self.class.root_node) : ""
end