Class: Fakecrm::ResourceView
- Inherits:
-
Object
- Object
- Fakecrm::ResourceView
- Defined in:
- lib/fakecrm/resource/views/resource_view.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #available?(property) ⇒ Boolean
- #exclude?(property) ⇒ Boolean
- #get_additional_methods(resource) ⇒ Object
- #get_custom_attributes(resource) ⇒ Object
- #get_resource_properties(resource) ⇒ Object
- #include?(property) ⇒ Boolean
-
#initialize(resource, options = {}) ⇒ ResourceView
constructor
A new instance of ResourceView.
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(resource, options = {}) ⇒ ResourceView
Returns a new instance of ResourceView.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 16 def initialize(resource, ={}) self.resource = resource self.custom_attributes = get_custom_attributes(self.resource) self.properties = get_resource_properties(self.resource) self.include = .fetch(:include, :all) self.exclude = .fetch(:exclude, []) self.additional = .fetch(:methods, get_additional_methods(self.resource)) end |
Class Method Details
.decorate(source, options = {}) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 7 def decorate(source, ={}) if source.class.include?(::Enumerable) source.map {|item| self.new(item, )} else self.new(source, ) end end |
Instance Method Details
#as_json(*args) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 32 def as_json(*args) result = {} self.properties.each do |property| if available?(property) && include?(property) result[property] = resource.send(property) end end self.additional.each do |method| result[method] = resource.send(method) end return result end |
#available?(property) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 48 def available?(property) if property =~ /^custom/ self.custom_attributes.include?(property) else true end end |
#exclude?(property) ⇒ Boolean
60 61 62 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 60 def exclude?(property) !include?(property) end |
#get_additional_methods(resource) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 64 def get_additional_methods(resource) if resource.class.respond_to? :virtual_properties resource.class.virtual_properties || [] else [] end end |
#get_custom_attributes(resource) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 76 def get_custom_attributes(resource) if resource.respond_to?(:one_of_a_kind?) && resource.one_of_a_kind? CustomType.get!(resource.kind).custom_attributes.map(&:name).map(&:to_sym) else get_resource_properties(resource).select {|property| property =~ /^custom/ } end end |
#get_resource_properties(resource) ⇒ Object
72 73 74 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 72 def get_resource_properties(resource) resource.class.properties.map(&:name) end |
#include?(property) ⇒ Boolean
56 57 58 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 56 def include?(property) !self.exclude.include?(property) && (self.include == :all || self.include.include?(property)) end |
#to_json(*args) ⇒ Object
28 29 30 |
# File 'lib/fakecrm/resource/views/resource_view.rb', line 28 def to_json(*args) return as_json.to_json end |