Class: RegApi2::Entity::EntityBase

Inherits:
Object
  • Object
show all
Defined in:
lib/reg_api2/entity/entity_base.rb

Overview

Base entity class.

Direct Known Subclasses

User

Constant Summary

SKIPPED_MEMBERS =

Skipped property names.

[
  "taguri" # from YAML mixin
].freeze

Instance Method Summary (collapse)

Constructor Details

- (EntityBase) initialize(opts = {})

Initializes the instance. opts values are assigned to properties if exist.

Parameters:

  • opts (Hash) (defaults to: {})


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

- (Array(String)) property_names

Gets instance property names

Returns:

  • (Array(String))


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

- (Hash) to_hash

All r/w properties interpreted as symbol hash.

Returns:

  • (Hash)

    properties as 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

- (String) to_json

Returns JSON

Returns:

  • (String)

    JSON

See Also:



42
43
44
# File 'lib/reg_api2/entity/entity_base.rb', line 42

def to_json
  Yajl::Encoder.encode to_hash
end