Class: StrongResources::StrongResource
- Inherits:
-
Object
- Object
- StrongResources::StrongResource
- Defined in:
- lib/strong_resources/strong_resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#customized_actions ⇒ Object
readonly
Returns the value of attribute customized_actions.
-
#destroy ⇒ Object
Returns the value of attribute destroy.
-
#disassociate ⇒ Object
Returns the value of attribute disassociate.
-
#except ⇒ Object
Returns the value of attribute except.
-
#jsonapi_type(type = nil) ⇒ Object
readonly
Returns the value of attribute jsonapi_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only ⇒ Object
Returns the value of attribute only.
-
#relation_type ⇒ Object
Returns the value of attribute relation_type.
-
#relations ⇒ Object
Returns the value of attribute relations.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute(name, type, opts = {}) ⇒ Object
- #belongs_to(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) ⇒ Object
- #destroy? ⇒ Boolean
- #disassociate? ⇒ Boolean
- #has_many(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) ⇒ Object
- #has_many? ⇒ Boolean
- #has_one(*args, &blk) ⇒ Object
-
#initialize(name) ⇒ StrongResource
constructor
A new instance of StrongResource.
- #on(action_name, &blk) ⇒ Object
- #permits(controller) ⇒ Object
- #remove_attribute(name) ⇒ Object
- #remove_relationship(name) ⇒ Object
Constructor Details
#initialize(name) ⇒ StrongResource
Returns a new instance of StrongResource.
20 21 22 23 24 25 26 |
# File 'lib/strong_resources/strong_resource.rb', line 20 def initialize(name) @name = name @jsonapi_type = name.to_s.pluralize @customized_actions = {} self.attributes = {} self.relations = {} end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def attributes @attributes end |
#customized_actions ⇒ Object (readonly)
Returns the value of attribute customized_actions.
10 11 12 |
# File 'lib/strong_resources/strong_resource.rb', line 10 def customized_actions @customized_actions end |
#destroy ⇒ Object
Returns the value of attribute destroy.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def destroy @destroy end |
#disassociate ⇒ Object
Returns the value of attribute disassociate.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def disassociate @disassociate end |
#except ⇒ Object
Returns the value of attribute except.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def except @except end |
#jsonapi_type(type = nil) ⇒ Object (readonly)
Returns the value of attribute jsonapi_type.
10 11 12 |
# File 'lib/strong_resources/strong_resource.rb', line 10 def jsonapi_type @jsonapi_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/strong_resources/strong_resource.rb', line 10 def name @name end |
#only ⇒ Object
Returns the value of attribute only.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def only @only end |
#relation_type ⇒ Object
Returns the value of attribute relation_type.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def relation_type @relation_type end |
#relations ⇒ Object
Returns the value of attribute relations.
3 4 5 |
# File 'lib/strong_resources/strong_resource.rb', line 3 def relations @relations end |
Class Method Details
.from(name, opts = {}, &blk) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/strong_resources/strong_resource.rb', line 12 def self.from(name, opts = {}, &blk) config = StrongResources.find(name) resource = new(name) resource.instance_eval(&config[:base]) resource.instance_eval(&blk) if blk resource end |
Instance Method Details
#attribute(name, type, opts = {}) ⇒ Object
44 45 46 |
# File 'lib/strong_resources/strong_resource.rb', line 44 def attribute(name, type, opts = {}) self.attributes[name] = { type: type, if: opts[:if] } end |
#belongs_to(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/strong_resources/strong_resource.rb', line 78 def belongs_to(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) resource_name = resource || name = self.class.from(resource_name, &blk) add_relation(name, , only, except, disassociate, destroy) end |
#destroy? ⇒ Boolean
40 41 42 |
# File 'lib/strong_resources/strong_resource.rb', line 40 def destroy? !!@destroy end |
#disassociate? ⇒ Boolean
36 37 38 |
# File 'lib/strong_resources/strong_resource.rb', line 36 def disassociate? !!@disassociate end |
#has_many(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/strong_resources/strong_resource.rb', line 64 def has_many(name, resource: nil, only: nil, except: nil, disassociate: false, destroy: false, &blk) resource_name = resource || name.to_s.singularize.to_sym = self.class.from(resource_name) .instance_eval(&blk) if block_given? .relation_type = :has_many add_relation(name, , only, except, disassociate, destroy) end |
#has_many? ⇒ Boolean
60 61 62 |
# File 'lib/strong_resources/strong_resource.rb', line 60 def has_many? relation_type == :has_many end |
#has_one(*args, &blk) ⇒ Object
90 91 92 |
# File 'lib/strong_resources/strong_resource.rb', line 90 def has_one(*args, &blk) belongs_to(*args, &blk) end |
#on(action_name, &blk) ⇒ Object
56 57 58 |
# File 'lib/strong_resources/strong_resource.rb', line 56 def on(action_name, &blk) self.customized_actions[action_name] = blk end |
#permits(controller) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/strong_resources/strong_resource.rb', line 94 def permits(controller) base_permits(self, controller).tap do |permits| permits[:relationships] ||= {} self.relations.each_pair do |relation_name, opts| = opts[:resource] = (, controller) permits[:relationships][relation_name] = permits end end end |
#remove_attribute(name) ⇒ Object
48 49 50 |
# File 'lib/strong_resources/strong_resource.rb', line 48 def remove_attribute(name) self.attributes.delete(name) end |
#remove_relationship(name) ⇒ Object
52 53 54 |
# File 'lib/strong_resources/strong_resource.rb', line 52 def remove_relationship(name) self.relations.delete(name) end |