Class: Axel::Payload::Base
- Inherits:
-
Object
- Object
- Axel::Payload::Base
- Defined in:
- app/models/axel/payload/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .attribute(name, options = {}) ⇒ Object
- .attribute_defaults ⇒ Object
- .root_node(name = nil) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #display ⇒ Object
- #display? ⇒ Boolean
- #drop! ⇒ Object
- #drop? ⇒ Boolean
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #reset! ⇒ Object
- #to_json ⇒ Object
- #to_xml ⇒ Object
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
#attributes ⇒ Object (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, = {}) attribute_defaults[name.to_s.to_sym] = [:default] visibility_read = .has_key?(:read) ? [:read] : true visibility_write = .has_key?(:write) ? [: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_defaults ⇒ Object
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 |
#display ⇒ Object
60 61 62 |
# File 'app/models/axel/payload/base.rb', line 60 def display drop? ? {} : @attributes end |
#display? ⇒ 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
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_json ⇒ Object
76 77 78 |
# File 'app/models/axel/payload/base.rb', line 76 def to_json display? ? display.to_json : "" end |
#to_xml ⇒ Object
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 |