Class: Responsible::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer, data) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
44
45
46
# File 'lib/responsible.rb', line 38

def initialize(consumer, data)
  @consumer, @data = consumer, data

  undefined_properties = _properties_.keys - methods

  if undefined_properties.any?
    raise Responsible::PropertyNotImplemented, undefined_properties.join(", ")
  end
end

Instance Attribute Details

#consumerObject (readonly)

Returns the value of attribute consumer.



36
37
38
# File 'lib/responsible.rb', line 36

def consumer
  @consumer
end

#dataObject (readonly)

Returns the value of attribute data.



36
37
38
# File 'lib/responsible.rb', line 36

def data
  @data
end

Class Method Details

.doc(str = nil) ⇒ Object



9
10
11
12
13
# File 'lib/responsible.rb', line 9

def doc(str=nil)
  @doc ||= []
  @doc << str if str
  @doc
end

.propertiesObject



23
24
25
# File 'lib/responsible.rb', line 23

def properties
  @properties ||= {}
end

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



15
16
17
18
19
20
21
# File 'lib/responsible.rb', line 15

def property(name, options={})
  unknown_configuration_params = options.keys - [:delegate, :to, :restrict_to, :doc]
  raise(Responsible::UnknownConfigurationParameter, unknown_configuration_params.join(", ")) if unknown_configuration_params.any?

  properties[name.to_sym] = options
  delegate_method(name, options[:to]) if options[:delegate]
end

Instance Method Details

#as_json(opt = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/responsible.rb', line 48

def as_json(opt={})
  result = {}

  _properties_.each do |name, options|
    if consumer.can_see?(options[:restrict_to])
      result[name] = send(name)
    end
  end

  result
end