Class: Supplejack::UserStoryRelation
- Inherits:
-
Object
- Object
- Supplejack::UserStoryRelation
- Includes:
- Request
- Defined in:
- lib/supplejack/user_story_relation.rb
Instance Attribute Summary collapse
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Supplejack::Story
Initializes a new Story object and links it to the current User.
-
#create(attributes = {}) ⇒ Supplejack::Story
Build and persist a Story object with the provided attributes.
-
#fetch(force: false) ⇒ Object
(also: #all)
Returns an array of Story objects and memoizes the array.
-
#find(story_id) ⇒ Supplejack::Story
Finds a Story object with the provided ID that belongs to the current User.
-
#initialize(user) ⇒ UserStoryRelation
constructor
A new instance of UserStoryRelation.
-
#method_missing(method, *args, &block) ⇒ Object
Any method missing on this class is delegated to the Stories objects array so that the developer can easily execute any Array method on the UserSttoryRelation.
-
#order(attribute) ⇒ Array
Return an array of Story objects ordered in ascending order by the specified attribute.
- #to_json(include_contents: true) ⇒ Object
Methods included from Request
#delete, #get, #patch, #post, #put
Constructor Details
#initialize(user) ⇒ UserStoryRelation
Returns a new instance of UserStoryRelation.
16 17 18 19 20 21 22 23 24 |
# File 'lib/supplejack/user_story_relation.rb', line 16 def initialize(user) @user = user @stories = [] # This is used to determine if we have requested the Stories from the API # Previously @stories defaulted to nil so the ||= operator was used to determine # whether to fetch. This was changed to allow built/created stories to be # automatically included in the relation @initial_fetch = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Any method missing on this class is delegated to the Stories objects array so that the developer can easily execute any Array method on the UserSttoryRelation
116 117 118 |
# File 'lib/supplejack/user_story_relation.rb', line 116 def method_missing(method, *args, &block) all.send(method, *args, &block) end |
Instance Attribute Details
#user ⇒ Object (readonly)
Returns the value of attribute user.
14 15 16 |
# File 'lib/supplejack/user_story_relation.rb', line 14 def user @user end |
Instance Method Details
#build(attributes = {}) ⇒ Supplejack::Story
Initializes a new Story object and links it to the current User
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/supplejack/user_story_relation.rb', line 62 def build(attributes={}) story = Supplejack::Story.new(attributes) story.api_key = user.api_key # Possible at some point in the future we'll want # to change this to something more robust. # # Just not sure if unsaved Stories being in this # will ever be a problem or not @stories << story story end |
#create(attributes = {}) ⇒ Supplejack::Story
Build and persist a Story object with the provided attributes
82 83 84 85 86 87 |
# File 'lib/supplejack/user_story_relation.rb', line 82 def create(attributes={}) story = build(attributes) story.save story end |
#fetch(force: false) ⇒ Object Also known as: all
Returns an array of Story objects and memoizes the array
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/supplejack/user_story_relation.rb', line 28 def fetch(force: false) fetch_stories = -> { fetch_api_stories.map{|s| Supplejack::Story.new(s.merge(user: user.attributes))} } @stories = if force fetch_stories.call elsif !@initial_fetch @initial_fetch = true fetch_stories.call else @stories end @stories end |
#find(story_id) ⇒ Supplejack::Story
Finds a Story object with the provided ID that belongs to the current User
51 52 53 54 |
# File 'lib/supplejack/user_story_relation.rb', line 51 def find(story_id) path = "/stories/#{story_id}" build get(path, api_key: API_KEY) end |
#order(attribute) ⇒ Array
Return an array of Story objects ordered in ascending order by the specified attribute.
96 97 98 99 100 101 102 103 |
# File 'lib/supplejack/user_story_relation.rb', line 96 def order(attribute) @stories = all.sort_by do |story| value = story.send(attribute) value = value.downcase if value.is_a?(String) value end end |
#to_json(include_contents: true) ⇒ Object
105 106 107 |
# File 'lib/supplejack/user_story_relation.rb', line 105 def to_json(include_contents: true) all.map{|story| story.as_json(include_contents: include_contents)}.to_json end |