Class: RegApi2::Entity::EntityBase
- Inherits:
-
Object
- Object
- RegApi2::Entity::EntityBase
- Defined in:
- lib/reg_api2/entity/entity_base.rb
Overview
Base entity class.
Direct Known Subclasses
Constant Summary collapse
- SKIPPED_MEMBERS =
Skipped property names.
[ "taguri" # from YAML mixin ].freeze
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ EntityBase
constructor
Initializes the instance.
-
#property_names ⇒ Array(String)
Gets instance property names.
-
#to_hash ⇒ Hash
All r/w properties interpreted as symbol hash.
-
#to_json ⇒ String
Returns JSON.
Constructor Details
#initialize(opts = {}) ⇒ EntityBase
Initializes the instance. opts values are assigned to properties if exist.
49 50 51 52 53 54 55 |
# File 'lib/reg_api2/entity/entity_base.rb', line 49 def initialize opts = {} methods = self.class.public_instance_methods(false).map(&:to_s) opts.keys.each do |key| next unless methods.detect { |m| m == "#{key}=" } send("#{key}=", opts[key]) end end |
Instance Method Details
#property_names ⇒ Array(String)
Gets instance property names
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/reg_api2/entity/entity_base.rb', line 17 def property_names methods = self.class.public_instance_methods(false).map(&:to_s) methods.select do |n| true && !SKIPPED_MEMBERS.detect { |n3| n3 == n } && n =~ /^[^=\?!]+$/ && methods.detect { |n2| "#{n}=" == n2 } && true end end |
#to_hash ⇒ Hash
All r/w properties interpreted as symbol hash.
30 31 32 33 34 35 36 37 |
# File 'lib/reg_api2/entity/entity_base.rb', line 30 def to_hash h = {} property_names.each do |n| v = self.send n.to_sym h[n.to_sym] = v unless v.nil? end h end |
#to_json ⇒ String
Returns JSON
42 43 44 |
# File 'lib/reg_api2/entity/entity_base.rb', line 42 def to_json Yajl::Encoder.encode to_hash end |